Author: jalkanen
Date: Sun Jul 26 09:24:21 2009
New Revision: 797881

URL: http://svn.apache.org/viewvc?rev=797881&view=rev
Log:
Switch to EhCache 1.6.0.  Removed OSCache.  Documented used Caches.

Added:
    incubator/jspwiki/trunk/doc/README - Caches.txt
    incubator/jspwiki/trunk/src/WebContent/WEB-INF/lib/ehcache-1.6.0.jar   
(with props)
Removed:
    incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/oscache.properties
    incubator/jspwiki/trunk/src/WebContent/WEB-INF/lib/oscache.jar
Modified:
    incubator/jspwiki/trunk/ChangeLog
    incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/priha.properties
    incubator/jspwiki/trunk/src/WebContent/rss.jsp
    incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java
    
incubator/jspwiki/trunk/src/java/org/apache/wiki/attachment/AttachmentManager.java
    
incubator/jspwiki/trunk/src/java/org/apache/wiki/render/RenderingManager.java

Modified: incubator/jspwiki/trunk/ChangeLog
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/ChangeLog?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- incubator/jspwiki/trunk/ChangeLog (original)
+++ incubator/jspwiki/trunk/ChangeLog Sun Jul 26 09:24:21 2009
@@ -1,5 +1,13 @@
 2009-07-23 Janne Jalkanen <[email protected]>
 
+        * 3.0.0-svn-137
+        
+        * Switched from OSCache to EhCache to provide better performance
+        (and remove one extra JAR from the distro).  RenderingManagerTest
+        performance goes up 50%...
+
+2009-07-23 Janne Jalkanen <[email protected]>
+
         * 3.0.0-svn-136
         
         * Improved LoggerFactory not to fail horribly when the SLF4J

Added: incubator/jspwiki/trunk/doc/README - Caches.txt
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/doc/README%20-%20Caches.txt?rev=797881&view=auto
==============================================================================
--- incubator/jspwiki/trunk/doc/README - Caches.txt (added)
+++ incubator/jspwiki/trunk/doc/README - Caches.txt Sun Jul 26 09:24:21 2009
@@ -0,0 +1,60 @@
+
+As of 3.0, JSPWiki uses EhCache (http://ehcache.sourceforge.net/) as the 
primary
+caching solution.  The 2.x caching solution was based around OSCache, but 
EhCache
+provides some good benefits:
+
+* EhCache is distributable, that is, you can tell multiple instances to use
+  the same cache.
+* EhCache has better configurability, allowing you to configure it in multiple
+  ways, including a JMX bean.
+* EhCache is simpler to use from a programmer's point of view
+* Priha uses EhCache, so there's no need for two caching solutions.
+* EhCache 1.6, is in JSPWiki use, approximately 50% faster.
+
+JSPWiki uses the default CachingManager instance, in which it installs several
+caches.  You may configure them by installing your own "ehcache.xml" document, 
or
+you can just let JSPWiki use its own configurations.  Some of the caches can
+be configured from jspwiki.properties as well to provide backwards 
compatibility.
+
+
+
+CACHES
+======
+
+Name:                   jspwiki.rssCache
+Default configuration:  500 entries; 
+                                               memory only caching; 
+                                               expiry time 1 hr.
+
+This cache stores RSS fragments as Strings to make it easier to deal with 
+RSS-harvesting bots.  If you have a large wiki, you might want to consider
+increasing the size of this cache and enable disk persistence.
+
+-------------------------------------------------------------------------------
+
+Name:                                  jspwiki.renderingCache
+Default configuration: 1000 entries
+                                               memory only caching;
+                                               expiry time 24 hrs.
+                                               
+This cache stores WikiDocuments so that the RenderingManager can skip the 
+expensive parsing part of the pipeline.  Note that the WikiDocuments are
+not Serializable, so therefore you CANNOT distribute nor use disk persistence
+for this cache.
+
+The cache size is also controlled by the jspwiki.properties -setting
+"jspwiki.renderingManager.capacity".  Any EhCache configuration takes 
precedence
+over jspwiki.properties.
+
+-------------------------------------------------------------------------------
+
+Name:                                  jspwiki.dynamicAttachmentCache
+Default configuration: No maximum size
+                                               memory only caching;
+                                               No expiry time.
+                                               
+Stores Dynamic Attachments.  In this case, EhCache is just used as a 
replacement
+for a HashMap, but allows for configurability.
+
+-------------------------------------------------------------------------------
+

Modified: 
incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/priha.properties
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/priha.properties?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/priha.properties 
(original)
+++ incubator/jspwiki/trunk/src/WebContent/WEB-INF/classes/priha.properties Sun 
Jul 26 09:24:21 2009
@@ -23,4 +23,16 @@
 #  File to give a test configuration for Priha.
 #
 
+# Set up an EhCache instance which uses the defaultProvider as the underlying
+# storage.
+priha.provider.ehcache.class = org.priha.providers.EhCachingProvider
+priha.provider.ehcache.realProvider = defaultProvider
+priha.provider.ehcache.size = 1000
+
+# Define workspaces
 priha.provider.defaultProvider.workspaces = default jspwiki
+
+# Finally, tell Priha which providers shall we use.  We only need to mention
+# the EhCache provider, since that already includes the defaultProvider.
+priha.providers = ehcache
+

Added: incubator/jspwiki/trunk/src/WebContent/WEB-INF/lib/ehcache-1.6.0.jar
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/WebContent/WEB-INF/lib/ehcache-1.6.0.jar?rev=797881&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/jspwiki/trunk/src/WebContent/WEB-INF/lib/ehcache-1.6.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: incubator/jspwiki/trunk/src/WebContent/rss.jsp
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/WebContent/rss.jsp?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/WebContent/rss.jsp (original)
+++ incubator/jspwiki/trunk/src/WebContent/rss.jsp Sun Jul 26 09:24:21 2009
@@ -25,17 +25,22 @@
 <%@ page import="java.text.*" %>
 <%@ page import="org.apache.wiki.rss.*" %>
 <%@ page import="org.apache.wiki.util.*" %>
-<%@ page import="com.opensymphony.oscache.base.*" %>
+<%@ page import="net.sf.ehcache.*" %>
 <%@ page import="org.apache.wiki.api.WikiPage" %>
-<%@ taglib uri="http://www.opensymphony.com/oscache"; prefix="oscache" %>
 
 <%!
     Logger log = LoggerFactory.getLogger("JSPWiki");
-    Cache m_cache = new Cache( true, false, false, true, 
-                               
"com.opensymphony.oscache.base.algorithm.LRUCache", 256 );
+    CacheManager m_cacheManager = CacheManager.getInstance();
 %>
 
 <%
+    Cache cache = m_cacheManager.getCache("jspwiki.rssCache");
+    if( cache == null )
+    {
+        cache = new Cache( "jspwiki.rssCache", 500, false, false, 24*3600, 
24*3600 );
+        m_cacheManager.addCache(cache);
+    }
+
     WikiEngine wiki = WikiEngine.getInstance( getServletConfig() );
     // Create wiki context and check for authorization
     WikiContext wikiContext = wiki.createContext( request, "rss" );
@@ -140,20 +145,21 @@
     
     String rss = "";
     
-    try
+    Element e = cache.get(hashKey);
+    
+    if( e != null )
     {
-        rss = (String)m_cache.getFromCache(hashKey);
+        rss = (String)e.getValue();
     }
-    catch( NeedsRefreshException e )
+    else
     { 
         try
         {
             rss = wiki.getRSSGenerator().generateFeed( wikiContext, changed, 
mode, type );
-            m_cache.putInCache(hashKey,rss);
+            cache.put( new Element(hashKey,rss) );
         }
         catch( Exception e1 )
         {
-            m_cache.cancelUpdate(hashKey);            
         }
     }
     

Modified: incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java (original)
+++ incubator/jspwiki/trunk/src/java/org/apache/wiki/Release.java Sun Jul 26 
09:24:21 2009
@@ -77,7 +77,7 @@
      *  <p>
      *  If the build identifier is empty, it is not added.
      */
-    public static final String     BUILD         = "136";
+    public static final String     BUILD         = "137";
 
     /**
      *  This is the generic version string you should use

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/attachment/AttachmentManager.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/attachment/AttachmentManager.java?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- 
incubator/jspwiki/trunk/src/java/org/apache/wiki/attachment/AttachmentManager.java
 (original)
+++ 
incubator/jspwiki/trunk/src/java/org/apache/wiki/attachment/AttachmentManager.java
 Sun Jul 26 09:24:21 2009
@@ -26,6 +26,10 @@
 import java.io.InputStream;
 import java.util.*;
 
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
@@ -40,9 +44,6 @@
 import org.apache.wiki.providers.ProviderException;
 import org.apache.wiki.util.TextUtil;
 
-import com.opensymphony.oscache.base.Cache;
-import com.opensymphony.oscache.base.NeedsRefreshException;
-
 /**
  *  <p>Provides facilities for handling attachments.  All attachment
  *  handling goes through this class.</p>
@@ -77,6 +78,10 @@
     static Logger log = LoggerFactory.getLogger( AttachmentManager.class );
     private WikiEngine             m_engine;
 
+    private CacheManager           m_cachingManager = 
CacheManager.getInstance();
+    private Cache                  m_dynamicAttachments;
+    private static final String    CACHE_NAME = 
"jspwiki.dynamicAttachmentCache";
+    
     /**
      *  List of attachment types which are allowed.
      */
@@ -103,6 +108,14 @@
     public AttachmentManager( WikiEngine engine, Properties props )
     {
         m_engine = engine;
+        
+        m_dynamicAttachments = m_cachingManager.getCache( CACHE_NAME );
+        if( m_dynamicAttachments == null )
+        {
+            m_dynamicAttachments = new Cache( CACHE_NAME, Integer.MAX_VALUE, 
false, true, 3600, 3600 );
+            m_cachingManager.addCache( m_dynamicAttachments );
+        }
+        
         initFileRestrictions();
     }
 
@@ -340,8 +353,6 @@
         return att.getContentAsStream();
     }
 
-    private Cache m_dynamicAttachments = new Cache( true, false, false );
-
     /**
      *  Stores a dynamic attachment.  Unlike storeAttachment(), this just 
stores
      *  the attachment in the memory.
@@ -351,7 +362,7 @@
      */
     public void storeDynamicAttachment( WikiContext ctx, DynamicAttachment att 
)
     {
-        m_dynamicAttachments.putInCache( att.getName(),  att );
+        m_dynamicAttachments.put( new Element( att.getName(), att) );
     }
 
     /**
@@ -365,19 +376,14 @@
 
     public DynamicAttachment getDynamicAttachment( WikiPath name )
     {
-        try
+        Element att = m_dynamicAttachments.get( name.toString() );
+        
+        if( att != null )
         {
-            return (DynamicAttachment) m_dynamicAttachments.getFromCache( 
name.toString() );
+            return (DynamicAttachment) att.getObjectValue();
         }
-        catch( NeedsRefreshException e )
-        {
-            //
-            //  Remove from cache, it has expired.
-            //
-            m_dynamicAttachments.putInCache( name.toString(), null );
 
-            return null;
-        }
+        return null;
     }
 
     /**

Modified: 
incubator/jspwiki/trunk/src/java/org/apache/wiki/render/RenderingManager.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/java/org/apache/wiki/render/RenderingManager.java?rev=797881&r1=797880&r2=797881&view=diff
==============================================================================
--- 
incubator/jspwiki/trunk/src/java/org/apache/wiki/render/RenderingManager.java 
(original)
+++ 
incubator/jspwiki/trunk/src/java/org/apache/wiki/render/RenderingManager.java 
Sun Jul 26 09:24:21 2009
@@ -24,9 +24,13 @@
 import java.io.StringReader;
 import java.lang.reflect.Constructor;
 import java.util.Collection;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
+
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.api.WikiException;
@@ -45,10 +49,6 @@
 import org.apache.wiki.providers.ProviderException;
 import org.apache.wiki.util.TextUtil;
 
-
-import com.opensymphony.oscache.base.Cache;
-import com.opensymphony.oscache.base.NeedsRefreshException;
-
 /**
  *  This class provides a facade towards the differing rendering routines.  
You should
  *  use the routines in this manager instead of the ones in WikiEngine, if you 
don't
@@ -62,7 +62,13 @@
  *  the same size as the page cache), but you may control them separately.
  *  <p>
  *  You can turn caching completely off by stating a cacheSize of zero.
- *
+ *  <p>
+ *  Since 3.0, the underlying cache implementation is based on EhCache, with 
the cache
+ *  name set to {...@link RenderingManager#CACHE_NAME} (Currently set to 
{...@value RenderingManager#CACHE_NAME}.)
+ *  If a cache by that name exists already (e.g. configured in ehcache.xml), 
then that
+ *  cache is used.  If the cache does not exist, then we'll just simply use 
the settings
+ *  from jspwiki.properties.
+ *  
  *  @since  2.4
  */
 public class RenderingManager implements WikiEventListener, InternalModule
@@ -79,12 +85,18 @@
     public  static final String PROP_CACHESIZE    = 
"jspwiki.renderingManager.capacity";
     private static final int    DEFAULT_CACHESIZE = 1000;
     private static final String VERSION_DELIMITER = "::";
-    private static final String OSCACHE_ALGORITHM = 
"com.opensymphony.oscache.base.algorithm.LRUCache";
     private static final String PROP_RENDERER     = 
"jspwiki.renderingManager.renderer";
     
     /** The name of the default renderer. */
     public  static final String DEFAULT_RENDERER  = 
XHTMLRenderer.class.getName();
 
+    /** Name of the EhCache cache. */
+    public  static final String CACHE_NAME        = "jspwiki.renderingCache";
+    
+    /**
+     *  Create a private caching manager.
+     */
+    private CacheManager m_cacheManager = CacheManager.getInstance();
     /**
      *  Stores the WikiDocuments that have been cached.
      */
@@ -121,25 +133,25 @@
         throws WikiException
     {
         m_engine = engine;
-        int cacheSize = TextUtil.getIntegerProperty( properties, 
PROP_CACHESIZE, -1 );
-
-        if( cacheSize == -1 )
-        {
-            cacheSize = TextUtil.getIntegerProperty( properties,
-                                                     
CachingProvider.PROP_CACHECAPACITY,
-                                                     DEFAULT_CACHESIZE );
-        }
+        int cacheSize = TextUtil.getIntegerProperty( properties, 
PROP_CACHESIZE, DEFAULT_CACHESIZE );
 
-        if( cacheSize > 0 )
+        //
+        //  First check, if there's already a cache configured in ehcache.xml
+        //  If not, create our own.
+        //
+        Cache c = m_cacheManager.getCache( CACHE_NAME );
+        
+        if( c == null && cacheSize > 0 )
         {
-            m_documentCache = new Cache(true,false,false,false,
-                                        OSCACHE_ALGORITHM,
-                                        cacheSize);
+            c = new 
Cache(CACHE_NAME,cacheSize,false,false,m_cacheExpiryPeriod,m_cacheExpiryPeriod);
+            m_cacheManager.addCache( c );
         }
         else
         {
             log.info( "RenderingManager caching is disabled." );
         }
+        
+        m_documentCache = m_cacheManager.getCache( CACHE_NAME );
 
         String renderImplName = properties.getProperty( PROP_RENDERER );
         if( renderImplName == null )
@@ -149,8 +161,8 @@
         Class<?>[] rendererParams = { WikiContext.class, WikiDocument.class };
         try
         {
-            Class<?> c = Class.forName( renderImplName );
-            m_rendererConstructor = c.getConstructor( rendererParams );
+            Class<?> cc = Class.forName( renderImplName );
+            m_rendererConstructor = cc.getConstructor( rendererParams );
         }
         catch( ClassNotFoundException e )
         {
@@ -202,28 +214,26 @@
     {
         String pageid = 
context.getRealPage().getName()+VERSION_DELIMITER+context.getRealPage().getVersion();
 
-        boolean wasUpdated = false;
-
         if( m_documentCache != null )
         {
-            try
+            Element e = m_documentCache.get( pageid );
+            
+            if( e != null )
             {
-                WikiDocument doc = (WikiDocument) 
m_documentCache.getFromCache( pageid,
-                                                                               
 m_cacheExpiryPeriod );
-
-                wasUpdated = true;
+                WikiDocument doc = (WikiDocument) e.getObjectValue();
 
                 //
                 //  This check is needed in case the different filters have 
actually
                 //  changed the page data.
                 //  FIXME: Figure out a faster method
+                
                 if( pagedata.equals(doc.getPageData()) )
                 {
                     if( log.isDebugEnabled() ) log.debug("Using cached HTML 
for page "+pageid );
                     return doc;
                 }
             }
-            catch( NeedsRefreshException e )
+            else
             {
                 if( log.isDebugEnabled() ) log.debug("Re-rendering and storing 
"+pageid );
             }
@@ -239,8 +249,7 @@
             doc.setPageData( pagedata );
             if( m_documentCache != null )
             {
-                m_documentCache.putInCache( pageid, doc );
-                wasUpdated = true;
+                m_documentCache.put( new Element( pageid, doc ) );
             }
             return doc;
         }
@@ -248,10 +257,6 @@
         {
             log.error("Unable to parse",ex);
         }
-        finally
-        {
-            if( m_documentCache != null && !wasUpdated ) 
m_documentCache.cancelUpdate( pageid );
-        }
 
         return null;
     }
@@ -328,6 +333,19 @@
         return null;
     }
 
+    private void flushCache( String key )
+    {
+        // We use Strings always
+        List<String> keys = (List<String>)m_documentCache.getKeys();
+        
+        for( String k : keys )
+        {
+            if( k.startsWith( key ) )
+                m_documentCache.remove( k );
+        }
+        
+    }
+    
     /**
      * Flushes the document cache in response to a POST_SAVE_BEGIN event.
      *
@@ -342,7 +360,9 @@
             if( m_documentCache != null )
             {
                 String pageName = ((WikiPageEvent) event).getPageName();
-                m_documentCache.flushPattern( pageName );
+
+                flushCache( pageName );
+
                 try
                 {
                     Collection<WikiPath> referringPages = 
m_engine.getReferenceManager().getReferredBy( WikiPath.valueOf(pageName) );
@@ -356,7 +376,7 @@
                         for ( WikiPath path : referringPages )
                         {
                             if( log.isDebugEnabled() ) log.debug( "Flushing " 
+ path );
-                            m_documentCache.flushPattern( path.toString() );
+                            flushCache( path.toString() );
                         }
                     }
                 }


Reply via email to