Spring Cache Integration এর মাধ্যমে ORM Performance বৃদ্ধি

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

258

Spring Cache Integration স্প্রিং ওআরএম (Spring ORM) এর কার্যক্ষমতা বাড়ানোর জন্য একটি গুরুত্বপূর্ণ উপায়। এটি ডাটাবেস কোয়েরির ফলাফলকে ক্যাশে সংরক্ষণ করে পুনরায় কোয়েরি করার প্রয়োজনীয়তা কমায়, যা অ্যাপ্লিকেশনের গতিশীলতা উন্নত করে।


স্প্রিং ক্যাশ (Spring Cache) কী?

Spring Cache হলো একটি অ্যাবস্ট্রাকশন লেয়ার যা ডেটা বা কোয়েরির ফলাফলকে ইন-মেমরি ক্যাশে সংরক্ষণ করে। এটি অ্যাপ্লিকেশনের পারফরম্যান্স বাড়ায় এবং পুনরাবৃত্ত ডেটাবেস অ্যাক্সেস কমায়।


Spring Cache Integration এর গুরুত্বপূর্ণ বৈশিষ্ট্য

  1. ক্যাশ মেকানিজম: ডাটাবেসে একই কোয়েরি একাধিকবার করার পরিবর্তে ক্যাশে থেকে ডেটা ফেরত দেওয়া হয়।
  2. ক্যাশ স্টোরেজ: Spring বিভিন্ন ক্যাশ প্রদানকারী যেমন EhCache, Redis, Caffeine, বা SimpleCacheManager সমর্থন করে।
  3. অটোমেটেড কনফিগারেশন: Spring Boot Cache Starter ক্যাশ সেটআপ সহজ করে।

Spring Cache Integration এর ধাপসমূহ

১. Maven ডিপেনডেন্সি যোগ করা

Spring Boot ব্যবহার করলে Maven pom.xml এ নিম্নলিখিত ডিপেনডেন্সি যোগ করতে হবে:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

প্রয়োজন অনুযায়ী, নির্দিষ্ট ক্যাশ টেকনোলজির জন্য ডিপেনডেন্সি যোগ করুন (যেমন EhCache বা Redis)।


২. @EnableCaching ব্যবহার করে ক্যাশিং সক্রিয় করা

Spring Cache সক্রিয় করতে প্রধান ক্লাসে @EnableCaching অ্যানোটেশন ব্যবহার করতে হবে:

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableCaching
public class SpringOrmCacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringOrmCacheApplication.class, args);
    }
}

৩. ক্যাশিং যোগ করা @Cacheable অ্যানোটেশন ব্যবহার করে

কোনো মেথডে ক্যাশিং প্রয়োগ করতে @Cacheable অ্যানোটেশন ব্যবহার করা হয়। উদাহরণস্বরূপ:

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

@Service
public class UserService {

    @Cacheable("users")
    public User findUserById(Long id) {
        // Mock database call
        System.out.println("Fetching user from database...");
        return new User(id, "John Doe");
    }
}

ব্যাখ্যা:

  • @Cacheable("users"): মেথডের রিটার্ন ভ্যালুকে users নামক ক্যাশে সংরক্ষণ করে।
  • প্রথমবার ডেটাবেস থেকে ডেটা নিয়ে আসা হবে এবং পরবর্তী সময়ে ক্যাশ থেকে তা রিটার্ন করা হবে।

৪. ক্যাশ ম্যানেজমেন্ট কাস্টমাইজেশন (অপশনাল)

Spring Boot এর ডিফল্ট SimpleCacheManager ব্যবহার করে। তবে, উন্নত পারফরম্যান্সের জন্য EhCache বা Redis ইন্টিগ্রেট করা যেতে পারে।

EhCache কনফিগারেশন উদাহরণ: ehcache.xml ফাইল তৈরি করুন:

<ehcache>
    <cache name="users"
           maxEntriesLocalHeap="1000"
           timeToLiveSeconds="3600">
    </cache>
</ehcache>

Spring Configuration ক্লাসে EhCache যোগ করুন:

import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManagerFactoryBean().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
        EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
        factory.setConfigLocation(new ClassPathResource("ehcache.xml"));
        factory.setShared(true);
        return factory;
    }
}

Hibernate Second-Level Cache

Spring Cache Integration এর পাশাপাশি Hibernate এর Second-Level Cache ব্যবহার করা ORM পারফরম্যান্স বাড়াতে পারে।

Second-Level Cache সক্রিয় করার জন্য:

application.properties এ কনফিগারেশন যোগ করুন:

spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=true

Hibernate এ Second-Level Cache ক্যাশ প্যারামিটার সংরক্ষণ করে, যা স্প্রিং ক্যাশের সাথে সম্পূর্ণরূপে একীভূত হয়।


সারাংশ

Spring Cache Integration এবং Hibernate Second-Level Cache একত্রে ব্যবহার করলে স্প্রিং ওআরএম এর কার্যক্ষমতা উল্লেখযোগ্যভাবে বৃদ্ধি পায়। ক্যাশিং পদ্ধতিতে ইন-মেমরি ডেটা স্টোরেজ ব্যবহার করে ডেটাবেস অ্যাক্সেসের সময় এবং খরচ কমানো হয়। Redis বা EhCache এর মতো উন্নত ক্যাশ সিস্টেম ব্যবহার করলে আরও উন্নত পারফরম্যান্স নিশ্চিত করা যায়।

Content added By
Promotion

Are you sure to start over?

Loading...