You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2005/12/22 20:46:26 UTC

svn commit: r358609 - in /incubator/roller/trunk/src/org/roller/presentation: cache/ filters/

Author: agilliland
Date: Thu Dec 22 11:46:22 2005
New Revision: 358609

URL: http://svn.apache.org/viewcvs?rev=358609&view=rev
Log:
a bit of cache property cleanup.  the process is now more generic so that props can be defined in the config file and easily passed along to a CacheFactory for use.


Modified:
    incubator/roller/trunk/src/org/roller/presentation/cache/CacheManager.java
    incubator/roller/trunk/src/org/roller/presentation/filters/FeedCacheFilter.java
    incubator/roller/trunk/src/org/roller/presentation/filters/IfModifiedFeedCacheFilter.java
    incubator/roller/trunk/src/org/roller/presentation/filters/MainPageCacheFilter.java
    incubator/roller/trunk/src/org/roller/presentation/filters/PlanetCacheFilter.java
    incubator/roller/trunk/src/org/roller/presentation/filters/WeblogPageCacheFilter.java

Modified: incubator/roller/trunk/src/org/roller/presentation/cache/CacheManager.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/cache/CacheManager.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/cache/CacheManager.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/cache/CacheManager.java Thu Dec 22 11:46:22 2005
@@ -56,7 +56,7 @@
     
     static {
         // lookup what cache factory we want to use
-        String classname = RollerConfig.getProperty("cache.factory.classname");
+        String classname = RollerConfig.getProperty("cache.defaultFactory");
         
         // use reflection to instantiate our factory class
         try {
@@ -135,9 +135,9 @@
         
         Cache cache = null;
         
-        if(properties != null && properties.containsKey("cache.factory")) {
+        if(properties != null && properties.containsKey("factory")) {
             // someone wants a custom cache instance
-            String classname = (String) properties.get("cache.factory");
+            String classname = (String) properties.get("factory");
             
             try {
                 // use reflection to instantiate the factory class

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/FeedCacheFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/FeedCacheFilter.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/FeedCacheFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/FeedCacheFilter.java Thu Dec 22 11:46:22 2005
@@ -8,6 +8,7 @@
 
 import java.io.IOException;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -54,7 +55,11 @@
     
     private static Log mLogger = LogFactory.getLog(FeedCacheFilter.class);
     
-    private Cache mFeedCache = null;
+    // a unique identifier for this cache, this is used as the prefix for
+    // roller config properties that apply to this cache
+    private static final String CACHE_ID = "cache.feed";
+    
+    private Cache mCache = null;
     
     // for metrics
     private double hits = 0;
@@ -89,7 +94,7 @@
         String key = "feedCache:"+this.generateKey(feedRequest);
         
         try {
-            ResponseContent respContent = (ResponseContent) this.mFeedCache.get(key);
+            ResponseContent respContent = (ResponseContent) this.mCache.get(key);
             if (respContent == null) {
                 
                 mLogger.debug("MISS "+key);
@@ -106,7 +111,7 @@
                 if (request.getAttribute("DisplayException") == null) {
                     ResponseContent rc = cacheResponse.getContent();
                     
-                    this.mFeedCache.put(key, rc);
+                    this.mCache.put(key, rc);
                 } else {
                     // it is expected that whoever caught this display exception
                     // is the one who reported it to the logs
@@ -213,8 +218,8 @@
         //       over the entire cache key set
         String key = null;
         
-        synchronized(mFeedCache) {
-            Iterator allKeys = this.mFeedCache.keySet().iterator();
+        synchronized(mCache) {
+            Iterator allKeys = this.mCache.keySet().iterator();
             while(allKeys.hasNext()) {
                 key = (String) allKeys.next();
                 
@@ -228,7 +233,7 @@
             }
         }
         
-        this.mFeedCache.remove(removeSet);
+        this.mCache.remove(removeSet);
         this.purges += removeSet.size();
     }
     
@@ -286,7 +291,7 @@
      */
     public void clear() {
         mLogger.info("Clearing cache");
-        this.mFeedCache.clear();
+        this.mCache.clear();
         this.startTime = new Date();
         this.hits = 0;
         this.misses = 0;
@@ -305,7 +310,7 @@
     public Map getStats() {
         
         Map stats = new HashMap();
-        stats.put("cacheType", this.mFeedCache.getClass().getName());
+        stats.put("cacheType", this.mCache.getClass().getName());
         stats.put("startTime", this.startTime);
         stats.put("hits", new Double(this.hits));
         stats.put("misses", new Double(this.misses));
@@ -335,36 +340,22 @@
         
         mLogger.info("Initializing feed cache");
         
-        String factory = RollerConfig.getProperty("cache.feed.factory");
-        String size = RollerConfig.getProperty("cache.feed.size");
-        String timeout = RollerConfig.getProperty("cache.feed.timeout");
-        
-        int cacheSize = 100;
-        try {
-            cacheSize = Integer.parseInt(size);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache size ["+size+"], using default");
-        }
-        
-        long cacheTimeout = 30 * 60;
-        try {
-            cacheTimeout = Long.parseLong(timeout);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache timeout ["+timeout+
-                    "], using default");
+        Map cacheProps = new HashMap();
+        Enumeration allProps = RollerConfig.keys();
+        String prop = null;
+        while(allProps.hasMoreElements()) {
+            prop = (String) allProps.nextElement();
+            
+            // we are only interested in props for this cache
+            if(prop.startsWith(CACHE_ID+".")) {
+                cacheProps.put(prop.substring(CACHE_ID.length()+1), 
+                        RollerConfig.getProperty(prop));
+            }
         }
         
+        mLogger.info(cacheProps);
         
-        Map props = new HashMap();
-        props.put("timeout", ""+cacheTimeout);
-        props.put("size", ""+cacheSize);
-        
-        if(factory != null && factory.trim().length() > 0)
-            props.put("cache.factory", factory);
-        
-        mLogger.info(props);
-        
-        mFeedCache = CacheManager.constructCache(this, props);
+        mCache = CacheManager.constructCache(this, cacheProps);
     }
     
 }

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/IfModifiedFeedCacheFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/IfModifiedFeedCacheFilter.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/IfModifiedFeedCacheFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/IfModifiedFeedCacheFilter.java Thu Dec 22 11:46:22 2005
@@ -9,6 +9,7 @@
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -59,6 +60,10 @@
     private static Log mLogger = 
             LogFactory.getLog(IfModifiedFeedCacheFilter.class);
     
+    // a unique identifier for this cache, this is used as the prefix for
+    // roller config properties that apply to this cache
+    private static final String CACHE_ID = "cache.ifmodified.feed";
+    
     private Cache mCache = null;
     
     SimpleDateFormat dateFormatter =
@@ -323,37 +328,24 @@
      */
     public void init(FilterConfig filterConfig) {
         
-        mLogger.info("Initializing if-modified cache");
-        
-        String factory = RollerConfig.getProperty("cache.feed.ifmodified.factory");
-        String size = RollerConfig.getProperty("cache.feed.ifmodified.size");
-        String timeout = RollerConfig.getProperty("cache.feed.ifmodified.timeout");
-        
-        int cacheSize = 100;
-        try {
-            cacheSize = Integer.parseInt(size);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache size ["+size+"], using default");
-        }
+        mLogger.info("Initializing if-modified feed cache");
         
-        long cacheTimeout = 30 * 60;
-        try {
-            cacheTimeout = Long.parseLong(timeout);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache timeout ["+timeout+"], using default");
+        Map cacheProps = new HashMap();
+        Enumeration allProps = RollerConfig.keys();
+        String prop = null;
+        while(allProps.hasMoreElements()) {
+            prop = (String) allProps.nextElement();
+            
+            // we are only interested in props for this cache
+            if(prop.startsWith(CACHE_ID+".")) {
+                cacheProps.put(prop.substring(CACHE_ID.length()+1), 
+                        RollerConfig.getProperty(prop));
+            }
         }
         
+        mLogger.info(cacheProps);
         
-        Map props = new HashMap();
-        props.put("timeout", ""+cacheTimeout);
-        props.put("size", ""+cacheSize);
-        
-        if(factory != null && factory.trim().length() > 0)
-            props.put("cache.factory", factory);
-        
-        mLogger.info(props);
-        
-        mCache = CacheManager.constructCache(this, props);
+        mCache = CacheManager.constructCache(this, cacheProps);
     }
     
 }

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/MainPageCacheFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/MainPageCacheFilter.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/MainPageCacheFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/MainPageCacheFilter.java Thu Dec 22 11:46:22 2005
@@ -8,6 +8,7 @@
 import java.io.IOException;
 import java.security.Principal;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
@@ -50,8 +51,12 @@
     
     private static Log mLogger = LogFactory.getLog(MainPageCacheFilter.class);
     
+    // a unique identifier for this cache, this is used as the prefix for
+    // roller config properties that apply to this cache
+    private static final String CACHE_ID = "cache.mainpage";
+    
     private boolean excludeOwnerPages = false;
-    private Cache mPageCache = null;
+    private Cache mCache = null;
     
     // for metrics
     private double hits = 0;
@@ -106,7 +111,7 @@
         try {
             ResponseContent respContent = null;
             if(!this.excludeOwnerPages || prince == null) {
-                respContent = (ResponseContent) this.mPageCache.get(key);
+                respContent = (ResponseContent) this.mCache.get(key);
             }
             
             if(respContent == null) {
@@ -127,7 +132,7 @@
                     
                     // only cache if this is not a logged in user?
                     if(!this.excludeOwnerPages || prince == null) {
-                        this.mPageCache.put(key, rc);
+                        this.mCache.put(key, rc);
                     } else {
                         mLogger.debug("SKIPPED "+key);
                         this.skips++;
@@ -172,7 +177,7 @@
      * A weblog entry has changed.
      */
     public void invalidate(WeblogEntryData entry) {
-        this.mPageCache.clear();
+        this.mCache.clear();
     }
     
     
@@ -180,7 +185,7 @@
      * A weblog has changed.
      */
     public void invalidate(WebsiteData website) {
-        this.mPageCache.clear();
+        this.mCache.clear();
     }
     
     
@@ -245,7 +250,7 @@
      */
     public void clear() {
         mLogger.info("Clearing cache");
-        this.mPageCache.clear();
+        this.mCache.clear();
         this.startTime = new Date();
         this.hits = 0;
         this.misses = 0;
@@ -256,7 +261,7 @@
     public Map getStats() {
         
         Map stats = new HashMap();
-        stats.put("cacheType", this.mPageCache.getClass().getName());
+        stats.put("cacheType", this.mCache.getClass().getName());
         stats.put("startTime", this.startTime);
         stats.put("hits", new Double(this.hits));
         stats.put("misses", new Double(this.misses));
@@ -285,39 +290,26 @@
     public void init(FilterConfig filterConfig) {
         
         mLogger.info("Initializing main page cache");
-        
-        String factory = RollerConfig.getProperty("cache.mainpage.factory");
-        String size = RollerConfig.getProperty("cache.mainpage.size");
-        String timeout = RollerConfig.getProperty("cache.mainpage.timeout");
+
         this.excludeOwnerPages = 
                 RollerConfig.getBooleanProperty("cache.mainpage.excludeOwnerEditPages");
         
-        int cacheSize = 20;
-        try {
-            cacheSize = Integer.parseInt(size);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache size ["+size+"], using default");
-        }
-        
-        long cacheTimeout = 30 * 60;
-        try {
-            cacheTimeout = Long.parseLong(timeout);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache timeout ["+timeout+
-                    "], using default");
+        Map cacheProps = new HashMap();
+        Enumeration allProps = RollerConfig.keys();
+        String prop = null;
+        while(allProps.hasMoreElements()) {
+            prop = (String) allProps.nextElement();
+            
+            // we are only interested in props for this cache
+            if(prop.startsWith(CACHE_ID+".")) {
+                cacheProps.put(prop.substring(CACHE_ID.length()+1), 
+                        RollerConfig.getProperty(prop));
+            }
         }
         
+        mLogger.info(cacheProps);
         
-        Map props = new HashMap();
-        props.put("timeout", ""+cacheTimeout);
-        props.put("size", ""+cacheSize);
-        
-        if(factory != null && factory.trim().length() > 0)
-            props.put("cache.factory", factory);
-        
-        mLogger.info(props);
-        
-        mPageCache = CacheManager.constructCache(this, props);
+        mCache = CacheManager.constructCache(this, cacheProps);
     }
     
 }

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/PlanetCacheFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/PlanetCacheFilter.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/PlanetCacheFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/PlanetCacheFilter.java Thu Dec 22 11:46:22 2005
@@ -7,10 +7,9 @@
 package org.roller.presentation.filters;
 
 import java.io.IOException;
-import java.security.Principal;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -32,7 +31,6 @@
 import org.roller.pojos.WeblogEntryData;
 import org.roller.pojos.WeblogTemplate;
 import org.roller.pojos.WebsiteData;
-import org.roller.presentation.LanguageUtil;
 import org.roller.presentation.PlanetRequest;
 import org.roller.presentation.cache.Cache;
 import org.roller.presentation.cache.CacheHandler;
@@ -40,7 +38,6 @@
 import org.roller.presentation.util.CacheHttpServletResponseWrapper;
 import org.roller.presentation.util.ResponseContent;
 
-
 /**
  * A cache filter for Planet Roller items ... /planet.do, /planetrss
  *
@@ -52,6 +49,10 @@
     
     private static Log mLogger = LogFactory.getLog(PlanetCacheFilter.class);
     
+    // a unique identifier for this cache, this is used as the prefix for
+    // roller config properties that apply to this cache
+    private static final String CACHE_ID = "cache.planet";
+    
     private boolean excludeOwnerPages = false;
     private Cache mCache = null;
     
@@ -313,38 +314,25 @@
         
         mLogger.info("Initializing planet cache");
         
-        String factory = RollerConfig.getProperty("cache.planet.factory");
-        String size = RollerConfig.getProperty("cache.planet.size");
-        String timeout = RollerConfig.getProperty("cache.planet.timeout");
         this.excludeOwnerPages = 
                 RollerConfig.getBooleanProperty("cache.planet.excludeOwnerEditPages");
         
-        int cacheSize = 20;
-        try {
-            cacheSize = Integer.parseInt(size);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache size ["+size+"], using default");
-        }
-        
-        long cacheTimeout = 30 * 60;
-        try {
-            cacheTimeout = Long.parseLong(timeout);
-        } catch (Exception e) {
-            mLogger.warn("Invalid cache timeout ["+timeout+
-                    "], using default");
+        Map cacheProps = new HashMap();
+        Enumeration allProps = RollerConfig.keys();
+        String prop = null;
+        while(allProps.hasMoreElements()) {
+            prop = (String) allProps.nextElement();
+            
+            // we are only interested in props for this cache
+            if(prop.startsWith(CACHE_ID+".")) {
+                cacheProps.put(prop.substring(CACHE_ID.length()+1), 
+                        RollerConfig.getProperty(prop));
+            }
         }
         
+        mLogger.info(cacheProps);
         
-        Map props = new HashMap();
-        props.put("timeout", ""+cacheTimeout);
-        props.put("size", ""+cacheSize);
-        
-        if(factory != null && factory.trim().length() > 0)
-            props.put("cache.factory", factory);
-        
-        mLogger.info(props);
-        
-        mCache = CacheManager.constructCache(this, props);
+        mCache = CacheManager.constructCache(this, cacheProps);
     }
     
 }

Modified: incubator/roller/trunk/src/org/roller/presentation/filters/WeblogPageCacheFilter.java
URL: http://svn.apache.org/viewcvs/incubator/roller/trunk/src/org/roller/presentation/filters/WeblogPageCacheFilter.java?rev=358609&r1=358608&r2=358609&view=diff
==============================================================================
--- incubator/roller/trunk/src/org/roller/presentation/filters/WeblogPageCacheFilter.java (original)
+++ incubator/roller/trunk/src/org/roller/presentation/filters/WeblogPageCacheFilter.java Thu Dec 22 11:46:22 2005
@@ -7,6 +7,7 @@
 
 import java.io.IOException;
 import java.util.Date;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -52,8 +53,12 @@
     private static Log mLogger =
             LogFactory.getFactory().getInstance(WeblogPageCacheFilter.class);
     
+    // a unique identifier for this cache, this is used as the prefix for
+    // roller config properties that apply to this cache
+    private static final String CACHE_ID = "cache.weblogpage";
+    
     private boolean excludeOwnerPages = false;
-    private Cache mPageCache = null;
+    private Cache mCache = null;
     
     // for metrics
     private double hits = 0;
@@ -89,7 +94,7 @@
         try {
             ResponseContent respContent = null;
             if(!this.excludeOwnerPages || !pageRequest.isLoggedIn()) {
-                respContent = (ResponseContent) this.mPageCache.get(key);
+                respContent = (ResponseContent) this.mCache.get(key);
             }
             
             if (respContent == null) {
@@ -116,7 +121,7 @@
                                 "Page size < 100 bytes: " + key 
                                 + ": " + request.getRequestURL());
                         }
-                        this.mPageCache.put(key, rc);
+                        this.mCache.put(key, rc);
                     } else {
                         mLogger.debug("SKIPPED "+key);
                         this.skips++;
@@ -238,8 +243,8 @@
         //       over the entire cache key set
         String key = null;
         
-        synchronized(mPageCache) {
-            Iterator allKeys = this.mPageCache.keySet().iterator();
+        synchronized(mCache) {
+            Iterator allKeys = this.mCache.keySet().iterator();
             while(allKeys.hasNext()) {
                 key = (String) allKeys.next();
                 if(key.startsWith(keyBase+"/main")) {
@@ -254,7 +259,7 @@
             }
         }
 
-        this.mPageCache.remove(removeSet);
+        this.mCache.remove(removeSet);
         this.purges += removeSet.size();
     }
     
@@ -275,8 +280,8 @@
         //       over the entire cache key set
         String key = null;
         
-        synchronized(mPageCache) {
-            Iterator allKeys = this.mPageCache.keySet().iterator();
+        synchronized(mCache) {
+            Iterator allKeys = this.mCache.keySet().iterator();
             while(allKeys.hasNext()) {
                 key = (String) allKeys.next();
                 
@@ -286,7 +291,7 @@
             }
         }
         
-        this.mPageCache.remove(removeSet);
+        this.mCache.remove(removeSet);
         this.purges += removeSet.size();
     }
     
@@ -354,7 +359,7 @@
      */
     public void clear() {
         mLogger.info("Clearing cache");
-        this.mPageCache.clear();
+        this.mCache.clear();
         this.startTime = new Date();
         this.hits = 0;
         this.misses = 0;
@@ -366,7 +371,7 @@
     public Map getStats() {
         
         Map stats = new HashMap();
-        stats.put("cacheType", this.mPageCache.getClass().getName());
+        stats.put("cacheType", this.mCache.getClass().getName());
         stats.put("startTime", this.startTime);
         stats.put("hits", new Double(this.hits));
         stats.put("misses", new Double(this.misses));
@@ -396,39 +401,26 @@
     public void init(FilterConfig filterConfig) {
         
         mLogger.info("Initializing weblog page cache");
-        
-        String factory = RollerConfig.getProperty("cache.weblogpage.factory");
-        String size = RollerConfig.getProperty("cache.weblogpage.size");
-        String timeout = RollerConfig.getProperty("cache.weblogpage.timeout");
+
         this.excludeOwnerPages = 
                 RollerConfig.getBooleanProperty("cache.weblogpage.excludeOwnerEditPages");
         
-        int cacheSize = 100;
-        try {
-            cacheSize = Integer.parseInt(size);
-        } catch (Exception e) {
-            mLogger.warn("Invalid page cache size ["+size+"], using default");
-        }
-        
-        long cacheTimeout = 30 * 60;
-        try {
-            cacheTimeout = Long.parseLong(timeout);
-        } catch (Exception e) {
-            mLogger.warn("Invalid page cache timeout ["+timeout+
-                    "], using default");
+        Map cacheProps = new HashMap();
+        Enumeration allProps = RollerConfig.keys();
+        String prop = null;
+        while(allProps.hasMoreElements()) {
+            prop = (String) allProps.nextElement();
+            
+            // we are only interested in props for this cache
+            if(prop.startsWith(CACHE_ID+".")) {
+                cacheProps.put(prop.substring(CACHE_ID.length()+1), 
+                        RollerConfig.getProperty(prop));
+            }
         }
         
+        mLogger.info(cacheProps);
         
-        Map props = new HashMap();
-        props.put("timeout", ""+cacheTimeout);
-        props.put("size", ""+cacheSize);
-        
-        if(factory != null && factory.trim().length() > 0)
-            props.put("cache.factory", factory);
-        
-        mLogger.info(props);
-        
-        mPageCache = CacheManager.constructCache(this, props);
+        mCache = CacheManager.constructCache(this, cacheProps);
     }
     
 }