উদাহরণ সহ Spring Caching ব্যবস্থাপনা

Spring Caching এবং Performance Optimization - স্প্রিং (Spring) - Java Technologies

310

Spring Caching একটি শক্তিশালী ফিচার যা অ্যাপ্লিকেশন ডেভেলপমেন্টে ক্যাশিং ব্যবস্থাপনা সহজ এবং কার্যকরী করে তোলে। ক্যাশিং হল একটি টেকনিক যা অস্থায়ীভাবে ডেটা সংরক্ষণ করে, যাতে একই ডেটার জন্য বারবার ডাটাবেস বা অন্য কোনো সোর্স থেকে রিকোয়েরি না করতে হয়। এটি অ্যাপ্লিকেশনের পারফরম্যান্স এবং প্রতিক্রিয়া সময় (response time) উন্নত করতে সাহায্য করে।

Spring Caching ফ্রেমওয়ার্ক annotation-based caching সমর্থন করে, যা সহজেই ক্যাশিং কনফিগার করা যায়। Spring Caching অনেক ক্যাশ মেকানিজমের সাথে ইন্টিগ্রেট করতে পারে, যেমন EHCache, Redis, Caffeine, Simple Map ইত্যাদি।

Spring Caching এর মূল উপাদান

  1. @EnableCaching: Spring Caching সক্রিয় করার জন্য ব্যবহৃত এনোটেশন।
  2. @Cacheable: মেথডে ক্যাশিং সক্ষম করতে ব্যবহৃত হয়।
  3. @CachePut: ক্যাশে আপডেট করার জন্য ব্যবহৃত হয়।
  4. @CacheEvict: ক্যাশে থেকে ডেটা মুছতে ব্যবহৃত হয়।

১. Spring Caching সেটআপ এবং কনফিগারেশন

প্রথমে আপনার pom.xml বা build.gradle ফাইলে Spring Caching এবং ক্যাশ মেকানিজম (যেমন Redis বা EhCache) ডিপেনডেন্সি যোগ করতে হবে।

pom.xml (Maven)

<dependencies>
    <!-- Spring Cache and EhCache -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <!-- EhCache Dependency -->
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
</dependencies>

build.gradle (Gradle)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-cache'
    implementation 'org.ehcache:ehcache'
}

২. Spring Caching Enable করা

@EnableCaching এনোটেশন ব্যবহার করে Spring Caching সক্রিয় করতে হবে।

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching  // Enable Spring Caching
public class CacheConfig {
    // Additional cache configuration can go here (if needed)
}

৩. @Cacheable, @CachePut, @CacheEvict এর ব্যবহার

@Cacheable: ক্যাশে ডেটা রাখা

@Cacheable এনোটেশন ব্যবহার করলে একটি মেথডের রিটার্ন ভ্যালু ক্যাশে রাখা হয়। পরবর্তীবার যখন সেই মেথডটি একই আর্গুমেন্ট সহ কল হবে, তখন ক্যাশ থেকে ডেটা ফেরত আসবে, ডাটাবেস বা অন্য সোর্স থেকে আবার রিকোয়েস্ট করতে হবে না।

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Cacheable(value = "users", key = "#id")  // Cache the result with 'users' cache and use the 'id' as key
    public User getUserById(Long id) {
        // Simulating a database call (or any expensive operation)
        System.out.println("Fetching user from the database...");
        return new User(id, "John Doe");  // Return a user object
    }
}

ব্যাখ্যা:

  • এখানে getUserById মেথডটি প্রথমবার যখন কল হবে, তখন ডেটা ক্যাশে সংরক্ষিত হবে। পরবর্তীবার যখন একই id সহ কল হবে, ক্যাশ থেকে ফলস্বরূপ পাওয়া যাবে, ডাটাবেসে যাওয়ার প্রয়োজন হবে না।

@CachePut: ক্যাশে আপডেট করা

@CachePut ব্যবহার করে ক্যাশে ডেটা আপডেট করা হয়, এমনকি মেথডটি চালু থাকলেও। এটি মেথডের রিটার্ন ভ্যালু ক্যাশে আপডেট করে।

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @CachePut(value = "users", key = "#user.id")
    public User updateUser(User user) {
        // Simulate updating the user in the database
        System.out.println("Updating user in the database...");
        return user;
    }
}

ব্যাখ্যা:

  • এখানে updateUser মেথডটি ব্যবহৃত হলে ক্যাশে সংশ্লিষ্ট ইউজারের ডেটা আপডেট করা হবে।

@CacheEvict: ক্যাশ থেকে ডেটা মুছে ফেলা

@CacheEvict ব্যবহার করে ক্যাশ থেকে ডেটা মুছে ফেলা হয়। এটি সাধারণত ডেটাবেসে ডেটা পরিবর্তিত হলে পুরনো ক্যাশ ডেটা মুছে ফেলতে ব্যবহৃত হয়।

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @CacheEvict(value = "users", key = "#id")
    public void deleteUser(Long id) {
        // Simulating user deletion from the database
        System.out.println("Deleting user from the database...");
    }
}

ব্যাখ্যা:

  • deleteUser মেথডটি কল হওয়ার পর ক্যাশ থেকে ঐ ইউজারের ডেটা মুছে যাবে, যাতে পরবর্তী সময়ে সঠিক ডেটা ফেচ করা যায়।

৪. EhCache Configuration Example

Spring Caching ব্যবহারের জন্য একটি ক্যাশ মেকানিজম কনফিগার করতে হবে। এখানে EhCache ব্যবহার করে ক্যাশ কনফিগার করার উদাহরণ দেওয়া হল।

ehcache.xml (EhCache Configuration)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.ehcache.org/schema/ehcache
             http://www.ehcache.org/schema/ehcache/2.10/ehcache.xsd"
         updateCheck="false">
    <cache name="users"
           maxEntriesLocalHeap="1000"
           eternal="false"
           timeToLiveSeconds="600"
           timeToIdleSeconds="300" />
</ehcache>

ব্যাখ্যা:

  • users নামে একটি ক্যাশ কনফিগার করা হয়েছে যার মধ্যে ১০০০ এন্ট্রি থাকতে পারবে। এই ক্যাশে ডেটা ১০ মিনিট (600 সেকেন্ড) পর্যন্ত জীবিত থাকবে।

৫. Spring Boot Application Class

এখন, Spring Boot অ্যাপ্লিকেশন ক্লাসে CommandLineRunner ব্যবহার করে ক্যাশিং ফিচার পরীক্ষা করা যায়:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringCachingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCachingApplication.class, args);
    }

    @Bean
    public CommandLineRunner demo(UserService userService) {
        return args -> {
            // First call: fetches from database and caches it
            System.out.println(userService.getUserById(1L));
            // Second call: fetches from cache
            System.out.println(userService.getUserById(1L));
        };
    }
}

ব্যাখ্যা:

  • প্রথমবার যখন getUserById(1L) মেথডটি কল হবে, এটি ডেটাবেস থেকে ডেটা নিয়ে আসবে এবং ক্যাশে সংরক্ষণ করবে। পরবর্তী কলটি ক্যাশ থেকে ডেটা ফেরত দেবে, ডাটাবেস থেকে আবার রিকোয়েস্ট করার প্রয়োজন হবে না।

সারাংশ

Spring Caching একটি শক্তিশালী ফিচার যা অ্যাপ্লিকেশনের পারফরম্যান্স উন্নত করতে ব্যবহৃত হয়। @Cacheable, @CachePut, এবং @CacheEvict এনোটেশন ব্যবহার করে আপনি সহজেই ডেটা ক্যাশ করতে, ক্যাশে আপডেট করতে এবং ক্যাশ থেকে ডেটা মুছে ফেলতে পারেন। Spring Caching Spring-managed ক্যাশ মেকানিজমের মাধ্যমে ডেটা দ্রুত অ্যাক্সেস করার সুবিধা দেয়। EhCache বা Redis এর মতো ক্যাশ মেকানিজম ব্যবহার করে ডেটাবেসের চাপ কমানো যায় এবং অ্যাপ্লিকেশনের প্রতিক্রিয়া সময় (response time) বৃদ্ধি পায়।

Content added By
Promotion

Are you sure to start over?

Loading...