Author: doogie
Date: Thu Apr  1 04:51:14 2010
New Revision: 929845

URL: http://svn.apache.org/viewvc?rev=929845&view=rev
Log:
Remove SoftReferences automatically in the background.  This is the
final step in removing all polling code patterns from UtilCache.
Because of this, all helper methods that were used to clear elements
programmatically are now officially deprecated.

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheLine.java
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheSoftReference.java
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/HardRefCacheLine.java
    
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/SoftRefCacheLine.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/UtilCacheEvents.java
    ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
    ofbiz/trunk/framework/webtools/webapp/webtools/cache/findUtilCache.ftl

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheLine.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheLine.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheLine.java 
(original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheLine.java Thu 
Apr  1 04:51:14 2010
@@ -37,7 +37,6 @@ public abstract class CacheLine<V> exten
         return this.expireTimeNanos - loadTimeNanos - expireTimeNanos != 0;
     }
     public abstract V getValue();
-    public abstract boolean isInvalid();
 
     void cancel() {
     }

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheSoftReference.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheSoftReference.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheSoftReference.java
 (original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/CacheSoftReference.java
 Thu Apr  1 04:51:14 2010
@@ -18,14 +18,13 @@
  
*******************************************************************************/
 package org.ofbiz.base.util.cache;
 
-import java.lang.ref.SoftReference;
-import java.lang.ref.ReferenceQueue;
 import java.io.Serializable;
 
 import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.ReferenceCleaner;
 
 @SuppressWarnings("serial")
-public class CacheSoftReference<V> extends SoftReference<V> implements 
Serializable {
+public abstract class CacheSoftReference<V> extends ReferenceCleaner.Soft<V> 
implements Serializable {
 
     public static final String module = CacheSoftReference.class.getName();
 
@@ -33,10 +32,6 @@ public class CacheSoftReference<V> exten
         super(o);
     }
 
-    public CacheSoftReference(V o, ReferenceQueue<? super V> referenceQueue) {
-        super(o, referenceQueue);
-    }
-
     @Override
     public void clear() {
         if (Debug.verboseOn()) {

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/HardRefCacheLine.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/HardRefCacheLine.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/HardRefCacheLine.java 
(original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/HardRefCacheLine.java 
Thu Apr  1 04:51:14 2010
@@ -31,9 +31,4 @@ public abstract class HardRefCacheLine<V
     public V getValue() {
         return value;
     }
-
-    @Override
-    public boolean isInvalid() {
-        return value == null;
-    }
 }

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/SoftRefCacheLine.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/SoftRefCacheLine.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/SoftRefCacheLine.java 
(original)
+++ 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/SoftRefCacheLine.java 
Thu Apr  1 04:51:14 2010
@@ -24,16 +24,20 @@ public abstract class SoftRefCacheLine<V
 
     public SoftRefCacheLine(V value, long loadTimeNanos, long expireTimeNanos) 
{
         super(loadTimeNanos, expireTimeNanos);
-        this.ref = new CacheSoftReference<V>(value);
+        this.ref = new CacheSoftReference<V>(value) {
+            public void remove() {
+                SoftRefCacheLine.this.remove();
+            }
+        };
     }
 
     @Override
-    public V getValue() {
-        return ref.get();
+    void cancel() {
+        ref.clear();
     }
 
     @Override
-    public boolean isInvalid() {
-        return ref.get() == null;
+    public V getValue() {
+        return ref.get();
     }
 }

Modified: 
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java 
(original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java Thu 
Apr  1 04:51:14 2010
@@ -416,10 +416,6 @@ public class UtilCache<K, V> implements 
             } else {
                 missCountNotFound.incrementAndGet();
             }
-        } else if (line.isInvalid()) {
-            removeInternal(key, line);
-            if (countGet) missCountSoftRef.incrementAndGet();
-            line = null;
         } else {
             if (countGet) hitCount.incrementAndGet();
         }
@@ -791,9 +787,6 @@ public class UtilCache<K, V> implements 
                 }
             }
             return false;
-        } else if (line.isInvalid()) {
-            removeInternal(key, false);
-            return false;
         } else {
             return true;
         }
@@ -876,24 +869,19 @@ public class UtilCache<K, V> implements 
      *
      * @param key The key for the element, used to reference it in the 
hastables and LRU linked list
      * @return True is the element corresponding to the specified key has 
expired, otherwise false
+     * @deprecated elements are automatically expired in the background
      */
+    @Deprecated
     public boolean hasExpired(Object key) {
-        CacheLine<V> line = memoryTable.get(fromKey(key));
-        if (line == null) return false;
-        return line.isInvalid();
+        return !memoryTable.containsKey(fromKey(key));
     }
 
-    /** Clears all expired cache entries; also clear any cache entries where 
the SoftReference in the CacheLine object has been cleared by the gc */
+    /** Clears all expired cache entries; also clear any cache entries where 
the SoftReference in the CacheLine object has been cleared by the gc
+     * @deprecated entries are removed automatically now
+    */
+    @Deprecated
     public void clearExpired() {
-        Iterator<Map.Entry<Object, CacheLine<V>>> it = 
memoryTable.entrySet().iterator();
-        while (it.hasNext()) {
-            Map.Entry<Object, CacheLine<V>> entry = it.next();
-            CacheLine<V> line = entry.getValue();
-            if (line.isInvalid()) {
-                it.remove();
-                postRemove(toKey(entry.getKey()), line.getValue(), false);
-            }
-        }
+        // do nothing, expired values are removed automatically in the 
background
     }
 
     /** Send a key addition event to all registered listeners */
@@ -927,12 +915,12 @@ public class UtilCache<K, V> implements 
         listeners.remove(listener);
     }
 
-    /** Clears all expired cache entries from all caches */
+    /** Clears all expired cache entries from all caches
+     * @deprecated entries are removed automatically now
+    */
+    @Deprecated
     public static void clearExpiredFromAllCaches() {
-        // We make a copy since clear may take time
-        for (UtilCache<?,?> utilCache : utilCacheTable.values()) {
-            utilCache.clearExpired();
-        }
+        // do nothing, expired values are removed automatically in the 
background
     }
 
     /** Checks for a non-expired key in a specific cache */

Modified: 
ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/UtilCacheEvents.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/UtilCacheEvents.java?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/UtilCacheEvents.java 
(original)
+++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/UtilCacheEvents.java 
Thu Apr  1 04:51:14 2010
@@ -168,28 +168,6 @@ public class UtilCacheEvents {
         return "success";
     }
 
-    /** An HTTP WebEvent handler that clears all caches
-     * @param request The HTTP request object for the current JSP or Servlet 
request.
-     * @param response The HTTP response object for the current JSP or Servlet 
request.
-     * @return
-     */
-    public static String clearAllExpiredEvent(HttpServletRequest request, 
HttpServletResponse response) {
-        String errMsg = "";
-        Locale locale = UtilHttp.getLocale(request);
-
-        Security security = (Security) request.getAttribute("security");
-        if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
-            errMsg = UtilProperties.getMessage(UtilCacheEvents.err_resource, 
"utilCacheEvents.permissionEdit", locale) + ".";
-            request.setAttribute("_ERROR_MESSAGE_", errMsg);
-            return "error";
-        }
-
-        UtilCache.clearExpiredFromAllCaches();
-        errMsg = UtilProperties.getMessage(UtilCacheEvents.err_resource, 
"utilCache.clearAllExpiredElements", locale) + ".";
-        request.setAttribute("_EVENT_MESSAGE_", errMsg);
-        return "success";
-    }
-
     /** An HTTP WebEvent handler that updates the named cache
      * @param request The HTTP request object for the current JSP or Servlet 
request.
      * @param response The HTTP response object for the current JSP or Servlet 
request.

Modified: ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml 
(original)
+++ ofbiz/trunk/framework/webtools/webapp/webtools/WEB-INF/controller.xml Thu 
Apr  1 04:51:14 2010
@@ -188,12 +188,6 @@ under the License.
         <response name="success" type="view" value="FindUtilCache"/>
         <response name="error" type="view" value="FindUtilCache"/>
     </request-map>
-    <request-map uri="FindUtilCacheClearAllExpired">
-        <security https="true" auth="true"/>
-        <event type="java" path="org.ofbiz.webtools.UtilCacheEvents" 
invoke="clearAllExpiredEvent"/>
-        <response name="success" type="view" value="FindUtilCache"/>
-        <response name="error" type="view" value="FindUtilCache"/>
-    </request-map>
     <request-map uri="ForceGarbageCollection">
         <security https="true" auth="true"/>
         <event type="service" invoke="forceGarbageCollection"/>

Modified: ofbiz/trunk/framework/webtools/webapp/webtools/cache/findUtilCache.ftl
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/webapp/webtools/cache/findUtilCache.ftl?rev=929845&r1=929844&r2=929845&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/webapp/webtools/cache/findUtilCache.ftl 
(original)
+++ ofbiz/trunk/framework/webtools/webapp/webtools/cache/findUtilCache.ftl Thu 
Apr  1 04:51:14 2010
@@ -21,7 +21,6 @@ under the License.
     <div class="button-bar">
         <a href="<@ofbizUrl>FindUtilCache</@ofbizUrl>" class="buttontext 
refresh">${uiLabelMap.WebtoolsReloadCacheList}</a>
         <a href="<@ofbizUrl>FindUtilCacheClearAll</@ofbizUrl>" 
class="buttontext">${uiLabelMap.WebtoolsClearAllCaches}</a>
-        <a href="<@ofbizUrl>FindUtilCacheClearAllExpired</@ofbizUrl>" 
class="buttontext">${uiLabelMap.WebtoolsClearExpiredFromAll}</a>
         <a href="<@ofbizUrl>ForceGarbageCollection</@ofbizUrl>" 
class="buttontext">${uiLabelMap.WebtoolsRunGC}</a>
     </div>
 </#macro>


Reply via email to