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
উপকারিতা
- পারফরম্যান্স বৃদ্ধি: বারবার ডেটা রিকোয়েস্ট প্রক্রিয়া করার বদলে ক্যাশ থেকে দ্রুত রেসপন্স করা যায়।
- লোড হ্রাস: সার্ভার এবং ডাটাবেসে লোড কমে।
- ব্যবহারকারী অভিজ্ঞতা উন্নত: দ্রুত রেসপন্স সময়।
আপনার প্রজেক্টের প্রয়োজন অনুযায়ী Spring Cache এর মাধ্যমে SOAP এবং REST সার্ভিস অপ্টিমাইজ করতে পারবেন।
Read more