This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit a6e23fc82eeae5bfeac2c404e63722106af9f71a
Author: Juan Pablo Santos Rodríguez <[email protected]>
AuthorDate: Thu Dec 2 17:06:36 2021 +0100

    Fix for JSPWIKI-873 + delegate caching operations to CachingManager
---
 .../wiki/providers/CachingAttachmentProvider.java  | 199 +++++++------------
 .../org/apache/wiki/providers/CachingProvider.java | 211 ++++++---------------
 2 files changed, 119 insertions(+), 291 deletions(-)

diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
 
b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
index dad7375..fd67634 100644
--- 
a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
+++ 
b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingAttachmentProvider.java
@@ -18,9 +18,6 @@
  */
 package org.apache.wiki.providers;
 
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.wiki.api.core.Attachment;
@@ -32,6 +29,8 @@ import org.apache.wiki.api.providers.AttachmentProvider;
 import org.apache.wiki.api.providers.WikiProvider;
 import org.apache.wiki.api.search.QueryItem;
 import org.apache.wiki.attachment.AttachmentManager;
+import org.apache.wiki.cache.CacheInfo;
+import org.apache.wiki.cache.CachingManager;
 import org.apache.wiki.util.ClassUtil;
 import org.apache.wiki.util.TextUtil;
 
@@ -39,6 +38,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.NoSuchElementException;
@@ -51,35 +51,12 @@ import java.util.Properties;
  *
  *  @since 2.1.64.
  */
-//        EntryRefreshPolicy for that.
 public class CachingAttachmentProvider implements AttachmentProvider {
 
-    private static final Logger log = 
LogManager.getLogger(CachingAttachmentProvider.class);
+    private static final Logger LOG = LogManager.getLogger( 
CachingAttachmentProvider.class );
 
     private AttachmentProvider m_provider;
-
-    private final CacheManager m_cacheManager = CacheManager.getInstance();
-
-    /** Default cache capacity for now. */
-    public static final int m_capacity = 1_000;
-
-    /** The cache contains Collection objects which contain Attachment 
objects. The key is the parent wiki page name (String). */
-    private Cache m_cache;
-
-    /** Name of the attachment cache. */
-    public static final String ATTCACHE_NAME = "jspwiki.attachmentsCache";
-    /** Name of the attachment cache. */
-    public static final String ATTCOLLCACHE_NAME = 
"jspwiki.attachmentCollectionsCache";
-
-    /**
-     * This cache contains Attachment objects and is keyed by attachment name.
-     * This provides for quickly giving recently changed attachments (for the 
RecentChanges plugin)
-     */
-    private Cache m_attCache;
-
-    private final long m_cacheMisses = 0;
-    private final long m_cacheHits = 0;
-
+    private CachingManager cachingManager;
     private boolean m_gotall;
 
     /**
@@ -87,28 +64,10 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      */
     @Override
     public void initialize( final Engine engine, final Properties properties ) 
throws NoRequiredPropertyException, IOException {
-        log.info( "Initing CachingAttachmentProvider" );
-        final String attCollCacheName = engine.getApplicationName() + "." + 
ATTCOLLCACHE_NAME;
-        if( m_cacheManager.cacheExists( attCollCacheName ) ) {
-            m_cache = m_cacheManager.getCache( attCollCacheName );
-        } else {
-            m_cache = new Cache( attCollCacheName, m_capacity, false, false, 
0, 0 );
-            m_cacheManager.addCache( m_cache );
-        }
+        LOG.info( "Initing CachingAttachmentProvider" );
+        cachingManager = engine.getManager( CachingManager.class );
 
-        //
-        // cache for the individual Attachment objects, attachment name is 
key, the Attachment object is the cached object
-        //
-        final String attCacheName = engine.getApplicationName() + "." + 
ATTCACHE_NAME;
-        if( m_cacheManager.cacheExists( attCacheName ) ) {
-            m_attCache = m_cacheManager.getCache( attCacheName );
-        } else {
-            m_attCache = new Cache( attCacheName, m_capacity, false, false, 0, 
0 );
-            m_cacheManager.addCache( m_attCache );
-        }
-        //
-        //  Find and initialize real provider.
-        //
+        // Find and initialize real provider.
         final String classname;
         try {
             classname = TextUtil.getRequiredProperty( properties, 
AttachmentManager.PROP_PROVIDER );
@@ -118,10 +77,10 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
 
         try {
             m_provider = ClassUtil.buildInstance( "org.apache.wiki.providers", 
classname );
-            log.debug( "Initializing real provider class {}", m_provider );
+            LOG.debug( "Initializing real provider class {}", m_provider );
             m_provider.initialize( engine, properties );
         } catch( final ReflectiveOperationException e ) {
-            log.error( "Unable to instantiate provider class {}", classname, e 
);
+            LOG.error( "Unable to instantiate provider class {}", classname, e 
);
             throw new IllegalArgumentException( "illegal provider class", e );
         }
     }
@@ -130,21 +89,18 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      * {@inheritDoc}
      */
     @Override
-    public void putAttachmentData( final Attachment att, final InputStream 
data )
-        throws ProviderException, IOException {
+    public void putAttachmentData( final Attachment att, final InputStream 
data ) throws ProviderException, IOException {
         m_provider.putAttachmentData( att, data );
-
-        m_cache.remove(att.getParentName());
-        att.setLastModified(new Date());
-        m_attCache.put(new Element(att.getName(), att));
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS_COLLECTION, 
att.getParentName() );
+        att.setLastModified( new Date() );
+        cachingManager.put( CachingManager.CACHE_ATTACHMENTS, att.getName(), 
att );
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
-    public InputStream getAttachmentData( final Attachment att )
-        throws ProviderException, IOException {
+    public InputStream getAttachmentData( final Attachment att ) throws 
ProviderException, IOException {
         return m_provider.getAttachmentData( att );
     }
 
@@ -152,31 +108,22 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      * {@inheritDoc}
      */
     @Override
-    public List< Attachment > listAttachments( final Page page) throws 
ProviderException {
-        log.debug("Listing attachments for " + page);
-        final Element element = m_cache.get(page.getName());
-
-        if (element != null) {
-            @SuppressWarnings("unchecked") final List< Attachment > c = ( 
List< Attachment > )element.getObjectValue();
-            log.debug("LIST from cache, " + page.getName() + ", size=" + 
c.size());
-            return cloneCollection(c);
-        }
-
-        log.debug("list NOT in cache, " + page.getName());
-
-        return refresh(page);
+    public List< Attachment > listAttachments( final Page page ) throws 
ProviderException {
+        LOG.debug( "Listing attachments for {}", page );
+        final List< Attachment > atts = cachingManager.get( 
CachingManager.CACHE_ATTACHMENTS_COLLECTION, page.getName(),
+                                                            () -> 
m_provider.listAttachments( page ) );
+        return cloneCollection( atts );
     }
 
     private < T > List< T > cloneCollection( final Collection< T > c ) {
-        return new ArrayList<>( c );
+        return c != null ? new ArrayList<>( c ) : Collections.emptyList();
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
-    public Collection< Attachment > findAttachments( final QueryItem[] query )
-    {
+    public Collection< Attachment > findAttachments( final QueryItem[] query ) 
{
         return m_provider.findAttachments( query );
     }
 
@@ -184,31 +131,37 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      * {@inheritDoc}
      */
     @Override
-    public List<Attachment> listAllChanged( final Date timestamp ) throws 
ProviderException {
+    public List< Attachment > listAllChanged( final Date timestamp ) throws 
ProviderException {
         final List< Attachment > all;
-        // we do a one-time build up of the cache, after this the cache is 
updated for every attachment add/delete
         if ( !m_gotall ) {
-            all = m_provider.listAllChanged(timestamp);
+            all = m_provider.listAllChanged( timestamp );
 
-            // Put all pages in the cache :
-            synchronized (this) {
+            // Make sure that all attachments are in the cache.
+            synchronized( this ) {
                 for( final Attachment att : all ) {
-                    m_attCache.put( new Element( att.getName(), att ) );
+                    cachingManager.put( CachingManager.CACHE_ATTACHMENTS, 
att.getName(), att );
                 }
                 m_gotall = true;
             }
         } else {
-            @SuppressWarnings("unchecked") final List< String > keys = 
m_attCache.getKeysWithExpiryCheck();
+            final List< String > keys = cachingManager.keys( 
CachingManager.CACHE_ATTACHMENTS );
             all = new ArrayList<>();
-            for ( final String key : keys) {
-                final Element element = m_attCache.get(key);
-                final Attachment cachedAttachment = ( Attachment 
)element.getObjectValue();
-                if (cachedAttachment != null) {
-                    all.add(cachedAttachment);
+            for( final String key : keys) {
+                final Attachment cachedAttachment = cachingManager.get( 
CachingManager.CACHE_ATTACHMENTS, key, () -> null );
+                if( cachedAttachment != null ) {
+                    all.add( cachedAttachment );
                 }
             }
         }
 
+        if( cachingManager.enabled( CachingManager.CACHE_ATTACHMENTS )
+                && all.size() >= cachingManager.info( 
CachingManager.CACHE_ATTACHMENTS ).getMaxElementsAllowed() ) {
+            LOG.warn( "seems {} can't hold all attachments from your page 
repository, " +
+                    "so we're delegating on the underlying provider instead. 
Please consider increasing " +
+                    "your cache sizes on the ehcache configuration file to 
avoid this behaviour", CachingManager.CACHE_ATTACHMENTS );
+            return m_provider.listAllChanged( timestamp );
+        }
+
         return all;
     }
 
@@ -219,9 +172,11 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      *  @return null, if no such attachment was in this collection.
      */
     private Attachment findAttachmentFromCollection( final Collection< 
Attachment > c, final String name ) {
-        for( final Attachment att : new ArrayList< >( c ) ) {
-            if( name.equals( att.getFileName() ) ) {
-                return att;
+        if( c != null ) {
+            for( final Attachment att : c ) {
+                if( name.equals( att.getFileName() ) ) {
+                    return att;
+                }
             }
         }
 
@@ -229,47 +184,18 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
     }
 
     /**
-     *  Refreshes the cache content and updates counters.
-     *
-     *  @return The newly fetched object from the provider.
-     */
-    private List< Attachment > refresh( final Page page ) throws 
ProviderException {
-        final List< Attachment > c = m_provider.listAttachments( page );
-        m_cache.put( new Element( page.getName(), c ) );
-        return c;
-    }
-
-    /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     @Override
-    public Attachment getAttachmentInfo( final Page page, final String name, 
final int version) throws ProviderException {
-        if( log.isDebugEnabled() ) {
-            log.debug( "Getting attachments for " + page + ", name=" + name + 
", version=" + version );
-        }
-
+    public Attachment getAttachmentInfo( final Page page, final String name, 
final int version ) throws ProviderException {
+        LOG.debug( "Getting attachments for {}, name={}, version={}", page, 
name, version );
         //  We don't cache previous versions
         if( version != WikiProvider.LATEST_VERSION ) {
-            log.debug( "...we don't cache old versions" );
+            LOG.debug( "...we don't cache old versions" );
             return m_provider.getAttachmentInfo( page, name, version );
         }
-
-        final Collection< Attachment > c;
-        final Element element = m_cache.get( page.getName() );
-
-        if( element == null ) {
-            log.debug( page.getName() + " wasn't in the cache" );
-            c = refresh( page );
-
-            if( c == null ) {
-                return null; // No such attachment
-            }
-        } else {
-            log.debug( page.getName() + " FOUND in the cache" );
-            c = ( Collection< Attachment > )element.getObjectValue();
-        }
-
+        final Collection< Attachment > c = cachingManager.get( 
CachingManager.CACHE_ATTACHMENTS_COLLECTION, page.getName(),
+                                                               ()-> 
m_provider.listAttachments( page ) );
         return findAttachmentFromCollection( c, name );
     }
 
@@ -277,7 +203,7 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      * {@inheritDoc}
      */
     @Override
-    public List<Attachment> getVersionHistory( final Attachment att ) {
+    public List< Attachment > getVersionHistory( final Attachment att ) {
         return m_provider.getVersionHistory( att );
     }
 
@@ -287,7 +213,7 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
     @Override
     public void deleteVersion( final Attachment att ) throws ProviderException 
{
         // This isn't strictly speaking correct, but it does not really matter
-        m_cache.remove( att.getParentName() );
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS_COLLECTION, 
att.getParentName() );
         m_provider.deleteVersion( att );
     }
 
@@ -296,8 +222,8 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      */
     @Override
     public void deleteAttachment( final Attachment att ) throws 
ProviderException {
-        m_cache.remove( att.getParentName() );
-        m_attCache.remove( att.getName() );
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS_COLLECTION, 
att.getParentName() );
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS, att.getName() 
);
         m_provider.deleteAttachment( att );
     }
 
@@ -308,9 +234,13 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
      */
     @Override
     public synchronized String getProviderInfo() {
+        final CacheInfo attCacheInfo = cachingManager.info( 
CachingManager.CACHE_ATTACHMENTS );
+        final CacheInfo attColCacheInfo = cachingManager.info( 
CachingManager.CACHE_ATTACHMENTS_COLLECTION );
         return "Real provider: " + m_provider.getClass().getName() +
-                ".  Cache misses: " + m_cacheMisses +
-                ".  Cache hits: " + m_cacheHits;
+                ". Attachment cache hits: " + attCacheInfo.getHits() +
+                ". Attachment cache misses: " + attCacheInfo.getMisses() +
+                ". Attachment collection cache hits: " + 
attColCacheInfo.getHits() +
+                ". Attachment collection cache misses: " + 
attColCacheInfo.getMisses();
     }
 
     /**
@@ -328,16 +258,15 @@ public class CachingAttachmentProvider implements 
AttachmentProvider {
     @Override
     public void moveAttachmentsForPage( final String oldParent, final String 
newParent ) throws ProviderException {
         m_provider.moveAttachmentsForPage( oldParent, newParent );
-        m_cache.remove( newParent );
-        m_cache.remove( oldParent );
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS_COLLECTION, 
newParent );
+        cachingManager.remove( CachingManager.CACHE_ATTACHMENTS_COLLECTION, 
oldParent );
 
         // This is a kludge to make sure that the pages are removed from the 
other cache as well.
         final String checkName = oldParent + "/";
-
-        @SuppressWarnings("unchecked") final List< String > names = 
m_cache.getKeysWithExpiryCheck();
+        final List< String > names = cachingManager.keys( 
CachingManager.CACHE_ATTACHMENTS_COLLECTION );
         for( final String name : names ) {
             if( name.startsWith( checkName ) ) {
-                m_attCache.remove( name );
+                cachingManager.remove( CachingManager.CACHE_ATTACHMENTS, name 
);
             }
         }
     }
diff --git 
a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java 
b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
index de9e651..1be3b29 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/providers/CachingProvider.java
@@ -18,9 +18,6 @@
  */
 package org.apache.wiki.providers;
 
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.wiki.api.core.Context;
@@ -32,6 +29,8 @@ import org.apache.wiki.api.providers.PageProvider;
 import org.apache.wiki.api.search.QueryItem;
 import org.apache.wiki.api.search.SearchResult;
 import org.apache.wiki.api.spi.Wiki;
+import org.apache.wiki.cache.CacheInfo;
+import org.apache.wiki.cache.CachingManager;
 import org.apache.wiki.pages.PageManager;
 import org.apache.wiki.parser.MarkupParser;
 import org.apache.wiki.render.RenderingManager;
@@ -59,84 +58,27 @@ import java.util.TreeSet;
  *
  *  @since 1.6.4
  */
-// FIXME: Synchronization is a bit inconsistent in places.
-// FIXME: A part of the stuff is now redundant, since we could easily use the 
text cache for a lot of things.  RefactorMe.
 public class CachingProvider implements PageProvider {
 
-    private static final Logger log = LogManager.getLogger( 
CachingProvider.class );
-
-    private final CacheManager m_cacheManager = CacheManager.getInstance();
+    private static final Logger LOG = LogManager.getLogger( 
CachingProvider.class );
 
+    private CachingManager cachingManager;
     private PageProvider m_provider;
-    // FIXME: Find another way to the search engine to use instead of from 
Engine?
     private Engine m_engine;
 
-    private Cache m_cache;
-    /** Name of the regular page cache. */
-    public static final String CACHE_NAME = "jspwiki.pageCache";
-
-    private Cache            m_textCache;
-    /** Name of the page text cache. */
-    public static final String TEXTCACHE_NAME = "jspwiki.pageTextCache";
-
-    private Cache            m_historyCache;
-    /** Name of the page history cache. */
-    public static final String HISTORYCACHE_NAME = "jspwiki.pageHistoryCache";
-
-    private long             m_cacheMisses;
-    private long             m_cacheHits;
-
-    private long             m_historyCacheMisses;
-    private long             m_historyCacheHits;
-
     // FIXME: This MUST be cached somehow.
-
-    private boolean          m_gotall;
-
-    // The default settings of the caches, if you want something else, provide 
an "ehcache.xml" file
-    // Please note that JSPWiki ships with a default "ehcache.xml" in the 
classpath
-    public static final int   DEFAULT_CACHECAPACITY   = 1000; // Good most 
wikis
-    public static final int   DEFAULT_CACHETIMETOLIVESECONDS = 24*3600;
-    public static final int   DEFAULT_CACHETIMETOIDLESECONDS = 24*3600;
+    private boolean m_gotall;
 
     /**
      *  {@inheritDoc}
      */
     @Override
     public void initialize( final Engine engine, final Properties properties ) 
throws NoRequiredPropertyException, IOException {
-        log.debug("Initing CachingProvider");
+        LOG.debug( "Initing CachingProvider" );
 
         // engine is used for getting the search engine
         m_engine = engine;
-
-        final String cacheName = engine.getApplicationName() + "." + 
CACHE_NAME;
-        if (m_cacheManager.cacheExists(cacheName)) {
-            m_cache = m_cacheManager.getCache(cacheName);
-        } else {
-            log.info("cache with name " + cacheName +  " not found in 
ehcache.xml, creating it with defaults.");
-            m_cache = new Cache(cacheName, DEFAULT_CACHECAPACITY, false, 
false, DEFAULT_CACHETIMETOLIVESECONDS, DEFAULT_CACHETIMETOIDLESECONDS);
-            m_cacheManager.addCache(m_cache);
-        }
-
-        final String textCacheName = engine.getApplicationName() + "." + 
TEXTCACHE_NAME;
-        if (m_cacheManager.cacheExists(textCacheName)) {
-            m_textCache= m_cacheManager.getCache(textCacheName);
-        } else {
-            log.info("cache with name " + textCacheName +  " not found in 
ehcache.xml, creating it with defaults.");
-            m_textCache = new Cache(textCacheName, DEFAULT_CACHECAPACITY, 
false, false, DEFAULT_CACHETIMETOLIVESECONDS, DEFAULT_CACHETIMETOIDLESECONDS);
-            m_cacheManager.addCache(m_textCache);
-        }
-
-        final String historyCacheName = engine.getApplicationName() + "." + 
HISTORYCACHE_NAME;
-        if (m_cacheManager.cacheExists(historyCacheName)) {
-            m_historyCache= m_cacheManager.getCache(historyCacheName);
-        } else {
-            log.info("cache with name " + historyCacheName +  " not found in 
ehcache.xml, creating it with defaults.");
-            m_historyCache = new Cache(historyCacheName, 
DEFAULT_CACHECAPACITY, false, false, DEFAULT_CACHETIMETOLIVESECONDS, 
DEFAULT_CACHETIMETOIDLESECONDS);
-            m_cacheManager.addCache(m_historyCache);
-        }
-
-        // m_cache.getCacheEventNotificationService().registerListener(new 
CacheItemCollector());
+        cachingManager = m_engine.getManager( CachingManager.class );
 
         //  Find and initialize real provider.
         final String classname;
@@ -148,32 +90,20 @@ public class CachingProvider implements PageProvider {
 
         try {
             m_provider = ClassUtil.buildInstance( "org.apache.wiki.providers", 
classname );
-            log.debug( "Initializing real provider class {}", m_provider );
+            LOG.debug( "Initializing real provider class {}", m_provider );
             m_provider.initialize( engine, properties );
         } catch( final ReflectiveOperationException e ) {
-            log.error( "Unable to instantiate provider class {}", classname, e 
);
+            LOG.error( "Unable to instantiate provider class {}", classname, e 
);
             throw new IllegalArgumentException( "illegal provider class", e );
         }
     }
 
-    private Page getPageInfoFromCache( final String name) throws 
ProviderException {
+    private Page getPageInfoFromCache( final String name ) throws 
ProviderException {
         // Sanity check; seems to occur sometimes
         if( name == null ) {
             return null;
         }
-
-        final Element cacheElement = m_cache.get( name );
-        if( cacheElement == null ) {
-            final Page refreshed = m_provider.getPageInfo( name, 
PageProvider.LATEST_VERSION );
-            if( refreshed != null ) {
-                m_cache.put( new Element( name, refreshed ) );
-                return refreshed;
-            } else {
-                // page does not exist anywhere
-                return null;
-            }
-        }
-        return ( Page )cacheElement.getObjectValue();
+        return cachingManager.get( CachingManager.CACHE_PAGES, name, () -> 
m_provider.getPageInfo( name, PageProvider.LATEST_VERSION ) );
     }
 
 
@@ -190,7 +120,7 @@ public class CachingProvider implements PageProvider {
         try {
             p = getPageInfoFromCache( pageName );
         } catch( final ProviderException e ) {
-            log.info( "Provider failed while trying to check if page exists: " 
+ pageName );
+            LOG.info( "Provider failed while trying to check if page exists: 
{}", pageName );
             return false;
         }
 
@@ -224,7 +154,7 @@ public class CachingProvider implements PageProvider {
         try {
             p = getPageInfoFromCache( pageName );
         } catch( final ProviderException e ) {
-            log.info( "Provider failed while trying to check if page exists: " 
+ pageName );
+            LOG.info( "Provider failed while trying to check if page exists: 
{}", pageName );
             return false;
         }
 
@@ -270,26 +200,17 @@ public class CachingProvider implements PageProvider {
         return result;
     }
 
-
     private String getTextFromCache( final String pageName ) throws 
ProviderException {
-        if (pageName == null) {
+        if( pageName == null ) {
             return null;
         }
 
-        final String text;
-        final Element cacheElement = m_textCache.get(pageName);
-        if( cacheElement != null ) {
-            m_cacheHits++;
-            return ( String )cacheElement.getObjectValue();
-        }
-        if( pageExists( pageName ) ) {
-            text = m_provider.getPageText( pageName, 
PageProvider.LATEST_VERSION );
-            m_textCache.put( new Element( pageName, text ) );
-            m_cacheMisses++;
-            return text;
-        }
-        //page not found (not in cache, not by real provider)
-        return  null;
+        return cachingManager.get( CachingManager.CACHE_PAGES_TEXT, pageName, 
() -> {
+            if( pageExists( pageName ) ) {
+                return m_provider.getPageText( pageName, 
PageProvider.LATEST_VERSION );
+            }
+            return null;
+        } );
     }
 
     /**
@@ -302,9 +223,9 @@ public class CachingProvider implements PageProvider {
             page.setLastModified( new Date() );
 
             // Refresh caches properly
-            m_cache.remove( page.getName() );
-            m_textCache.remove( page.getName() );
-            m_historyCache.remove( page.getName() );
+            cachingManager.remove( CachingManager.CACHE_PAGES, page.getName() 
);
+            cachingManager.remove( CachingManager.CACHE_PAGES_TEXT, 
page.getName() );
+            cachingManager.remove( CachingManager.CACHE_PAGES_HISTORY, 
page.getName() );
 
             getPageInfoFromCache( page.getName() );
         }
@@ -316,35 +237,32 @@ public class CachingProvider implements PageProvider {
     @Override
     public Collection< Page > getAllPages() throws ProviderException {
         final Collection< Page > all;
-
         if ( !m_gotall ) {
             all = m_provider.getAllPages();
-
             // Make sure that all pages are in the cache.
             synchronized( this ) {
                 for( final Page p : all ) {
-                    m_cache.put( new Element( p.getName(), p ) );
+                    cachingManager.put( CachingManager.CACHE_PAGES,  
p.getName(), p );
                 }
-
                 m_gotall = true;
             }
         } else {
-            @SuppressWarnings("unchecked") final List< String > keys = 
m_cache.getKeysWithExpiryCheck();
+            final List< String > keys = cachingManager.keys( 
CachingManager.CACHE_PAGES );
             all = new TreeSet<>();
             for( final String key : keys ) {
-                final Element element = m_cache.get( key );
-                final Page cachedPage = ( Page )element.getObjectValue();
+                final Page cachedPage = cachingManager.get( 
CachingManager.CACHE_PAGES, key, () -> null );
                 if( cachedPage != null ) {
                     all.add( cachedPage );
                 }
             }
         }
 
-        if( all.size() >= 
m_cache.getCacheConfiguration().getMaxEntriesLocalHeap() ) {
-               log.warn( "seems " + m_cache.getName() + " can't hold all pages 
from your page repository, " +
-                                 "so we're delegating on the underlying 
provider instead. Please consider increasing " +
-                                 "your cache sizes on ehcache.xml to avoid 
this behaviour" );
-               return m_provider.getAllPages();
+        if( cachingManager.enabled( CachingManager.CACHE_PAGES )
+                && all.size() >= cachingManager.info( 
CachingManager.CACHE_PAGES ).getMaxElementsAllowed() ) {
+            LOG.warn( "seems {} can't hold all pages from your page 
repository, " +
+                    "so we're delegating on the underlying provider instead. 
Please consider increasing " +
+                    "your cache sizes on the ehcache configuration file to 
avoid this behaviour", CachingManager.CACHE_PAGES );
+            return m_provider.getAllPages();
         }
 
         return all;
@@ -387,7 +305,7 @@ public class CachingProvider implements PageProvider {
 
                 parser.parse();
             } catch( final Exception ex ) {
-                log.debug( "Failed to retrieve variables for wikipage " + page 
);
+                LOG.debug( "Failed to retrieve variables for wikipage {}", 
page );
             }
         }
     }
@@ -401,15 +319,7 @@ public class CachingProvider implements PageProvider {
         final Page cached = getPageInfoFromCache( pageName );
         final int latestcached = ( cached != null ) ? cached.getVersion() : 
Integer.MIN_VALUE;
         if( version == PageProvider.LATEST_VERSION || version == latestcached 
) {
-            if( cached == null ) {
-                final Page data = m_provider.getPageInfo( pageName, version );
-                if( data != null ) {
-                    m_cache.put( new Element( pageName, data ) );
-                }
-                page = data;
-            } else {
-                page = cached;
-            }
+            page = cached;
         } else {
             // We do not cache old versions.
             page = m_provider.getPageInfo( pageName, version );
@@ -421,38 +331,28 @@ public class CachingProvider implements PageProvider {
     /**
      *  {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
     @Override
     public List< Page > getVersionHistory( final String pageName) throws 
ProviderException {
-        final List< Page > history;
         if( pageName == null ) {
             return null;
         }
-        final Element element = m_historyCache.get( pageName );
-        if( element != null ) {
-            m_historyCacheHits++;
-            history = ( List< Page > )element.getObjectValue();
-        } else {
-            history = m_provider.getVersionHistory( pageName );
-            m_historyCache.put( new Element( pageName, history ) );
-            m_historyCacheMisses++;
-        }
-
-        return history;
+        return cachingManager.get( CachingManager.CACHE_PAGES_HISTORY, 
pageName, () -> m_provider.getVersionHistory( pageName ) );
     }
 
     /**
      * Gets the provider class name, and cache statistics (misscount and 
hitcount of page cache and history cache).
      *
-     * @return A plain string with all the above mentioned values.
+     * @return A plain string with all the above-mentioned values.
      */
     @Override
     public synchronized String getProviderInfo() {
+        final CacheInfo pageCacheInfo = cachingManager.info( 
CachingManager.CACHE_PAGES );
+        final CacheInfo pageHistoryCacheInfo = cachingManager.info( 
CachingManager.CACHE_PAGES_HISTORY );
         return "Real provider: " + m_provider.getClass().getName()+
-                ". Cache misses: " + m_cacheMisses+
-                ". Cache hits: " + m_cacheHits+
-                ". History cache hits: " + m_historyCacheHits+
-                ". History cache misses: " + m_historyCacheMisses;
+                ". Page cache hits: " + pageCacheInfo.getHits() +
+                ". Page cache misses: " + pageCacheInfo.getMisses() +
+                ". History cache hits: " + pageHistoryCacheInfo.getHits() +
+                ". History cache misses: " + pageHistoryCacheInfo.getMisses();
     }
 
     /**
@@ -467,12 +367,12 @@ public class CachingProvider implements PageProvider {
 
             //  If we have this version cached, remove from cache.
             if( version == PageProvider.LATEST_VERSION || version == 
latestcached ) {
-                m_cache.remove( pageName );
-                m_textCache.remove( pageName );
+                cachingManager.remove( CachingManager.CACHE_PAGES, pageName );
+                cachingManager.remove( CachingManager.CACHE_PAGES_TEXT, 
pageName );
             }
 
             m_provider.deleteVersion( pageName, version );
-            m_historyCache.remove( pageName );
+            cachingManager.remove( CachingManager.CACHE_PAGES_HISTORY, 
pageName );
         }
     }
 
@@ -483,9 +383,9 @@ public class CachingProvider implements PageProvider {
     public void deletePage( final String pageName ) throws ProviderException {
         //  See note in deleteVersion().
         synchronized( this ) {
-            m_cache.put( new Element( pageName, null ) );
-            m_textCache.put( new Element( pageName, null ) );
-            m_historyCache.put( new Element( pageName, null ) );
+            cachingManager.put( CachingManager.CACHE_PAGES, pageName, null );
+            cachingManager.put( CachingManager.CACHE_PAGES_TEXT, pageName, 
null );
+            cachingManager.put( CachingManager.CACHE_PAGES_HISTORY, pageName, 
null );
             m_provider.deletePage( pageName );
         }
     }
@@ -496,16 +396,15 @@ public class CachingProvider implements PageProvider {
     @Override
     public void movePage( final String from, final String to ) throws 
ProviderException {
         m_provider.movePage( from, to );
-
         synchronized( this ) {
             // Clear any cached version of the old page and new page
-            m_cache.remove( from );
-            m_textCache.remove( from );
-            m_historyCache.remove( from );
-            log.debug( "Removing to page " + to + " from cache" );
-            m_cache.remove( to );
-            m_textCache.remove( to );
-            m_historyCache.remove( to );
+            cachingManager.remove( CachingManager.CACHE_PAGES, from );
+            cachingManager.remove( CachingManager.CACHE_PAGES_TEXT, from );
+            cachingManager.remove( CachingManager.CACHE_PAGES_HISTORY, from );
+            LOG.debug( "Removing to page {} from cache", to );
+            cachingManager.remove( CachingManager.CACHE_PAGES, to );
+            cachingManager.remove( CachingManager.CACHE_PAGES_TEXT, to );
+            cachingManager.remove( CachingManager.CACHE_PAGES_HISTORY, to );
         }
     }
 

Reply via email to