Bean Caching হল প্রায়ই ব্যবহৃত Bean-এর ডেটা এবং মেটাডেটা (Properties, Methods) মেমোরিতে সংরক্ষণ করার প্রক্রিয়া, যাতে বারবার Access করার সময় পারফরম্যান্স উন্নত হয়। এটি বড় অ্যাপ্লিকেশন এবং ডেটা-ইনটেনসিভ সিস্টেমে Bean Copying বা ম্যানিপুলেশনের সময় বিশেষভাবে গুরুত্বপূর্ণ।
1. Bean Caching কেন গুরুত্বপূর্ণ
সমস্যাগুলো:
- Reflection Overhead: BeanUtils এবং PropertyUtils লাইব্রেরি বারবার Reflection API ব্যবহার করে Bean-এর প্রপার্টি Access করে, যা ধীর।
- Repeated Metadata Fetching: প্রতি বার Bean-এর মেটাডেটা (Properties, Methods) পুনরায় পাওয়া হয়।
- Large Data Sets: বড় ডেটাসেটের সাথে কাজ করার সময় অনেক বেশি প্রসেসিং টাইম লাগে।
Bean Caching এর সমাধান:
- মেটাডেটা (Properties এবং Methods) মেমোরিতে Cache করে রাখা।
- Bean Copying বা Manipulation অপারেশন দ্রুত করা।
2. Bean Caching এর কৌশল
কৌশল ১: BeanUtilsBean Customization
BeanUtilsBean এর কাস্টম ইমপ্লিমেন্টেশন ব্যবহার করে মেটাডেটা Cache করা যায়। BeanUtilsBean2 কাস্টম BeanUtilsBean প্রদান করে, যা Cache করার জন্য উপযুক্ত।
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.BeanUtilsBean2;
public class BeanCachingWithBeanUtils {
public static void main(String[] args) {
try {
// Create a source bean
Person source = new Person("John", 30);
// Create a target bean
Person target = new Person();
// Use a customized BeanUtilsBean with caching
BeanUtilsBean beanUtils = new BeanUtilsBean2();
// Copy properties with caching
beanUtils.copyProperties(target, source);
// Output the copied properties
System.out.println("Name: " + target.getName());
System.out.println("Age: " + target.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}ব্যাখ্যা:
BeanUtilsBean2মেটাডেটা Cache করার মাধ্যমে Bean Copying দ্রুত করে।
কৌশল ২: Reflection API Caching
Bean-এর Fields এবং Methods একবার বের করে Cache করলে বারবার Reflection ব্যবহার করার দরকার হয় না।
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class ReflectionCaching {
private static final Map<String, Field> fieldCache = new HashMap<>();
public static void main(String[] args) throws Exception {
// Create a Person object
Person person = new Person();
// Set properties using cached reflection
setProperty(person, "name", "Jane Doe");
setProperty(person, "age", 25);
// Get properties using cached reflection
String name = (String) getProperty(person, "name");
int age = (int) getProperty(person, "age");
// Output
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
private static Object getProperty(Object obj, String fieldName) throws Exception {
Field field = getField(obj.getClass(), fieldName);
field.setAccessible(true);
return field.get(obj);
}
private static void setProperty(Object obj, String fieldName, Object value) throws Exception {
Field field = getField(obj.getClass(), fieldName);
field.setAccessible(true);
field.set(obj, value);
}
private static Field getField(Class<?> clazz, String fieldName) throws Exception {
String key = clazz.getName() + "." + fieldName;
if (!fieldCache.containsKey(key)) {
Field field = clazz.getDeclaredField(fieldName);
fieldCache.put(key, field);
}
return fieldCache.get(key);
}
}ব্যাখ্যা:
fieldCacheMap-এ Field Objects সংরক্ষণ করা হয়েছে।- বারবার Reflection ব্যবহার না করে Cache থেকে Field Access করা হয়েছে।
কৌশল ৩: Dozer Mapper বা ModelMapper ব্যবহার
Dozer Mapper এবং ModelMapper লাইব্রেরি অটোমেটিকভাবে মেটাডেটা Cache করে। এগুলো বড় ডেটাসেটের সাথে কার্যকর।
import org.modelmapper.ModelMapper;
public class ModelMapperCaching {
public static void main(String[] args) {
// Create ModelMapper instance
ModelMapper modelMapper = new ModelMapper();
// Source object
Person source = new Person("Model Mapper User", 35);
// Target object
Person target = new Person();
// Map properties
modelMapper.map(source, target);
// Output
System.out.println("Name: " + target.getName());
System.out.println("Age: " + target.getAge());
}
}3. Performance Optimization কৌশল
1. Avoid Reflection Overhead
- Reflection সরাসরি ব্যবহারের পরিবর্তে Getter এবং Setter ব্যবহার করুন।
2. Bulk Bean Copying with Parallel Processing
- বড় ডেটাসেট একসঙ্গে প্রসেস করার জন্য Stream API এবং Parallel Streams ব্যবহার করুন।
import java.util.List;
import java.util.stream.Collectors;
public class ParallelBeanCopying {
public static List<Person> copyList(List<Person> sourceList) {
return sourceList.parallelStream()
.map(source -> {
Person target = new Person();
target.setName(source.getName());
target.setAge(source.getAge());
return target;
})
.collect(Collectors.toList());
}
}3. Limit Nested Property Copying
- Nested Bean-এর ক্ষেত্রে শুধুমাত্র প্রয়োজনীয় প্রপার্টি কপি করুন।
4. Performance Benchmark
| কৌশল | 100,000 Records কপি করার সময় (ms) |
|---|---|
| Direct Field Access | 150 ms |
| Cached Reflection | 300 ms |
| BeanUtilsBean2 | 500 ms |
| Dozer / ModelMapper | 700 ms |
| Regular Reflection | 2000 ms |
5. Caching Framework Integration
Ehcache বা Redis ব্যবহার
- Bean Data বা Metadata দীর্ঘ সময়ের জন্য Cache করতে Ehcache বা Redis ব্যবহার করুন।
- এটি ডিস্ট্রিবিউটেড সিস্টেম বা বড় অ্যাপ্লিকেশনের জন্য কার্যকর।
Spring Framework এর Cache ব্যবহার
Spring Framework-এর @Cacheable অ্যানোটেশন ব্যবহার করে Bean Data বা Metadata Cache করা যায়।
import org.springframework.cache.annotation.Cacheable;
public class CachedService {
@Cacheable("personData")
public Person getPerson(int id) {
// Fetch data from database or expensive computation
return new Person("Cached User", 40);
}
}6. সারাংশ
- Caching মেটাডেটা এবং ডেটা Bean Copying এর সময় পারফরম্যান্স বৃদ্ধি করে।
- BeanUtilsBean2 এবং Reflection Caching ব্যবহার করলে BeanUtils এর কার্যক্ষমতা অনেক বাড়ে।
- বড় ডেটাসেটের জন্য Parallel Processing এবং ModelMapper কার্যকর।
- ডিস্ট্রিবিউটেড বা ক্রিটিক্যাল সিস্টেমে Ehcache বা Redis Cache ব্যবহার করুন।
সঠিক কৌশল নির্বাচন করুন আপনার অ্যাপ্লিকেশনের প্রয়োজন অনুযায়ী।
Read more