Method Caching with Annoations using Implementation Logic as Key

In a previous post I created an annotation based caching system using AspectJ


I found a shortcoming where I would like to specify part of the key using business logic in the implementing class where the @Cacheable annotated function resides. This situation occurs when the key must be more than just the function name and parameters sent into the function. I need to be also able to specify part of the key using logic in the class. I created an interface and added a function getCacheKey()

 * Allows Caching System to get any specific cache key value that 
 * is only available at runtime.  This allows caching where 
 * implementation logic can be added to make a uniquely 
 * cached function
 * Implement this function where the Cacheable annotation 
 * is applied for implementation(logic) specific caching
 * @author David Reepmeyer
public interface ImplementationCacheKey {
	 * Gets a implementation specific cache key 
	 * @return
	public String getCacheKey();

Next, I then implemented this class where The annotations were

public class SomeServiceImpl1 
	implements SomeService, ImplementationCacheKey {
	public List<String> getTypes() {  
		// get types
	@Cacheable(subclass=true, arguements=true)
	public List<String> getTypes(Object someArg) {  
		// get types
	public List<SelectItem> getItems() {  
		// get items
	public List<SelectItem> saveItems(List<Items> items) {  
		// save items to database
	public MyService getMyService() {
		return myService;
	public String getCacheKey() {
		return getMyService().determineServiceType()

The getCacheKey function allows the logic to cache the key specifically to the service type. The service type in this example can only be determined at runtime AND can change at runtime.

I modify the CacheingAspect from the last post just a little. I need to modify the getKey function.

From CachingAspect

private String getKey(ProceedingJoinPoint pjp, 
	boolean subclass, boolean arguements){
	String fullClassname = getClassName(pjp, subclass);	
	String method = pjp.getSignature().getName();
	String key = fullClassname + "." + method;
	if (arguements)
		key = key + "-" + getArguementsString(pjp);
	if (pjp.getThis() instanceof ImplementationCacheKey) {
		ImplementationCacheKey k;
		key = key + "-" + k.getCacheKey();
	return key;

Post new comment

Subscribe to Syndicate