In AEM sometime we may have to load the third-party data e.g. country list, state list to drop-down lists, or other components from an external service call, the response will be static and changes occasionally over time.
To improve the performance the data can be cached in AEM and enable a JMX bean to clear the cache whenever the data is modified in the external system. This approach can be modified according to the requirement if the volume of the cache data is more then better to integrate with an external caching system like Ehcache.
Define the cache manager to put/get the cache objects
Define JMX bean to clear the cache objects
1. Define CacheManager to put/get objects into cache and methods to clear the cache through JMX bean
public class CacheManager {
private static java.util.HashMap<String,Object> cacheHashMap = new java.util.HashMap<String,Object>();
public static void putCache(Object object, String identifier)
{
cacheHashMap.put(identifier, object);
}
public static Object getCache(String identifier)
{
Object object = cacheHashMap.get(identifier);
return object;
}
public static String removeCacheWithKey(String identifier)
{
if(!cacheHashMap.containsKey(identifier))
{
return "Invalid Key..";
}
cacheHashMap.remove(identifier);
return "Removed cache with key "+identifier;
}
public static String removeCacheAll()
{
cacheHashMap.clear();
return "Removed cache";
}
}
2. Sample client to get/put an object into cache
public class CacheCleint {
public static void main(String[] args) {
String[] countrylist=new String[]{"USA","UK","AUS","IN"};
CacheManager.putCache(countrylist, "country-list");
String[] cachedCountrylist=(String[])CacheManager.getCache("country-list");
System.out.println(cachedCountrylist[1]);
}
}
3. Define JMX MBean to clear the cache
Define JMX MBean that will help to clear cache from OSGI JMX console or other JMX client.
import com.adobe.granite.jmx.annotation.Description;
import com.adobe.granite.jmx.annotation.Name;
@Description("Cache Manager JMX Bean")
public interface CacheManagerMBean {
@Description("Remove the cache with cache id - country-list,state-list")
String removeCacheWithKey(@Name("cacheKey") @Description("Valid cache key") String Cachekey);
@Description("Remove All Cache")
String removeCacheAll();
}
import javax.management.NotCompliantMBeanException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import com.adobe.granite.jmx.annotation.AnnotatedStandardMBean;
@Component(immediate = true)
@Property(name = "jmx.objectname", value = "com.packagegenerator.core:type=CacheManager")
@Service
public class CacheManagerMBeanImpl extends AnnotatedStandardMBean implements CacheManagerMBean {
public CacheManagerMBeanImpl() throws NotCompliantMBeanException {
super(CacheManagerMBean.class);
}
@Override
public String removeCacheWithKey(String cacheKey) {
return CacheManager.removeCacheWithKey(cacheKey);
}
@Override
public String removeCacheAll() {
return CacheManager.removeCacheAll();
}
}
The cache objects specified to a key or complete cache can be cleared through JMX client.
This will enable us to cache the static data from an external system into AEM and improve the performance, the cache can be cleared through JMX console whenever required.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.