Spring Web Services এর মধ্যে Caching

স্প্রিং ওয়েব সার্ভিসেস (Spring Web Services) - Java Technologies

281

Spring Web Services-এ caching একটি গুরুত্বপূর্ণ কৌশল, যা ওয়েব সার্ভিসের কার্যক্ষমতা বৃদ্ধি এবং রিসোর্সের উপর চাপ কমানোর জন্য ব্যবহৃত হয়। Spring Framework-এ caching পরিচালনার জন্য সাধারণত Spring Cache Abstraction ব্যবহার করা হয়, যা বিভিন্ন cache providers (যেমন EhCache, Redis, Hazelcast, Guava) এর সঙ্গে কাজ করতে পারে।

Spring Web Services-এ caching কনফিগার করার কিছু প্রধান উপায় নিম্নরূপ:


১. Spring Cache Abstraction ব্যবহার

Spring-এর Cache Abstraction ফিচারটি annotations ব্যবহার করে caching পরিচালনার সুবিধা দেয়। উদাহরণস্বরূপ:

Dependency যুক্ত করুন

আপনার pom.xml ফাইলে caching-এর জন্য প্রয়োজনীয় dependency যোগ করুন (যদি Maven ব্যবহার করেন):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

Caching Enable করা

Spring Boot অ্যাপ্লিকেশনে caching সক্ষম করতে @EnableCaching annotation ব্যবহার করুন:

@Configuration
@EnableCaching
public class CacheConfig {
    // Cache configuration
}

Caching যুক্ত করা

একটি service method-এর আউটপুট cache করতে @Cacheable annotation ব্যবহার করা যেতে পারে:

@Service
public class MyWebService {

    @Cacheable("responses")
    public String getResponse(String request) {
        // Time-consuming operation
        return "Response for " + request;
    }
}

এখানে, প্রথমবার getResponse মেথডটি কল করা হলে এর রিটার্ন ভ্যালু cache করা হবে। পরবর্তীবার একই ইনপুট পেলে cache থেকে ডেটা রিটার্ন হবে।


২. Web Service Response Caching

Spring Web Services-এ response caching চালু করতে নিম্নোক্ত পদ্ধতি অনুসরণ করতে পারেন:

HTTP Caching Headers ব্যবহার

SOAP বা REST response-এ caching-এর জন্য HTTP headers ব্যবহার করতে পারেন। Spring-এর ResponseEntity বা HttpServletResponse ব্যবহার করে caching header যুক্ত করুন:

@Controller
public class MyController {

    @GetMapping("/data")
    public ResponseEntity<String> getData() {
        HttpHeaders headers = new HttpHeaders();
        headers.setCacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS));
        return new ResponseEntity<>("Cached Data", headers, HttpStatus.OK);
    }
}

৩. Caching SOAP Messages

Spring-WS-এ SOAP মেসেজ caching-এর জন্য একটি custom caching interceptor ব্যবহার করতে পারেন।

Custom Interceptor তৈরি

public class CachingInterceptor extends AbstractInterceptor {

    private Cache<String, Source> cache = new ConcurrentHashMap<>();

    @Override
    public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
        String requestKey = generateKey(messageContext);
        if (cache.containsKey(requestKey)) {
            messageContext.getResponse().setPayloadSource(cache.get(requestKey));
            return false; // Short-circuit the invocation
        }
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
        String requestKey = generateKey(messageContext);
        cache.put(requestKey, messageContext.getResponse().getPayloadSource());
        return true;
    }

    private String generateKey(MessageContext messageContext) {
        // Generate a unique key based on the request payload
        return messageContext.getRequest().getPayloadSource().toString();
    }
}

Interceptor রেজিস্টার করা

SOAP configuration-এ interceptor যুক্ত করুন:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Override
    public void addInterceptors(List<EndpointInterceptor> interceptors) {
        interceptors.add(new CachingInterceptor());
    }
}

৪. Redis বা External Cache ব্যবহার

বড় আকারের caching-এর জন্য Redis-এর মতো external cache ব্যবহার করা যেতে পারে। Spring Boot-এ Redis cache ইন্টিগ্রেশন সহজ:

Dependency যুক্ত করুন

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Redis Cache Configuration

application.properties বা application.yml ফাইলে Redis সেটআপ করুন:

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379

উপসংহার

Spring Web Services-এ caching সঠিকভাবে কনফিগার করলে সার্ভিসের কর্মক্ষমতা উল্লেখযোগ্যভাবে উন্নত করা যায়। আপনি Spring Cache Abstraction, HTTP caching headers, SOAP interceptor, অথবা Redis caching-এর মতো পদ্ধতি ব্যবহার করতে পারেন আপনার প্রয়োজন অনুযায়ী।

Content added By

Caching-এর ধারণা:

Caching একটি প্রযুক্তি, যা ডেটা বা রিসোর্সকে দ্রুত অ্যাক্সেস করার জন্য স্মৃতিতে (মেমোরি) সংরক্ষণ করে। সাধারণত, যখন কোনো ক্লায়েন্ট একটি ওয়েব সার্ভিস থেকে ডেটা অনুরোধ করে, তখন সার্ভার বারবার ডেটাবেস বা কোনো ভারী প্রসেসিং থেকে সেই ডেটা রিটার্ন করে। এই বারবার প্রসেসিং সময়সাপেক্ষ এবং ব্যয়বহুল। caching ব্যবহারের মাধ্যমে এই ডেটাগুলি সাময়িকভাবে মেমোরিতে সংরক্ষণ করা হয়, যাতে ভবিষ্যতে একই রিসোর্সের অনুরোধে তা দ্রুত সরবরাহ করা যায়।

Spring Framework-এ Caching:

Spring Framework caching সমর্থন করে, যা বিভিন্ন caching সমাধান (যেমন EhCache, Redis, Guava, Caffeine) এর সাথে একত্রে কাজ করতে পারে। Spring-এর caching abstraction API ব্যবহার করে ডেভেলপাররা সহজে caching-এর সুবিধা পেতে পারেন।

Caching-এর প্রয়োজনীয়তা:

  1. পারফরম্যান্স উন্নত করা:
    • একবার ডেটা মেমোরিতে সংরক্ষণ করার পর, একই ডেটা পুনরুদ্ধারের জন্য কম সময়ে প্রসেস করা যায়।
    • বারবার ডেটাবেস অ্যাক্সেস করার প্রয়োজন কমে যায়।
  2. লোড হ্রাস করা:
    • ডেটাবেস বা সার্ভারে কম অনুরোধ গেলে সার্ভারের উপর চাপ কমে।
    • এটি স্কেলেবল আর্কিটেকচার তৈরিতে সাহায্য করে।
  3. রেসপন্স টাইম কমানো:
    • ক্লায়েন্ট অনুরোধের দ্রুত রেসপন্স দেয়ার জন্য caching অপরিহার্য।
    • এটি ব্যবহারকারীর অভিজ্ঞতা (User Experience) উন্নত করে।
  4. নেটওয়ার্ক ব্যান্ডউইথ সাশ্রয়:
    • ক্লায়েন্ট এবং সার্ভারের মধ্যে বারবার ডেটা ট্রান্সফারের প্রয়োজনীয়তা হ্রাস পায়।
  5. অস্থায়ী ডেটার পুনর্ব্যবহার:
    • এমন ডেটা যা প্রায়ই পরিবর্তিত হয় না (যেমন কনফিগারেশন ডেটা বা সাধারণ স্ট্যাটিক ডেটা) caching-এর মাধ্যমে সংরক্ষণ করা হলে সিস্টেমের সামগ্রিক কার্যক্ষমতা উন্নত হয়।

Spring Web Services-এ Caching-এর ব্যবহার:

  1. Caching Annotation:
    • Spring caching সক্রিয় করার জন্য @EnableCaching এনোটেশন ব্যবহার করা হয়।
    • ডেটা ক্যাশিংয়ের জন্য @Cacheable এবং ক্যাশ পরিষ্কার করার জন্য @CacheEvict এনোটেশন ব্যবহৃত হয়।
  2. HTTP Cache Headers:
    • HTTP প্রোটোকলের ক্যাশ কন্ট্রোল হেডার ব্যবহার করে ব্রাউজার এবং প্রক্সি সার্ভারগুলিতে স্ট্যাটিক বা ডায়নামিক ডেটা ক্যাশ করা যায়।
  3. RESTful Web Services-এর জন্য ETag:
    • Spring Web Services-এ ETag ব্যবহারের মাধ্যমে ক্যাশ ভ্যালিডেশনের সুবিধা পাওয়া যায়।

উদাহরণ: Spring-এ Caching ব্যবহারের উদাহরণ

@Service
public class ProductService {

    @Cacheable("products")
    public Product getProductById(Long id) {
        // ভারী ডেটাবেস অনুরোধ সিমুলেট করা হচ্ছে
        simulateSlowService();
        return new Product(id, "Sample Product");
    }

    private void simulateSlowService() {
        try {
            Thread.sleep(3000); // 3 সেকেন্ড বিলম্ব
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

উপসংহার:

Caching একটি গুরুত্বপূর্ণ কৌশল, যা Spring Web Services-এর পারফরম্যান্স বৃদ্ধি করে, স্কেলেবিলিটি উন্নত করে এবং সার্ভার লোড হ্রাস করে। এটি ব্যবহারকারীর অভিজ্ঞতাকে উন্নত করার জন্য একটি অপরিহার্য উপাদান। Spring Framework caching-এর জন্য উন্নত API এবং ইন্টিগ্রেশন সমর্থন প্রদান করে, যা ডেভেলপারদের এটি সহজে ব্যবহার করতে সাহায্য করে।

Content added By

Spring Caching ব্যবহার করে Spring Web Services (SOAP এবং REST) এর পারফরম্যান্স অপটিমাইজ করা একটি কার্যকরী পদ্ধতি। এটি ক্যাশিংয়ের মাধ্যমে রিকোয়েস্ট-রেসপন্স চক্রের সময় কমিয়ে আনে এবং সার্ভারের লোড হ্রাস করে।

নিচে Spring Caching ব্যবহার করে Web Services Performance Optimization করার ধাপগুলো দেওয়া হলো:


Spring Caching ইন্ট্রোডাকশন

Spring Cache একটি abstraction প্রদান করে যেখানে বিভিন্ন ক্যাশিং মেকানিজম (যেমন EhCache, Redis, Caffeine) ব্যবহার করে ক্যাশিং পরিচালনা করা যায়।


Spring Caching কনফিগারেশন

স্টেপ ১: ডিপেন্ডেন্সি যোগ করুন

আপনার প্রজেক্টে নিচের ডিপেন্ডেন্সি যুক্ত করুন:

Maven
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
Gradle
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.ehcache:ehcache'

স্টেপ ২: ক্যাশিং সক্রিয় করুন

Spring Boot এ ক্যাশিং চালু করতে @EnableCaching ব্যবহার করুন:

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
    // Additional cache configurations (if needed)
}

স্টেপ ৩: ক্যাশ এনোটেশন ব্যবহার করুন

ক্যাশিং করতে, সেবার মেথডে @Cacheable, @CachePut, এবং @CacheEvict এনোটেশন ব্যবহার করুন:

  • @Cacheable: ডেটা ক্যাশে রাখে।
  • @CachePut: ক্যাশে ডেটা আপডেট করে।
  • @CacheEvict: ক্যাশ থেকে ডেটা মুছে ফেলে।

SOAP বা REST এপিআই রিকোয়েস্টের জন্য একটি উদাহরণ:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class WebService {

    @Cacheable(value = "myServiceCache", key = "#request.id")
    public String processRequest(Request request) {
        // Simulate a slow service call
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Processed: " + request.getData();
    }
}

EhCache কনফিগারেশন

Ehcache.xml ফাইল যোগ করুন

আপনার resources ডিরেক্টরিতে ehcache.xml নামে একটি ফাইল তৈরি করুন:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">

    <cache alias="myServiceCache">
        <expiry>
            <ttl unit="seconds">300</ttl>
        </expiry>
        <heap unit="entries">1000</heap>
    </cache>

</config>

Spring এ EhCache কনফিগার করুন

Spring এর মাধ্যমে EhCache ফাইল লোড করতে CacheManager ব্যবহার করুন:

import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.cache.CacheManager;
import javax.cache.Caching;

@Configuration
@EnableCaching
public class EhCacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return Caching.getCachingProvider(EhcacheCachingProvider.class.getName()).getCacheManager();
    }
}

REST এবং SOAP সার্ভিসে ক্যাশিং কনফিগারেশন

REST এবং SOAP উভয় সার্ভিসেই ক্যাশিং করা যায়:

REST Controller এ ক্যাশিং

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {

    @Cacheable(value = "restCache", key = "#id")
    @GetMapping("/api/data")
    public String getData(String id) {
        // Simulate a slow database call
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Data for ID: " + id;
    }
}

SOAP Endpoint এ ক্যাশিং

import org.springframework.cache.annotation.Cacheable;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;

@Endpoint
public class SoapService {

    private static final String NAMESPACE_URI = "http://example.com/soap";

    @Cacheable(value = "soapCache", key = "#request.id")
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetRequest")
    public String handleRequest(@RequestPayload Request request) {
        // Simulate a slow service call
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Processed SOAP Request: " + request.getData();
    }
}

Spring Boot Configuration in application.properties

# Enable cache statistics
spring.cache.ehcache.config=classpath:ehcache.xml
logging.level.org.springframework.cache=DEBUG

উপকারিতা

  1. পারফরম্যান্স বৃদ্ধি: বারবার ডেটা রিকোয়েস্ট প্রক্রিয়া করার বদলে ক্যাশ থেকে দ্রুত রেসপন্স করা যায়।
  2. লোড হ্রাস: সার্ভার এবং ডাটাবেসে লোড কমে।
  3. ব্যবহারকারী অভিজ্ঞতা উন্নত: দ্রুত রেসপন্স সময়।

আপনার প্রজেক্টের প্রয়োজন অনুযায়ী Spring Cache এর মাধ্যমে SOAP এবং REST সার্ভিস অপ্টিমাইজ করতে পারবেন।

Content added By

Spring Web Services-এ @Cacheable এবং @CacheEvict অ্যানোটেশন ব্যবহার করে ক্যাশিং (Caching) প্রক্রিয়া কার্যকর করা যায়। এই দুটি অ্যানোটেশন Spring Framework এর ক্যাশ অ্যাবস্ট্রাকশন সাপোর্টের অংশ, যা সিস্টেমের কার্যক্ষমতা উন্নত করতে ডেটা ক্যাশিং করে।


@Cacheable এবং @CacheEvict এর উদ্দেশ্য

  1. @Cacheable:
    • এটি একটি পদ্ধতিতে ডেটা ক্যাশ করে।
    • যদি একই ইনপুট দিয়ে একটি পদ্ধতি বারবার ডাকা হয়, তবে ক্যাশ থেকে ফলাফল ফেরত দেওয়া হবে, পুনরায় পদ্ধতিটি কার্যকর করা হবে না।
    • এটি ডাটাবেস বা ওয়েব সার্ভিস থেকে ডেটা রিট্রিভ করার ক্ষেত্রে কার্যকর।
  2. @CacheEvict:
    • এটি নির্দিষ্ট ক্যাশ থেকে ডেটা মুছে দেয়।
    • সাধারণত, ক্যাশ ডেটা পুরনো হয়ে গেলে বা নতুন ডেটা যুক্ত হলে ক্যাশ আপডেট করার জন্য ব্যবহার করা হয়।

কনফিগারেশন ধাপগুলো

১. ডিপেন্ডেন্সি যুক্ত করা

Spring Cache ব্যবহার করার জন্য, আপনার Maven বা Gradle ফাইলে প্রয়োজনীয় ডিপেন্ডেন্সি যুক্ত করুন।

Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>3.1.5</version>
</dependency>

Gradle:

implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.5'

২. Spring Boot ক্যাশ এনাবল করা

Spring Cache ব্যবহার শুরু করার জন্য আপনার মেইন ক্লাসে @EnableCaching অ্যানোটেশন যুক্ত করতে হবে।

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class SpringWsApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringWsApplication.class, args);
    }
}

৩. ক্যাশিং কনফিগারেশন (Cache Configuration)

Spring Framework বিভিন্ন ক্যাশিং প্রোভাইডার সাপোর্ট করে, যেমন Caffeine, EhCache, বা Redis। উদাহরণ হিসেবে Caffeine ক্যাশ ব্যবহার করা হয়েছে।

application.yml ফাইল কনফিগারেশন:

spring:
  cache:
    type: caffeine
  caffeine:
    spec: maximumSize=100,expireAfterAccess=5m

৪. @Cacheable ব্যবহার করা

যেখানে ডেটা ক্যাশ করতে চান, সেখানে @Cacheable অ্যানোটেশন ব্যবহার করুন। উদাহরণস্বরূপ, একটি প্রোডাক্ট ডেটা ক্যাশ করা হচ্ছে:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

    @Cacheable("products")
    public Product getProductById(String productId) {
        // Simulate a time-consuming operation
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Product(productId, "Sample Product");
    }
}

বিবরণ:

  • "products" ক্যাশে ডেটা রাখা হবে।
  • প্রথমবার getProductById কল করলে ডেটা তৈরি হবে এবং ক্যাশে রাখা হবে।
  • পরবর্তী বার একই productId দিয়ে কল করলে, ডেটা ক্যাশ থেকে ফিরবে।

৫. @CacheEvict ব্যবহার করা

ক্যাশ পরিষ্কার করতে বা পুরনো ডেটা মুছতে @CacheEvict ব্যবহার করা হয়।

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class ProductService {

    @CacheEvict(value = "products", allEntries = true)
    public void clearAllProductsCache() {
        System.out.println("All product cache cleared!");
    }

    @CacheEvict(value = "products", key = "#productId")
    public void clearProductCache(String productId) {
        System.out.println("Cache cleared for product: " + productId);
    }
}

বিবরণ:

  • clearAllProductsCache: সব ডেটা "products" ক্যাশ থেকে মুছে ফেলবে।
  • clearProductCache: নির্দিষ্ট productId এর ডেটা ক্যাশ থেকে মুছে ফেলবে।

৬. টেস্টিং এবং লগিং

ক্যাশ কাজ করছে কিনা তা নিশ্চিত করতে, লগিং চালু করুন। Spring Cache ডিবাগ মেসেজ দেখতে application.yml এ নিচের সেটআপ করুন:

logging:
  level:
    org.springframework.cache: DEBUG

উদাহরণ Spring Controller এ ক্যাশিং প্রয়োগ

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/product")
    public Product getProduct(String productId) {
        return productService.getProductById(productId);
    }

    @GetMapping("/clear-cache")
    public String clearCache(String productId) {
        productService.clearProductCache(productId);
        return "Cache cleared for product: " + productId;
    }
}

উপসংহার

  • @Cacheable ডেটা ক্যাশ করে, যা পুনরায় পদ্ধতি চালানো থেকে রক্ষা করে।
  • @CacheEvict ক্যাশ পরিষ্কার বা নির্দিষ্ট ডেটা অপসারণে ব্যবহার হয়।
  • সঠিক ক্যাশ স্ট্র্যাটেজি নির্বাচন করে অ্যাপ্লিকেশনের কার্যক্ষমতা উল্লেখযোগ্যভাবে বৃদ্ধি করা যায়।
Content added By

Spring Web Services-এ ক্যাশিং (Caching) কনফিগারেশন করার জন্য Spring Framework ক্যাশিং অ্যাবস্ট্রাকশন ব্যবহার করা হয়। এটি অ্যাপ্লিকেশনের পারফরম্যান্স উন্নত করতে সাহায্য করে। ক্যাশিং ব্যবহার করে কোনো সেবা বা ডাটাবেস থেকে প্রাপ্ত ডেটা পুনরায় প্রক্রিয়া না করে দ্রুত রেসপন্স প্রদান করা যায়।

নিচে উদাহরণসহ Spring Web Services-এ Caching কনফিগারেশন দেখানো হলো:


১. Maven ডিপেনডেন্সি যোগ করা

Spring Caching ব্যবহারের জন্য প্রয়োজনীয় ডিপেনডেন্সি spring-context-support অ্যাড করতে হবে:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

২. ক্যাশিং সক্ষম করা

Spring Boot অ্যাপ্লিকেশনে ক্যাশিং সক্রিয় করতে, @EnableCaching অ্যানোটেশন ব্যবহার করতে হবে।

উদাহরণ:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class CachingApplication {
    public static void main(String[] args) {
        SpringApplication.run(CachingApplication.class, args);
    }
}

৩. ক্যাশিং কনফিগারেশন

Spring Boot ডিফল্ট ক্যাশ ব্যবস্থাপনা প্রদান করে। তবে যদি নির্দিষ্ট ক্যাশ সরবরাহকারী (যেমন EhCache, Redis) ব্যবহার করতে চান, তাহলে তা কনফিগার করতে হবে।

ডিফল্ট In-Memory ক্যাশ ব্যবহার:

Spring Boot ডিফল্টভাবে ConcurrentMapCache ব্যবহার করে। আলাদা কনফিগারেশন ছাড়াই এটি কাজ করে।


৪. ক্যাশিং ব্যবহার করা

@Cacheable অ্যানোটেশন ব্যবহার করে ক্যাশিং প্রয়োগ করা যায়। এটি একটি নির্দিষ্ট মেথড বা সার্ভিসের ফলাফলকে ক্যাশ করে রাখে।

উদাহরণ:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class WeatherService {

    @Cacheable("weather")
    public String getWeather(String city) {
        simulateSlowService(); // এটি স্লো প্রসেসিং সিমুলেট করে
        return "Weather in " + city + " is Sunny!";
    }

    private void simulateSlowService() {
        try {
            Thread.sleep(3000); // 3 সেকেন্ড বিলম্ব
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
}

উপরের উদাহরণে:

  • প্রথমবার মেথডটি কল করলে ডেটা প্রসেস হবে।
  • পরবর্তীবারে একই প্যারামিটার দিয়ে কল করলে ক্যাশ থেকে ডেটা রিটার্ন হবে।

৫. ক্যাশ পরিস্কার করা

@CacheEvict ব্যবহার করে ক্যাশ পরিস্কার করা যায়।

উদাহরণ:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class WeatherService {

    @CacheEvict(value = "weather", allEntries = true)
    public void clearWeatherCache() {
        System.out.println("Weather cache cleared!");
    }
}

৬. ক্যাশ কনফিগারেশন প্রপার্টি

application.properties ফাইলে ক্যাশ সম্পর্কিত কনফিগারেশন:

spring.cache.type=simple
spring.cache.cache-names=weather

অথবা Redis/EhCache ব্যবহার করলে:

Redis উদাহরণ:

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379

৭. ক্যাশিং টেস্ট করা

ক্যাশিং কাজ করছে কি না যাচাই করার জন্য নিচের কোডটি ব্যবহার করা যায়:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestCaching implements CommandLineRunner {

    @Autowired
    private WeatherService weatherService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(weatherService.getWeather("Dhaka"));
        System.out.println(weatherService.getWeather("Dhaka")); // দ্বিতীয়বার দ্রুত ফলাফল দেবে
    }
}

উপসংহার

Spring Web Services-এ ক্যাশিং কনফিগারেশন প্রয়োগ করে অ্যাপ্লিকেশনের পারফরম্যান্স অনেক বাড়ানো যায়। Spring Boot-এর ডিফল্ট ইন-মেমোরি ক্যাশ ছোট প্রজেক্টে কার্যকর, তবে বড় স্কেলে Redis বা EhCache ব্যবহার করা ভালো।

Content added By
Promotion

Are you sure to start over?

Loading...