উদাহরণ সহ Caching কনফিগারেশন

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

255

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...