I posted these diffs last week with no response. I basically just sync'd
some of the set and get methods in ChachedObject and
RefreshableCachedObject. I rewrote clearCache in TurbineGlobalCacheService:
sync'd during the enumeration over the Hash and refreshing of objects done
outside of the loop to avoid locking up the whole service while refreshing
one of its contents. Any comments?

Thanks,
Chris

(these diffs are slightly different than my last post)

Index: TurbineGlobalCacheService.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/services/cache/Tur
bineGlobalCacheService.java,v
retrieving revision 1.6
diff -u -r1.6 TurbineGlobalCacheService.java
--- TurbineGlobalCacheService.java 2000/10/03 19:50:27 1.6
+++ TurbineGlobalCacheService.java 2000/10/09 17:45:23
@@ -212,22 +212,39 @@
      */
     public void clearCache()
     {
-        for ( Enumeration e = cache.keys(); e.hasMoreElements(); )
+        // Sync on this object so that other threads do not
+        // change the Hashtable while enumerating over it.
+        Vector refreshThese = new Vector(20);
+        synchronized (this)
         {
-            String key = (String) e.nextElement();
-            CachedObject co = (CachedObject) cache.get(key);
-            if (co instanceof RefreshableCachedObject)
+            for ( Enumeration e = cache.keys(); e.hasMoreElements(); )
             {
-                RefreshableCachedObject rco = (RefreshableCachedObject) co;
-                if (rco.isUntouched())
-                    cache.remove ( key );
-                else if (rco.isStale())
-                    rco.refresh();
+                String key = (String) e.nextElement();
+                CachedObject co = (CachedObject) cache.get(key);
+                if (co instanceof RefreshableCachedObject)
+                {
+                    RefreshableCachedObject rco =
(RefreshableCachedObject)co;
+                    if (rco.isUntouched())
+                        cache.remove(key);
+                    else if (rco.isStale())
+                        // Do refreshing outside of sync block so as not
+                        // to prolong sync'ing the object
+                        refreshThese.addElement(key);
+                }
+                else if ( co.isStale() )
+                {
+                    cache.remove(key);
+                }
             }
-            else if ( co.isStale() )
-            {
-                cache.remove ( key );
-            }
+        }
+
+        // Now refresh the RefreshableCachedObjects outside of the sync
block
+        for ( Enumeration e = refreshThese.elements();
e.hasMoreElements(); )
+        {
+            String key = (String)e.nextElement();
+            CachedObject co = (CachedObject)cache.get(key);
+            RefreshableCachedObject rco = (RefreshableCachedObject)co;
+            rco.refresh();
         }
     }
 }

Index: RefreshableCachedObject.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/services/cache/Ref
reshableCachedObject.java,v
retrieving revision 1.3
diff -u -r1.3 RefreshableCachedObject.java
--- RefreshableCachedObject.java 2000/10/03 19:50:27 1.3
+++ RefreshableCachedObject.java 2000/10/09 17:44:52
@@ -119,15 +119,15 @@
     }

     /** sets the timeToLive member (in milliseconds) */
-    public void setTTL(long l) { timeToLive = l; }
+    public synchronized void setTTL(long l) { timeToLive = l; }

     /** gets the timeToLive member (in milliseconds) */
-    public long getTTL() { return timeToLive; }
+    public synchronized long getTTL() { return timeToLive; }

     /**
      * Sets the last acccess time to the current time.
      */
-    public void touch()
+    public synchronized void touch()
     {
         lastAccess = System.currentTimeMillis();
     }
@@ -136,7 +136,7 @@
      * Returns true if the object hasn't been touched
      * in the previous TTL period.
      */
-    public boolean isUntouched()
+    public synchronized boolean isUntouched()
     {
         if (timeToLive < 0)
             return false;
@@ -156,7 +156,7 @@
         synchronized (this)
         {
             r.refresh();
+            created = created + getExpires();
         }
-        created = created + getExpires();
     }
 }

Index: CachedObject.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/services/cache/Cac
hedObject.java,v
retrieving revision 1.3
diff -u -r1.3 CachedObject.java
--- CachedObject.java 2000/08/29 22:27:34 1.3
+++ CachedObject.java 2000/10/09 17:50:15
@@ -157,7 +157,7 @@
      *
      * @param stale Whether the object is stale or not.
      */
-    public void setStale ( boolean stale )
+    public synchronized void setStale ( boolean stale )
     {
         this.stale = stale;
     }
@@ -167,7 +167,7 @@
      *
      * @return Whether the object is stale or not.
      */
-    public boolean getStale()
+    public synchronized boolean getStale()
     {
         return stale;
     }
@@ -177,7 +177,7 @@
      *
      * @return True if the object is stale.
      */
-    public boolean isStale()
+    public synchronized boolean isStale()
     {
         setStale( (System.currentTimeMillis() - created) > expires );
         return getStale();





------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to