Caching একটি গুরুত্বপূর্ণ পারফরম্যান্স অপটিমাইজেশন কৌশল যা ডাটাবেসের পুনরাবৃত্ত ডেটা রিড অপারেশনের পরিমাণ কমাতে সাহায্য করে। Hibernate Caching এবং EhCache দুইটি জনপ্রিয় ক্যাশিং প্রযুক্তি যা স্প্রিং বুট জেপিএ (Spring Boot JPA) অ্যাপ্লিকেশনগুলোতে ক্যাশিং প্রয়োগ করার জন্য ব্যবহৃত হয়।
EhCache হল একটি শক্তিশালী, ওপেন সোর্স ক্যাশিং লাইব্রেরি যা স্প্রিং এবং হিবারনেটের জন্য কার্যকর ক্যাশিং সমাধান প্রদান করে। Hibernate Caching হল হিবারনেটের অন্তর্নিহিত ক্যাশিং সমাধান, যা দ্বিতীয় স্তরের ক্যাশ (second-level cache) এর মাধ্যমে ডেটার ক্যাশিং সক্ষম করে।
এখানে আমরা দেখব কিভাবে EhCache এবং Hibernate Caching কনফিগারেশন করা যায় স্প্রিং বুট জেপিএ অ্যাপ্লিকেশনে।
Step 1: Maven ডিপেনডেন্সি যোগ করা
Maven ডিপেনডেন্সি:
pom.xml ফাইলে নিচের ডিপেনডেন্সি যোগ করুন:
<dependencies>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- EhCache dependency -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.9.0</version>
</dependency>
<!-- Hibernate Caching dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- Spring Boot Starter Web (for REST API) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Test (for testing) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
এখানে hibernate-ehcache ডিপেনডেন্সি দিয়ে Hibernate-এ EhCache ইন্টিগ্রেশন সাপোর্ট করা হয়েছে। এছাড়া spring-boot-starter-data-jpa ডিপেনডেন্সি দিয়ে JPA এর জন্য প্রয়োজনীয় স্টার্টার যুক্ত করা হয়েছে।
Step 2: application.properties বা application.yml কনফিগারেশন
এখন application.properties ফাইলে Hibernate ও EhCache কনফিগারেশন সেট করা হবে।
application.properties:
# Enable Hibernate second-level cache
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
# Cache settings for EhCache
spring.cache.type=ehcache
এখানে, hibernate.cache.use_second_level_cache=true দ্বারা Hibernate এর দ্বিতীয় স্তরের ক্যাশিং সক্রিয় করা হয়েছে এবং hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory দিয়ে Hibernate কে জানানো হয়েছে যে, ক্যাশিংয়ের জন্য EhCache ব্যবহার করা হবে।
Step 3: EhCache কনফিগারেশন ফাইল তৈরি করা
এখন, EhCache কনফিগারেশন ফাইল তৈরি করতে হবে। এটি একটি XML ফাইল হিসেবে তৈরি করা হয় যেখানে ক্যাশিং কনফিগারেশন থাকবে।
EhCache Config (ehcache.xml):
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns="http://www.ehcache.org/v3">
<cache alias="productCache">
<key-type>java.lang.Long</key-type>
<value-type>com.example.Product</value-type>
<resources>
<heap>1000</heap>
<offheap>10MB</offheap>
</resources>
</cache>
</ehcache>
এখানে, productCache একটি ক্যাশ এলিয়াস হিসেবে ব্যবহার করা হয়েছে যা Product Entity এর জন্য কাজ করবে। heap এবং offheap দ্বারা ক্যাশের মেমরি কনফিগারেশন দেওয়া হয়েছে। এটি ক্যাশে কী এবং মানের টাইপও নির্ধারণ করে।
Step 4: Entity ক্লাসে ক্যাশিং প্রয়োগ করা
স্প্রিং বুট জেপিএ এর Entity ক্লাসে ক্যাশিং প্রয়োগ করতে @Cacheable অ্যানোটেশন ব্যবহার করা হয়। @Cacheable অ্যানোটেশন স্প্রিং ক্যাশিং কনফিগারেশন প্রয়োগ করে, এবং এটি ক্যাশে ডেটা রাখার জন্য ব্যবহৃত হয়।
Product Entity Example:
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Product {
@Id
private Long id;
private String name;
private double price;
// Constructors, Getters, Setters
}
এখানে, @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) ব্যবহার করে Product Entity এর জন্য Hibernate দ্বিতীয় স্তরের ক্যাশিং সক্রিয় করা হয়েছে। এটি Entity লেভেলে ক্যাশ কনফিগারেশন নির্ধারণ করে।
Step 5: Service Layer এবং Controller তৈরি করা
Service Layer এর মাধ্যমে ক্যাশিং করা যাবে এবং Controller Layer দিয়ে ক্যাশড ডেটা রিটার্ন করা হবে।
Service Layer Example:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
@Cacheable(value = "productCache", key = "#id")
public Product getProductById(Long id) {
System.out.println("Fetching product from database...");
return productRepository.findById(id).orElse(null);
}
}
এখানে, @Cacheable অ্যানোটেশন ব্যবহার করে getProductById মেথডের ক্যাশিং নিশ্চিত করা হয়েছে। এটি productCache নামের ক্যাশ ব্যবহার করবে এবং id প্যারামিটারকে কী হিসেবে ব্যবহার করবে।
Controller Layer Example:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/{id}")
public Product getProduct(@PathVariable Long id) {
return productService.getProductById(id);
}
}
এখানে, ProductController ক্লাসে ProductService এর মাধ্যমে ক্যাশিং করা হয়েছে এবং REST API ব্যবহার করে ক্যাশড ডেটা রিটার্ন করা হচ্ছে।
Step 6: Testing the Cache
এখন, যখন আপনি প্রথমবার getProductById মেথডটি কল করবেন, তখন ডেটা ডাটাবেস থেকে ফেচ হবে। পরবর্তী সময়ে যদি একই id সহ আরেকটি রিকোয়েস্ট আসে, তাহলে ডেটা ক্যাশ থেকে রিটার্ন হবে, এবং ডেটাবেস থেকে আবার ফেচ করা হবে না।
সারাংশ
EhCache এবং Hibernate Caching ব্যবহার করে স্প্রিং বুট অ্যাপ্লিকেশনে ক্যাশিং কনফিগারেশন করার মাধ্যমে আপনি অ্যাপ্লিকেশনের পারফরম্যান্স উন্নত করতে পারেন। EhCache একটি শক্তিশালী ক্যাশিং ফিচার যা স্প্রিং বুট এবং হিবারনেটের সাথে ইন্টিগ্রেট করে ডেটা লোডের সময় কমাতে সহায়তা করে। @Cacheable, @Cache এবং Hibernate ক্যাশ কনফিগারেশন ব্যবহার করে ক্যাশিং করতে পারেন, যা পুনরাবৃত্ত ডেটা রিড অপারেশন কমিয়ে অ্যাপ্লিকেশনের কার্যক্ষমতা বৃদ্ধি করে।
Read more