Uploaded image for project: 'Spring Modules'
  1. Spring Modules
  2. MOD-67

add the possibility to not cache null return values (add CachingDecisionStrategy to CachingModel)

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 0.3
    • Fix Version/s: 0.10
    • Component/s: CACHE
    • Labels:
      None
    • Environment:
      any

      Description

      I'm using the CVS version of Springmodules Cache. I'm pretty happy with it.

      I'd like to be able to specify that I don't want to cache null return values.

      Currently it's not possible to extend the "invoke" method of org.springmodules.cache.interceptor.caching.AbstractCachingInterceptor in a subclass since it's marked "final".

      It would be nice if the configuration would be in the CachingModel class. There should be a way to specify settings in a Properties object to enable loosely coupled configuration.

      To get it even more generic, there could be a callback interface (strategy) for deciding if the return value should be cached or not.

      Here's example code for getting the idea:

      import java.io.Serializable;

      import org.aopalliance.intercept.MethodInvocation;
      import org.springmodules.cache.CachingModel;

      public interface CachingDecisionStrategy {
      public boolean shouldCache(MethodInvocation methodInvocation, Object retval, Serializable key, CachingModel model);
      }

      import java.io.Serializable;
      import java.util.Properties;

      import org.aopalliance.intercept.MethodInvocation;
      import org.springmodules.cache.CachingModel;

      public class DontCacheNullsCachingDecisionStrategyImpl implements CachingDecisionStrategy {
      private static final String PROPERTY_NAME_DONT_CACHE_NULL_RETURN="dontCacheNullReturn";
      private static final String DEFAULT_VALUE="false";

      public boolean shouldCache(MethodInvocation methodInvocation, Object retval, Serializable key, CachingModel model) {
      Properties props=model.getProperties();
      if(retval==null && Boolean.getBoolean(props.getProperty(PROPERTY_NAME_DONT_CACHE_NULL_RETURN,DEFAULT_VALUE)))

      { return false; }

      else

      { return true; }

      }
      }

      CachingDecisionStrategy would be a property of CachingModel. It would get called in org.springmodules.cache.interceptor.caching.AbstractCachingInterceptor :

      starting from line 219 in revision 1.4 of AbstractCachingInterceptor.java:

      if (null == exceptionThrownByProceed) {
      CachingDecisionStrategy cachingDecisionStrategy=model.getCachingDecisionStrategy();
      if(cachingDecisionStrategy==null || cachingDecisionStrategy.shouldCache(methodInvocation, cachedObject, key, model)) {
      if (null == cachedObject)

      { cacheProviderFacade.putInCache(key, model, NULL_ENTRY); }

      else

      { cacheProviderFacade.putInCache(key, model, cachedObject); }

      // notify the listeners a new entry was stored in the cache.
      if (!ObjectUtils.isEmpty(cachingListeners)) {
      int listenerCount = cachingListeners.length;
      for (int i = 0; i < listenerCount; i++)

      { cachingListeners[i].onCaching(key, cachedObject, model); }

      }
      } else

      { // notifyNotCaching(methodInvocation, cachedObject, key, model); }

      } else

      { cacheProviderFacade.cancelCacheUpdate(key); throw exceptionThrownByProceed; }

        Attachments

          Activity

            People

            Assignee:
            yatesco Colin Yates
            Reporter:
            lhotari Lari Hotari
            Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated: