Commits

Anonymous committed cdf4905

Additional CacheFilter parameter and interface for providing cache groups based on the request.
Issue number: CACHE-195
Submitted by: Lars Torunski

Comments (0)

Files changed (3)

src/java/com/opensymphony/oscache/web/filter/CacheFilter.java

  * @author <a href="mailto:ltorunski@t-online.de">Lars Torunski</a>
  * @version $Revision$
  */
-public class CacheFilter implements Filter, ICacheKeyProvider {
+public class CacheFilter implements Filter, ICacheKeyProvider, ICacheGroupsProvider {
     // Header
     public static final String HEADER_LAST_MODIFIED = "Last-Modified";
     public static final String HEADER_CONTENT_TYPE = "Content-Type";
     private int fragment = FRAGMENT_AUTODETECT; // defines if this filter handles fragments of a page - default is auto detect
     private int time = 60 * 60; // time before cache should be refreshed - default one hour (in seconds)
     private int nocache = NOCACHE_OFF; // defines special no cache option for the requests - default is off
-    private ICacheKeyProvider cacheKeyProvider = this; // the provider of the cache key - default is the CacheFilter itselfs 
+    private ICacheKeyProvider cacheKeyProvider = this; // the provider of the cache key - default is the CacheFilter itselfs
+    private ICacheGroupsProvider cacheGroupsProvider = this; // the provider of the cache groups - default is the CacheFilter itselfs
 
     /**
      * Filter clean-up
 
                 // Only cache if the response is cacheable
                 if (isCacheable(cacheResponse)) {
-                    //Store as the cache content the result of the response
-                    cache.putInCache(key, cacheResponse.getContent(), expiresRefreshPolicy);
+                    // get the cache groups of the content
+                    String[] groups = cacheGroupsProvider.createCacheGroups(httpRequest, admin, cache);
+                    // Store as the cache content the result of the response
+                    cache.putInCache(key, cacheResponse.getContent(), groups, expiresRefreshPolicy, null);
                     updateSucceeded = true;
                 }
             } finally {
      * <li><b>ICacheKeyProvider</b> - Class implementing the interface <code>ICacheKeyProvider</code>.
      * A developer can implement a method which provides cache keys based on the request, 
      * the servlect cache administrator and cache.</li>
+     * <li><b>ICacheGroupsProvider</b> - Class implementing the interface <code>ICacheGroupsProvider</code>.
+     * A developer can implement a method which provides cache groups based on the request, 
+     * the servlect cache administrator and cache.</li>
      *
      * @param filterConfig The filter configuration
      */
                 Class clazz = Class.forName(className);
 
                 if (!ICacheKeyProvider.class.isAssignableFrom(clazz)) {
-                    log.error("Specified class '" + className + "' does not implement ICacheKeyProvider. Ignoring this listener.");
+                    log.error("Specified class '" + className + "' does not implement ICacheKeyProvider. Ignoring this provider.");
                 } else {
                     cacheKeyProvider = (ICacheKeyProvider) clazz.newInstance();
                 }
             } catch (IllegalAccessException e) {
                 log.error("Class '" + className + "' could not be instantiated because it is not public. Ignoring this cache key provider.", e);
             }
+        } catch (Exception e) {
+            log.info("Could not get init parameter 'ICacheKeyProvider', defaulting to " + this.getClass().getName() + ".");
+        }
+
+        // filter parameter ICacheGroupsProvider
+        try {
+            String className = config.getInitParameter("ICacheGroupsProvider");
             
+            try {
+                Class clazz = Class.forName(className);
+
+                if (!ICacheGroupsProvider.class.isAssignableFrom(clazz)) {
+                    log.error("Specified class '" + className + "' does not implement ICacheGroupsProvider. Ignoring this provider.");
+                } else {
+                    cacheGroupsProvider = (ICacheGroupsProvider) clazz.newInstance();
+                }
+            } catch (ClassNotFoundException e) {
+                log.error("Class '" + className + "' not found. Ignoring this cache key provider.", e);
+            } catch (InstantiationException e) {
+                log.error("Class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this cache groups provider.", e);
+            } catch (IllegalAccessException e) {
+                log.error("Class '" + className + "' could not be instantiated because it is not public. Ignoring this cache groups provider.", e);
+            }
         } catch (Exception e) {
             log.info("Could not get init parameter 'ICacheKeyProvider', defaulting to " + this.getClass().getName() + ".");
         }
     }
 
     /**
+     * @see com.opensymphony.oscache.web.filter.ICacheGroupsProvider#createCacheGroups(javax.servlet.http.HttpServletRequest, ServletCacheAdministrator, Cache)
+     */
+    public String[] createCacheGroups(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache) {
+        return null;
+    }
+
+    /**
      * Checks if the request is a fragment in a page.
      *
      * According to Java Servlet API 2.2 (8.2.1 Dispatching Requests, Included
         String acceptEncoding = request.getHeader(HEADER_ACCEPT_ENCODING);
         return  (acceptEncoding != null) && (acceptEncoding.indexOf("gzip") != -1);
     }
+
 }

src/java/com/opensymphony/oscache/web/filter/ICacheGroupsProvider.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.oscache.web.filter;
+
+import javax.servlet.http.HttpServletRequest;
+
+import com.opensymphony.oscache.base.Cache;
+import com.opensymphony.oscache.web.ServletCacheAdministrator;
+
+/**
+ * Provider interface for cache groups creation in CacheFilter. A developer can implement a method which provides
+ * cache groups based on the request, the servlet cache administrator and cache.
+ * 
+ * JIRA issue: http://jira.opensymphony.com/browse/CACHE-195
+ *
+ * @author <a href="mailto:ltorunski@t-online.de">Lars Torunski</a>
+ * @version $Revision$
+ */
+public interface ICacheGroupsProvider {
+
+    /**
+     * Creates the cache groups for the CacheFilter.
+     *
+     * @param httpRequest the http request.
+     * @param scAdmin the ServletCacheAdministrator of the cache
+     * @param cache the cache of the ServletCacheAdministrator
+     * @return the cache key
+     */
+    public String[] createCacheGroups(HttpServletRequest httpRequest, ServletCacheAdministrator scAdmin, Cache cache);
+
+}

src/java/com/opensymphony/oscache/web/filter/ICacheKeyProvider.java

 
 /**
  * Provider interface for cache key creation. A developer can implement a method which provides
- * cache keys based on the request, the servlect cache administrator and cache.
+ * cache keys based on the request, the servlet cache administrator and cache.
  * 
  * JIRA issue: http://jira.opensymphony.com/browse/CACHE-179
  *
  * @author <a href="mailto:ltorunski@t-online.de">Lars Torunski</a>
- * 
  * @version $Revision$
  */
 public interface ICacheKeyProvider {