Search code examples
cachingehcacheehcache-3

Where does the filter for Ehcache 3 simple web page caching call the cache?


I am trying to cache a simple web page in Ehcache. Thanks to some help from another SO post I discovered that I need to implement my own filter based on Ehcache 2 code. When I look at the filter I don't understand it. Where does it ever call the cache to return a value? Here is my implementation (quite possibly wrong):

package com.sentiment360.pulse.cache;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.Element;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import static org.ehcache.config.builders.CacheManagerBuilder.newCacheManager;
import org.ehcache.core.Ehcache;
import org.ehcache.event.CacheEvent;
import org.ehcache.event.CacheEventListener;
import org.ehcache.xml.XmlConfiguration;

import javax.servlet.http.HttpServletRequest;

public class SimplePageCachingFilter implements CachingFilter {
     public static final String DEFAULT_CACHE_NAME = "SimplePageCachingFilter";
     private Logger LOG = Logger.getLogger(this.getClass().getName());
     private String cacheName="basicCache";

    protected String getCacheName() {
         if (cacheName != null && cacheName.length() > 0) {
              LOG.log(Level.INFO,"Using configured cacheName of {}.", cacheName);
              return cacheName;
          } else {
               LOG.log(Level.INFO,"No cacheName configured. Using default of {}.", DEFAULT_CACHE_NAME);
               return DEFAULT_CACHE_NAME;
           }
    }

    protected CacheManager getCacheManager() {
      return CacheManager.getInstance();
    }

    protected String calculateKey(HttpServletRequest httpRequest) {
      StringBuffer stringBuffer = new StringBuffer();
      stringBuffer.append(httpRequest.getMethod()).append(httpRequest.getRequestURI()).append(httpRequest.getQueryString());
      String key = stringBuffer.toString();
      return key;
    }
}

Solution

  • See in the super class. But you do implements CachingFilter ?! Where is that interface? It does look like you were trying to "copy" the previous Ehcache's SimplePageCachingFilter, right? You would also need to port that abstract super class (and maybe read a little about javax.servlet.Filter, in case these aren't entirely clear...)

    Now, you may also want to ping the dev team on the Ehcache Dev Google group about this. They should be able to provide pointers and then help with the implementation. Looks like a good idea for a future pull request! :)