উদাহরণ সহ Caching Implementation

Spring ORM এবং Caching - স্প্রিং ওআরএম (Spring ORM) - Java Technologies

283

ক্যাশিং কি?

Caching হলো ডেটা স্টোর করার একটি মেকানিজম, যা বারবার ডাটাবেজে অ্যাকসেস না করে প্রয়োজনীয় ডেটা দ্রুত সরবরাহ করতে সহায়তা করে। Spring ORM এ Hibernate এর ক্যাশিং মেকানিজম ব্যবহার করে ডাটাবেজ অপারেশনের পারফরম্যান্স বৃদ্ধি করা যায়। Hibernate দুই ধরনের ক্যাশিং সরবরাহ করে:

  1. First Level Cache: ডিফল্টভাবে সক্রিয় এবং একটি সেশন পর্যন্ত সীমাবদ্ধ।
  2. Second Level Cache: কনফিগারেশন-ভিত্তিক এবং একাধিক সেশনের মধ্যে শেয়ার করা যায়।

ক্যাশিং এর ধাপ

১. First Level Cache

Hibernate সেশনের লেভেলে স্বয়ংক্রিয়ভাবে কাজ করে। এই ক্যাশিং এর জন্য আলাদা কোনো কনফিগারেশন প্রয়োজন নেই।

২. Second Level Cache

Spring ORM এ Hibernate Second Level Cache ব্যবহার করতে হলে এটি স্পষ্টভাবে কনফিগার করতে হয়। Hibernate বিভিন্ন ক্যাশিং প্রোভাইডার সাপোর্ট করে, যেমন:

  • Ehcache
  • Redis
  • Infinispan

উদাহরণ সহ Second Level Cache ইমপ্লিমেন্টেশন

প্রয়োজনীয় ডিপেনডেন্সি

Maven এর pom.xml ফাইলে Hibernate এবং Ehcache এর ডিপেনডেন্সি যোগ করুন:

<dependencies>
    <!-- Hibernate Core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.15.Final</version>
    </dependency>

    <!-- Hibernate Ehcache -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.6.15.Final</version>
    </dependency>

    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.3.29</version>
    </dependency>
</dependencies>

Hibernate কনফিগারেশন ফাইল

Hibernate এর hibernate.cfg.xml ফাইলে Second Level Cache এর জন্য কনফিগারেশন যোগ করুন:

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

        <!-- Hibernate properties -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>

        <!-- Enable Second Level Cache -->
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="hibernate.cache.use_query_cache">true</property>
    </session-factory>
</hibernate-configuration>

Ehcache কনফিগারেশন ফাইল

Ehcache এর জন্য ehcache.xml ফাইল তৈরি করুন:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd">
    <cache name="com.example.Employee"
           maxEntriesLocalHeap="1000"
           timeToLiveSeconds="300">
    </cache>
</ehcache>

Entity ক্লাস

ক্যাশিং যোগ করার জন্য Entity ক্লাসে @Cache অ্যানোটেশন যোগ করুন:

Employee.java

import jakarta.persistence.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee {
    @Id
    private int id;
    private String name;
    private String department;

    // Getters and Setters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
}

DAO ক্লাসে ক্যাশিং পরীক্ষা

EmployeeDAO.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
public class EmployeeDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public Employee getEmployeeById(int id) {
        Session session = sessionFactory.getCurrentSession();
        return session.get(Employee.class, id); // First Level Cache
    }

    @Transactional
    public Employee getEmployeeWithSecondLevelCache(int id) {
        Session session = sessionFactory.getCurrentSession();
        return session.get(Employee.class, id); // Second Level Cache
    }
}

Main ক্লাস

App.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        EmployeeDAO employeeDAO = context.getBean(EmployeeDAO.class);

        // First Query (Hits Database)
        Employee emp1 = employeeDAO.getEmployeeById(1);
        System.out.println("Employee 1: " + emp1.getName());

        // Second Query (Uses Cache)
        Employee emp2 = employeeDAO.getEmployeeWithSecondLevelCache(1);
        System.out.println("Employee 2: " + emp2.getName());
    }
}

ক্যাশিং এর সুবিধা এবং সীমাবদ্ধতা

সুবিধা:

  • ডাটাবেস লোড কমায়।
  • অ্যাপ্লিকেশনের পারফরম্যান্স বৃদ্ধি করে।
  • বারবার ডেটা ফেচিং এ সময় সাশ্রয় করে।

সীমাবদ্ধতা:

  • কনফিগারেশন জটিল।
  • ক্যাশিং সঠিকভাবে ব্যবস্থাপনা না করলে Stale Data সমস্যার উদ্ভব হতে পারে।
  • মেমরি ব্যবহারের জন্য অতিরিক্ত রিসোর্স প্রয়োজন।

Hibernate Second Level Cache এর মাধ্যমে Spring ORM এ কার্যকর ক্যাশিং বাস্তবায়ন করা যায়। Ehcache এর মত ক্যাশিং প্রোভাইডার ব্যবহার করে অ্যাপ্লিকেশনের পারফরম্যান্স বৃদ্ধি করা সহজ হয়।

Content added By
Promotion

Are you sure to start over?

Loading...