Monday, January 15, 2018

Developing a caching service to cache third part elements to improve performance in Adobe Experience Manage(AEM)

Developing a caching service to cache third part elements to improve performance in Adobe Experience Manage(AEM) 


This post will explain the approach to cache third party elements to improve performance.
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 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 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 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 to cache the static data from a external system into AEM and improve the performance, the cache can be cleared through JMX console whenever required.

jmx-bean-cache-clear

jmx-bean-cache-clear

jmx-bean-cache-clear

jmx-bean-cache-clear

1 comment:

  1. Hi,

    Nice article. I am using AEM 6.3.2.0 and used JMX based on above approach.
    It seems, @Component is deperecated and replaced with org.osgi.service.component.annotations.Component.
    I couldn't see bean in the JMX console. I am not sure how to clear my cache.
    Let me know your thoughts.

    ReplyDelete