@Cacheable এবং @CacheEvict এর মাধ্যমে Cac

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

244

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
Promotion

Are you sure to start over?

Loading...