Below are the CVS diffs of what I think the CachedObject,
RefreshableCachedObject, and TurbineGlobalCacheService should look like.
Basically, I sync'd the get and set methods since they access fields that
are not garunteed to be atomic, and I put the loop in
TurbineGlobalCacheService's clearCache() in a sync block (see previous
post). I also added a protected method to CahedObject to reset the creation
time so RefreshableCachedObject does not have to directly manipulate those
fields itself. IMHO, creation time should not be manipulated this way. There
should be another field in RefreshableCachedObject to keep track of the
current startTime for expiration, but this is trivial. Feedback would be
much appreciated.

Chris Campbell
[EMAIL PROTECTED]


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/05 17:56:49
@@ -137,10 +137,20 @@
      *
      * @return When the object was created.
      */
-    public long getCreated()
+    public synchronized long getCreated()
     {
         return created;
     }
+
+    /**
+     * Resets creation time for the object.
+     *
+     * @return void
+     */
+    protected synchronized void resetCreated()
+    {
+        this.created = created + expires;
+    }

     /**
      * Returns the expiration time for the object.
@@ -157,7 +167,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 +177,7 @@
      *
      * @return Whether the object is stale or not.
      */
-    public boolean getStale()
+    public synchronized boolean getStale()
     {
         return stale;
     }
@@ -177,7 +187,7 @@
      *
      * @return True if the object is stale.
      */
-    public boolean isStale()
+    public synchronized boolean isStale()
     {
         setStale( (System.currentTimeMillis() - created) > expires );
         return getStale();



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/05 17:58:03
@@ -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;
@@ -157,6 +157,7 @@
         {
             r.refresh();
         }
-        created = created + getExpires();
+        resetCreated();
+
     }
 }



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/05 17:58:48
@@ -92,6 +92,9 @@
     /** cacheCheckFrequency (default - 5 seconds) */
     private long cacheCheckFrequency =
         TurbineResources.getLong("cachedobject.cacheCheckFrequency", 5000);
+
+    /** Initial capacity */
+    private int initCapacity = 20;

     /**
      * Constructor.
@@ -113,7 +116,7 @@
         try
         {
             org.apache.turbine.util.Log.note ("TurbineGlobalCacheService
not init()....starting!");
-            cache = new Hashtable(20);
+            cache = new Hashtable(initCapacity);

             // Start housekeeping thread.
             Thread housekeeping = new Thread(this);
@@ -212,22 +215,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(initCapacity);
+        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 for
performance
+                        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();
+        }
     }
+
 }




------------------------------------------------------------
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