You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2007/10/17 22:10:20 UTC

svn commit: r585661 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache: CacheLine.java UtilCache.java

Author: doogie
Date: Wed Oct 17 13:10:19 2007
New Revision: 585661

URL: http://svn.apache.org/viewvc?rev=585661&view=rev
Log:
Use sub-classes of CacheLine to manage hard/soft values.  Closes
https://issues.apache.org/jira/browse/OFBIZ-1302

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java?rev=585661&r1=585660&r2=585661&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/CacheLine.java Wed Oct 17 13:10:19 2007
@@ -22,63 +22,21 @@
 
 import org.ofbiz.base.util.UtilObject;
 
-public class CacheLine implements Serializable {
+public abstract class CacheLine implements Serializable {
+    public long loadTime;
+    public final long expireTime;
 
-    public static final String module = CacheLine.class.getName();
-
-    public Object valueRef = null;
-    public long loadTime = 0;
-    public long expireTime = 0;
-    public boolean useSoftReference = false;
-
-    public CacheLine(Object value, boolean useSoftReference, long expireTime) {
-        if (useSoftReference) {
-            this.valueRef = new CacheSoftReference(value);
-        } else {
-            this.valueRef = value;
-        }
-        this.useSoftReference = useSoftReference;
-        this.expireTime = expireTime;
+    protected CacheLine(long expireTime) {
+        this(0, expireTime);
     }
 
-    public CacheLine(Object value, boolean useSoftReference, long loadTime, long expireTime) {
-        this(value, useSoftReference, expireTime);
+    protected CacheLine(long loadTime, long expireTime) {
+        this.expireTime = expireTime;
         this.loadTime = loadTime;
     }
 
-    public Object getValue() {
-        if (valueRef == null) return null;
-        if (useSoftReference) {
-            return ((CacheSoftReference) valueRef).get();
-        } else {
-            return valueRef;
-        }
-    }
-    
-    public boolean softReferenceCleared() {
-        if (!this.useSoftReference || valueRef == null) {
-            return false;
-        } else {
-            if (((CacheSoftReference) valueRef).get() == null) {
-                return true;
-            } else {
-                return false;
-            }
-        }
-    }
-
-    public void setUseSoftReference(boolean useSoftReference) {
-        if (this.useSoftReference != useSoftReference) {
-            synchronized (this) {
-                this.useSoftReference = useSoftReference;
-                if (useSoftReference) {
-                    this.valueRef = new CacheSoftReference(this.valueRef);
-                } else {
-                    this.valueRef = ((CacheSoftReference) this.valueRef).get();
-                }
-            }
-        }
-    }
+    public abstract Object getValue();
+    public abstract boolean isInvalid();
 
     public long getExpireTime() {
         return this.expireTime;
@@ -91,7 +49,7 @@
     public boolean hasExpired() {
         // check this BEFORE checking to see if expireTime <= 0, ie if time expiration is enabled
         // check to see if we are using softReference first, slight performance increase
-        if (softReferenceCleared()) return true;
+        if (isInvalid()) return true;
 
         // check if expireTime <= 0, ie if time expiration is not enabled
         if (expireTime <= 0) return false;

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java?rev=585661&r1=585660&r2=585661&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/cache/UtilCache.java Wed Oct 17 13:10:19 2007
@@ -291,11 +291,13 @@
             key = ObjectType.NULL;
         }
         CacheLine oldCacheLine;
+        CacheLine newCacheLine;
         if (expireTime > 0) {
-            oldCacheLine = (CacheLine) cacheLineTable.put(key, new CacheLine(value, useSoftReference, System.currentTimeMillis(), expireTime));
+            newCacheLine = useSoftReference ? new SoftRefCacheLine(value, System.currentTimeMillis(), expireTime) : new HardRefCacheLine(value, System.currentTimeMillis(), expireTime);
         } else {
-            oldCacheLine = (CacheLine) cacheLineTable.put(key, new CacheLine(value, useSoftReference, expireTime));
+            newCacheLine = useSoftReference ? new SoftRefCacheLine(value, expireTime) : new HardRefCacheLine(value, expireTime);
         }
+        oldCacheLine = (CacheLine) cacheLineTable.put(key, newCacheLine);
 
         if (oldCacheLine == null) {
             noteAddition(key, value);
@@ -334,7 +336,7 @@
         CacheLine line = getInternalNoCheck(key);
         if (line == null) {
             if (countGet) missCountNotFound++;
-        } else if (line.softReferenceCleared()) {
+        } else if (line.isInvalid()) {
             removeInternal(key, false);
             if (countGet) missCountSoftRef++;
             line = null;
@@ -529,10 +531,17 @@
     public void setUseSoftReference(boolean useSoftReference) {
         if (this.useSoftReference != useSoftReference) {
             this.useSoftReference = useSoftReference;
-            Iterator values = cacheLineTable.values().iterator();
-            while (values.hasNext()) {
-                CacheLine line = (CacheLine) values.next();
-                line.setUseSoftReference(useSoftReference);
+            Iterator keys = cacheLineTable.keySet().iterator();
+            while (keys.hasNext()) {
+                Object key = keys.next();
+                CacheLine line = (CacheLine) cacheLineTable.get(key);
+                if (useSoftReference) {
+                    if (line instanceof SoftRefCacheLine) continue;
+                    cacheLineTable.put(key, new SoftRefCacheLine(line.getValue(), line.loadTime, line.expireTime));
+                } else {
+                    if (line instanceof HardRefCacheLine) continue;
+                    cacheLineTable.put(key, new HardRefCacheLine(line.getValue(), line.loadTime, line.expireTime));
+                }
             }
         }
     }