commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1775170 - in /commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory: AbstractDoubleLinkedListMemoryCache.java AbstractMemoryCache.java lru/LHMLRUMemoryCache.java soft/SoftReferenceMemoryCache.java
Date Mon, 19 Dec 2016 20:24:52 GMT
Author: tv
Date: Mon Dec 19 20:24:52 2016
New Revision: 1775170

URL: http://svn.apache.org/viewvc?rev=1775170&view=rev
Log:
Refactor common code

Modified:
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
    commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/soft/SoftReferenceMemoryCache.java

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java?rev=1775170&r1=1775169&r2=1775170&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
(original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractDoubleLinkedListMemoryCache.java
Mon Dec 19 20:24:52 2016
@@ -20,14 +20,13 @@ package org.apache.commons.jcs.engine.me
  */
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.jcs.engine.CacheConstants;
 import org.apache.commons.jcs.engine.behavior.ICacheElement;
@@ -36,7 +35,6 @@ import org.apache.commons.jcs.engine.con
 import org.apache.commons.jcs.engine.memory.util.DefaultMemoryElementDescriptor;
 import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
 import org.apache.commons.jcs.engine.stats.StatElement;
-import org.apache.commons.jcs.engine.stats.Stats;
 import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
 import org.apache.commons.jcs.engine.stats.behavior.IStats;
 import org.apache.commons.jcs.utils.struct.DoubleLinkedList;
@@ -59,15 +57,6 @@ public abstract class AbstractDoubleLink
     /** thread-safe double linked list for lru */
     protected DoubleLinkedList<MemoryElementDescriptor<K, V>> list; // TODO privatise
 
-    /** number of hits */
-    private AtomicLong hitCnt;
-
-    /** number of misses */
-    private AtomicLong missCnt;
-
-    /** number of puts */
-    private AtomicLong putCnt;
-
     /**
      * For post reflection creation initialization.
      * <p>
@@ -77,35 +66,9 @@ public abstract class AbstractDoubleLink
     @Override
     public void initialize(CompositeCache<K, V> hub)
     {
-        lock.lock();
-        try
-        {
-            super.initialize(hub);
-            hitCnt = new AtomicLong(0);
-            missCnt = new AtomicLong(0);
-            putCnt = new AtomicLong(0);
-            list = new DoubleLinkedList<MemoryElementDescriptor<K, V>>();
-            log.info("initialized MemoryCache for " + getCacheName());
-        }
-        finally
-        {
-            lock.unlock();
-        }
-    }
-
-    /**
-     * Reset statistics
-     *
-     * @see org.apache.commons.jcs.engine.memory.AbstractMemoryCache#dispose()
-     */
-    @Override
-    public void dispose() throws IOException
-    {
-        super.dispose();
-        removeAll();
-        hitCnt.set(0);
-        missCnt.set(0);
-        putCnt.set(0);
+        super.initialize(hub);
+        list = new DoubleLinkedList<MemoryElementDescriptor<K, V>>();
+        log.info("initialized MemoryCache for " + getCacheName());
     }
 
     /**
@@ -689,26 +652,12 @@ public abstract class AbstractDoubleLink
     @Override
     public IStats getStatistics()
     {
-        IStats stats = new Stats();
+        IStats stats = super.getStatistics();
         stats.setTypeName( /* add algorithm name */"Memory Cache");
 
-        ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
-
-        lock.lock(); // not sure that's really relevant here but not that important
-        try
-        {
-            elems.add(new StatElement<Integer>("List Size", Integer.valueOf(list.size())));
-            elems.add(new StatElement<Integer>("Map Size", Integer.valueOf(map.size())));
-            elems.add(new StatElement<AtomicLong>("Put Count", putCnt));
-            elems.add(new StatElement<AtomicLong>("Hit Count", hitCnt));
-            elems.add(new StatElement<AtomicLong>("Miss Count", missCnt));
-        }
-        finally
-        {
-            lock.unlock();
-        }
+        List<IStatElement<?>> elems = stats.getStatElements();
 
-        stats.setStatElements(elems);
+        elems.add(new StatElement<Integer>("List Size", Integer.valueOf(list.size())));
 
         return stats;
     }

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java?rev=1775170&r1=1775169&r2=1775170&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
(original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/AbstractMemoryCache.java
Mon Dec 19 20:24:52 2016
@@ -20,9 +20,11 @@ package org.apache.commons.jcs.engine.me
  */
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -32,7 +34,9 @@ import org.apache.commons.jcs.engine.beh
 import org.apache.commons.jcs.engine.control.CompositeCache;
 import org.apache.commons.jcs.engine.memory.behavior.IMemoryCache;
 import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
+import org.apache.commons.jcs.engine.stats.StatElement;
 import org.apache.commons.jcs.engine.stats.Stats;
+import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
 import org.apache.commons.jcs.engine.stats.behavior.IStats;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -66,6 +70,15 @@ public abstract class AbstractMemoryCach
     /** Map where items are stored by key.  This is created by the concrete child class.
*/
     protected Map<K, MemoryElementDescriptor<K, V>> map;// TODO privatise
 
+    /** number of hits */
+    protected AtomicLong hitCnt;
+
+    /** number of misses */
+    protected AtomicLong missCnt;
+
+    /** number of puts */
+    protected AtomicLong putCnt;
+
     /**
      * For post reflection creation initialization
      * <p>
@@ -74,20 +87,17 @@ public abstract class AbstractMemoryCach
     @Override
     public void initialize( CompositeCache<K, V> hub )
     {
-        lock.lock();
-        try
-        {
-            this.cacheAttributes = hub.getCacheAttributes();
-            this.cache = hub;
-            map = createMap();
+        hitCnt = new AtomicLong(0);
+        missCnt = new AtomicLong(0);
+        putCnt = new AtomicLong(0);
 
-            chunkSize = cacheAttributes.getSpoolChunkSize();
-            status = CacheStatus.ALIVE;
-        }
-        finally
-        {
-            lock.unlock();
-        }
+        this.cacheAttributes = hub.getCacheAttributes();
+        this.chunkSize = cacheAttributes.getSpoolChunkSize();
+        this.cache = hub;
+
+        this.map = createMap();
+
+        this.status = CacheStatus.ALIVE;
     }
 
     /**
@@ -213,7 +223,7 @@ public abstract class AbstractMemoryCach
     }
 
     /**
-     * Prepares for shutdown.
+     * Prepares for shutdown. Reset statistics
      * <p>
      * @throws IOException
      */
@@ -221,6 +231,10 @@ public abstract class AbstractMemoryCach
     public void dispose()
         throws IOException
     {
+        removeAll();
+        hitCnt.set(0);
+        missCnt.set(0);
+        putCnt.set(0);
         log.info( "Memory Cache dispose called." );
     }
 
@@ -232,6 +246,15 @@ public abstract class AbstractMemoryCach
     {
         IStats stats = new Stats();
         stats.setTypeName( "Abstract Memory Cache" );
+
+        ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
+        stats.setStatElements(elems);
+
+        elems.add(new StatElement<AtomicLong>("Put Count", putCnt));
+        elems.add(new StatElement<AtomicLong>("Hit Count", hitCnt));
+        elems.add(new StatElement<AtomicLong>("Miss Count", missCnt));
+        elems.add(new StatElement<Integer>( "Map Size", Integer.valueOf(getSize())
) );
+
         return stats;
     }
 

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java?rev=1775170&r1=1775169&r2=1775170&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
(original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/lru/LHMLRUMemoryCache.java
Mon Dec 19 20:24:52 2016
@@ -20,13 +20,11 @@ package org.apache.commons.jcs.engine.me
  */
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.jcs.engine.CacheConstants;
 import org.apache.commons.jcs.engine.behavior.ICacheElement;
@@ -35,9 +33,6 @@ import org.apache.commons.jcs.engine.con
 import org.apache.commons.jcs.engine.memory.AbstractMemoryCache;
 import org.apache.commons.jcs.engine.memory.util.DefaultMemoryElementDescriptor;
 import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
-import org.apache.commons.jcs.engine.stats.StatElement;
-import org.apache.commons.jcs.engine.stats.Stats;
-import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
 import org.apache.commons.jcs.engine.stats.behavior.IStats;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -51,15 +46,6 @@ public class LHMLRUMemoryCache<K, V>
     /** The Logger. */
     private static final Log log = LogFactory.getLog( LRUMemoryCache.class );
 
-    /** number of hits */
-    private AtomicLong hitCnt;
-
-    /** number of misses */
-    private AtomicLong missCnt;
-
-    /** number of puts */
-    private AtomicLong putCnt;
-
     /**
      * For post reflection creation initialization
      * <p>
@@ -69,9 +55,6 @@ public class LHMLRUMemoryCache<K, V>
     public void initialize( CompositeCache<K, V> hub )
     {
         super.initialize( hub );
-        hitCnt = new AtomicLong(0);
-        missCnt = new AtomicLong(0);
-        putCnt = new AtomicLong(0);
         log.info( "initialized LHMLRUMemoryCache for " + getCacheName() );
     }
 
@@ -230,18 +213,9 @@ public class LHMLRUMemoryCache<K, V>
     @Override
     public IStats getStatistics()
     {
-        IStats stats = new Stats();
+        IStats stats = super.getStatistics();
         stats.setTypeName( "LHMLRU Memory Cache" );
 
-        ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
-
-        elems.add(new StatElement<Integer>( "Map Size", Integer.valueOf(map.size())
) );
-        elems.add(new StatElement<AtomicLong>("Put Count", putCnt));
-        elems.add(new StatElement<AtomicLong>("Hit Count", hitCnt));
-        elems.add(new StatElement<AtomicLong>("Miss Count", missCnt));
-
-        stats.setStatElements( elems );
-
         return stats;
     }
 

Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/soft/SoftReferenceMemoryCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/soft/SoftReferenceMemoryCache.java?rev=1775170&r1=1775169&r2=1775170&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/soft/SoftReferenceMemoryCache.java
(original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/memory/soft/SoftReferenceMemoryCache.java
Mon Dec 19 20:24:52 2016
@@ -21,15 +21,14 @@ package org.apache.commons.jcs.engine.me
 
 import java.io.IOException;
 import java.lang.ref.SoftReference;
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.commons.jcs.engine.CacheConstants;
 import org.apache.commons.jcs.engine.behavior.ICacheElement;
@@ -40,7 +39,6 @@ import org.apache.commons.jcs.engine.mem
 import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
 import org.apache.commons.jcs.engine.memory.util.SoftReferenceElementDescriptor;
 import org.apache.commons.jcs.engine.stats.StatElement;
-import org.apache.commons.jcs.engine.stats.Stats;
 import org.apache.commons.jcs.engine.stats.behavior.IStatElement;
 import org.apache.commons.jcs.engine.stats.behavior.IStats;
 import org.apache.commons.logging.Log;
@@ -70,15 +68,6 @@ public class SoftReferenceMemoryCache<K,
      */
     private LinkedBlockingQueue<ICacheElement<K, V>> strongReferences;
 
-    /** number of hits */
-    private AtomicLong hitCnt;
-
-    /** number of misses */
-    private AtomicLong missCnt;
-
-    /** number of puts */
-    private AtomicLong putCnt;
-
     /**
      * For post reflection creation initialization
      * <p>
@@ -89,9 +78,6 @@ public class SoftReferenceMemoryCache<K,
     {
         super.initialize( hub );
         strongReferences = new LinkedBlockingQueue<ICacheElement<K, V>>();
-        hitCnt = new AtomicLong(0);
-        missCnt = new AtomicLong(0);
-        putCnt = new AtomicLong(0);
         log.info( "initialized Soft Reference Memory Cache for " + getCacheName() );
     }
 
@@ -149,21 +135,13 @@ public class SoftReferenceMemoryCache<K,
     @Override
     public IStats getStatistics()
     {
-        ArrayList<IStatElement<?>> elems = new ArrayList<IStatElement<?>>();
-
-        int size = getSize();
-        int emptyrefs = map.size() - size;
+        IStats stats = super.getStatistics();
+        stats.setTypeName("Soft Reference Memory Cache");
 
-        elems.add(new StatElement<Integer>("Size", Integer.valueOf(size)));
+        List<IStatElement<?>> elems = stats.getStatElements();
+        int emptyrefs = map.size() - getSize();
         elems.add(new StatElement<Integer>("Empty References", Integer.valueOf(emptyrefs)));
         elems.add(new StatElement<Integer>("Strong References", Integer.valueOf(strongReferences.size())));
-        elems.add(new StatElement<AtomicLong>("Put Count", putCnt));
-        elems.add(new StatElement<AtomicLong>("Hit Count", hitCnt));
-        elems.add(new StatElement<AtomicLong>("Miss Count", missCnt));
-
-        IStats stats = new Stats();
-        stats.setTypeName("Soft Reference Memory Cache");
-        stats.setStatElements(elems);
 
         return stats;
     }
@@ -358,21 +336,6 @@ public class SoftReferenceMemoryCache<K,
     }
 
     /**
-     * Prepares for shutdown.
-     * <p>
-     * @throws IOException
-     */
-    @Override
-    public void dispose() throws IOException
-    {
-        super.dispose();
-        removeAll();
-        hitCnt.set(0);
-        missCnt.set(0);
-        putCnt.set(0);
-    }
-
-    /**
      * This can't be implemented.
      * <p>
      * @param numberToFree



Mime
View raw message