You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2002/01/30 17:11:21 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/cache CachedObject.java GlobalCacheService.java RefreshableCachedObject.java TurbineGlobalCacheService.java

dlr         02/01/30 08:11:21

  Modified:    src/java/org/apache/turbine/services/cache CachedObject.java
                        GlobalCacheService.java
                        RefreshableCachedObject.java
                        TurbineGlobalCacheService.java
  Log:
  Patch by Paul Spencer <pa...@apache.org>:
  
  The following patch was prompted by work in the Jetspeed
  project. Below is a list of the fixes and enhancement included.
  
  o Fixed - TurbineGlobalCachedService now uses property
     services.GlobalCacheService.cache.check.frequency
     See Note 1
  
  o Added - TurbineGlobalCachedService now uses property
     services.GlobalCacheService.cache.initial.size to when initializing
     heaptable.
     See Note 2
  
  o Added - TurbineGlobalCachedService tries to refresh an
     RefreshableCachedObject that is expired and touched object in
     getObject()
     See Note 3
  
  o Added - TurbineGlobalCachedService and GlobalCachedServices now allows
     the entire cache to be flushed with the flushCache().
     See Note 4
  
  o Fixed - RefreshableCachedObject set the field created to the
     current time before refreshing the object.
     See Note 5
  
  o Added - CachedObject now allows the expiration time to be set.
     See Note 5
  
  Note 1: The value of property was not used.  Looks like a bug.
  
  Note 2: This allows for a larger initial cache size to minimize
  re-sizing during runtime.
  
  Note 3: RefreshableCachedObject where refreshed during clearCache(),
  but not during getObject().  This meant that the application must
  re-add the object to cache in the event the object expired after the
  last clearCache().
  
  Note 4: The only to clear cache is to restart the application.
  
  Note 5: Refreshable content may not be refreshed at regular
  intervals. Nor will the time required to complete the refresh be
  consistent.
  
  Revision  Changes    Path
  1.2       +19 -1     jakarta-turbine-2/src/java/org/apache/turbine/services/cache/CachedObject.java
  
  Index: CachedObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/cache/CachedObject.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -u -r1.1 -r1.2
  --- CachedObject.java	16 Aug 2001 05:08:49 -0000	1.1
  +++ CachedObject.java	30 Jan 2002 16:11:21 -0000	1.2
  @@ -63,7 +63,7 @@
    *
    * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
  - * @version $Id: CachedObject.java,v 1.1 2001/08/16 05:08:49 jvanzyl Exp $
  + * @version $Id: CachedObject.java,v 1.2 2002/01/30 16:11:21 dlr Exp $
    */
   public class CachedObject
       implements java.io.Serializable
  @@ -153,6 +153,24 @@
       public long getExpires()
       {
           return expires;
  +    }
  +
  +    /**
  +     * Set the expiration interval for the object.
  +     *
  +     * @param expires Expiration interval in millis ( 1 second = 1000 millis)
  +     */
  +    public void setExpires( long expires ) {
  +        if ( expires == DEFAULT ) {
  +            this.expires = defaultage;
  +        } else {
  +            this.expires = expires;
  +        }
  +        if (expires == FOREVER) {
  +            setStale(false);
  +        } else {
  +            setStale( (System.currentTimeMillis() - created) > expires );
  +        }
       }
   
       /**
  
  
  
  1.3       +6 -1      jakarta-turbine-2/src/java/org/apache/turbine/services/cache/GlobalCacheService.java
  
  Index: GlobalCacheService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/cache/GlobalCacheService.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- GlobalCacheService.java	4 Jan 2002 02:01:47 -0000	1.2
  +++ GlobalCacheService.java	30 Jan 2002 16:11:21 -0000	1.3
  @@ -61,7 +61,7 @@
    * GlobalCacheService interface.
    *
    * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
  - * @version $Id: GlobalCacheService.java,v 1.2 2002/01/04 02:01:47 dlr Exp $
  + * @version $Id: GlobalCacheService.java,v 1.3 2002/01/30 16:11:21 dlr Exp $
    */
   public interface GlobalCacheService
       extends Service
  @@ -106,4 +106,9 @@
        * @return int The current number of objects in the cache.
        */
       public int getNumberOfObjects();
  +
  +    /**
  +     * Flush the cache of all objects.
  +     */
  +    public void flushCache();
   }
  
  
  
  1.2       +2 -2      jakarta-turbine-2/src/java/org/apache/turbine/services/cache/RefreshableCachedObject.java
  
  Index: RefreshableCachedObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/cache/RefreshableCachedObject.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -u -r1.1 -r1.2
  --- RefreshableCachedObject.java	16 Aug 2001 05:08:50 -0000	1.1
  +++ RefreshableCachedObject.java	30 Jan 2002 16:11:21 -0000	1.2
  @@ -67,7 +67,7 @@
    * cached object, and resetting its expire time.
    *
    * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
  - * @version $Id: RefreshableCachedObject.java,v 1.1 2001/08/16 05:08:50 jvanzyl Exp $
  + * @version $Id: RefreshableCachedObject.java,v 1.2 2002/01/30 16:11:21 dlr Exp $
    */
   public class RefreshableCachedObject
       extends CachedObject
  @@ -147,8 +147,8 @@
           Refreshable r = (Refreshable)getContents();
           synchronized (this)
           {
  +            created = System.currentTimeMillis();
               r.refresh();
  -            created = created + getExpires();
           }
       }
   }
  
  
  
  1.2       +102 -21   jakarta-turbine-2/src/java/org/apache/turbine/services/cache/TurbineGlobalCacheService.java
  
  Index: TurbineGlobalCacheService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/cache/TurbineGlobalCacheService.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -u -r1.1 -r1.2
  --- TurbineGlobalCacheService.java	16 Aug 2001 05:08:50 -0000	1.1
  +++ TurbineGlobalCacheService.java	30 Jan 2002 16:11:21 -0000	1.2
  @@ -63,6 +63,7 @@
   import org.apache.turbine.services.InitializationException;
   import org.apache.turbine.services.TurbineBaseService;
   import org.apache.turbine.services.resources.TurbineResources;
  +import org.apache.velocity.runtime.configuration.Configuration;
   
   /**
    * This Service functions as a Global Cache.  A global cache is a good
  @@ -74,23 +75,62 @@
    * information in the Global Cache and decrease the overhead of
    * hitting the database everytime you need State information.
    *
  + * The following properties are needed to configure this service:<br>
  + *
  + * <code><pre>
  + * services.GlobalCacheService.classname=org.apache.turbine.services.cache.TurbineGlobalCacheService
  + * services.GlobalCacheService.cache.initial.size=20
  + * services.GlobalCacheService.cache.check.frequency=5000
  + * </pre></code>
  + *
  + * <dl>
  + * <dt>classname</dt><dd>the classname of this service</dd>
  + * <dt>cache.initial.size/dt><dd>Initial size of hash table use to store cached 
  + objects.  If this property is not present, the default value is 20</dd>
  + * <dt>cache.check.frequency</dt><dd>Cache check frequency in Millis (1000 
  + Millis = 1 second).  If this property is not present, the default value is 5000</dd>
  + * </dl>
    * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
    * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
    * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
  - * @version $Id: TurbineGlobalCacheService.java,v 1.1 2001/08/16 05:08:50 jvanzyl Exp $
  + * @version $Id: TurbineGlobalCacheService.java,v 1.2 2002/01/30 16:11:21 dlr Exp $
    */
   public class TurbineGlobalCacheService
       extends TurbineBaseService
       implements GlobalCacheService,
                  Runnable
   {
  +    /**
  +     * Initial size of hash table
  +     * Value must be > 0.
  +     * Default = 20
  +     */
  +    public static final int DEFAULT_INITIAL_CACHE_SIZE = 20;
  +
  +    /**
  +     * The property for the InitalCacheSize
  +     */
  +    public static final String INITIAL_CACHE_SIZE = "cache.initial.size";
  +
  +    /**
  +     * The property for the Cache check frequency
  +     */
  +    public static final String CACHE_CHECK_FREQUENCY = "cache.check.frequency";
  +
  +    /**
  +     * Cache check frequency in Millis (1000 Millis = 1 second).
  +     * Value must be > 0.
  +     * Default = 5 seconds
  +     */
  +    public static final long DEFAULT_CACHE_CHECK_FREQUENCY = 
  +      TurbineResources.getLong("cache.check.frequency", 5000 ); // 5 seconds
  +
       /** The cache. **/
       private Hashtable cache = null;
   
       /** cacheCheckFrequency (default - 5 seconds) */
  -    private long cacheCheckFrequency =
  -        TurbineResources.getLong("cache.check.frequency", 5000);
  -
  +    private long cacheCheckFrequency = DEFAULT_CACHE_CHECK_FREQUENCY;
  +    
       /**
        * Constructor.
        */
  @@ -102,12 +142,29 @@
        * Called the first time the Service is used.
        */
       public void init()
  -        throws InitializationException
  -    {
  -        try
  -        {
  -            cache = new Hashtable(20);
  -
  +    throws InitializationException {
  +        int cacheInitialSize = DEFAULT_INITIAL_CACHE_SIZE;
  +        Configuration conf = getConfiguration();
  +        if (conf != null) {
  +            try {
  +                cacheInitialSize = conf.getInt(INITIAL_CACHE_SIZE, DEFAULT_INITIAL_CACHE_SIZE);
  +                if (cacheInitialSize <= 0) {
  +                    throw new IllegalArgumentException(INITIAL_CACHE_SIZE + " must be >0");
  +                }
  +                cacheCheckFrequency = conf.getLong(CACHE_CHECK_FREQUENCY, DEFAULT_CACHE_CHECK_FREQUENCY);
  +                if (cacheCheckFrequency <= 0) {
  +                    throw new IllegalArgumentException(CACHE_CHECK_FREQUENCY + " must be >0");
  +                }
  +            }
  +            catch (Exception x) {
  +                throw new InitializationException(
  +                "Failed to initialize TurbineGlobalCacheService",x);
  +            }
  +        }
  +        
  +        try {
  +            cache = new Hashtable(cacheInitialSize);
  +            
               // Start housekeeping thread.
               Thread housekeeping = new Thread(this);
               // Indicate that this is a system thread. JVM will quit only when there
  @@ -116,22 +173,21 @@
               // to terminate in an orderly manner.
               housekeeping.setDaemon(true);
               housekeeping.start();
  -
               setInit(true);
           }
  -        catch (Exception e)
  -        {
  +        catch (Exception e) {
               throw new InitializationException(
  -                "TurbineGlobalCacheService failed to initialize", e);
  +            "TurbineGlobalCacheService failed to initialize", e);
           }
       }
   
       /**
  -     * Returns an item from the cache.
  +     * Returns an item from the cache.  RefreshableCachedObject will be
  +     * refreshed if it is expired not untouched.
        *
        * @param id The key of the stored object.
        * @return The object from the cache.
  -     * @exception ObjectExpiredException, when either the object is
  +     * @exception ObjectExpiredException when either the object is
        * not in the cache or it has expired.
        */
       public CachedObject getObject(String id)
  @@ -148,10 +204,21 @@
               throw new ObjectExpiredException();
           }
   
  -        if (obj.isStale())
  -        {
  -            // Expired.
  -            throw new ObjectExpiredException();
  +        if (obj.isStale()) {
  +            if (obj instanceof RefreshableCachedObject) {
  +                RefreshableCachedObject rco = (RefreshableCachedObject) obj;
  +                if (rco.isUntouched())
  +                    // Do not refresh an object that has exceeded TimeToLive
  +                    throw new ObjectExpiredException();
  +                // Refresh Object
  +                rco.refresh();
  +                if (rco.isStale())
  +                    // Object is Expired.
  +                    throw new ObjectExpiredException();
  +            } else {
  +                // Expired.
  +                throw new ObjectExpiredException();
  +            }
           }
   
           if (obj instanceof RefreshableCachedObject)
  @@ -284,4 +351,18 @@
           int objectsize = baos.toByteArray().length - 4;
           return objectsize;
       }
  -}
  +    
  +    /**
  +     * Flush the cache of all objects.
  +     */
  +    public void flushCache() {
  +        
  +        synchronized (this) {
  +            for ( Enumeration e = cache.keys(); e.hasMoreElements(); ) {
  +                String key = (String) e.nextElement();
  +                CachedObject co = (CachedObject) cache.get(key);
  +                cache.remove(key);
  +            }
  +        }
  +    }
  + }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>