You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2008/06/22 11:31:47 UTC
svn commit: r670326 [1/2] - in /turbine/fulcrum/trunk/cache/src:
java/org/apache/fulcrum/cache/ java/org/apache/fulcrum/cache/impl/
test/org/apache/fulcrum/cache/
Author: tv
Date: Sun Jun 22 02:31:47 2008
New Revision: 670326
URL: http://svn.apache.org/viewvc?rev=670326&view=rev
Log:
Major code cleanup, formatting and styling
Modified:
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/ObjectExpiredException.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/Refreshable.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/JCSCacheService.java
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/CachedObject.java Sun Jun 22 02:31:47 2008
@@ -22,20 +22,21 @@
import java.io.Serializable;
/**
- * Wrapper for an object you want to store in a cache for a period of
- * time.
- *
- * @todo The old Turbine version you could set the default age from Turbine. What
- * we need is a CachedObjectFactory that would generate CachedObject's that could then
- * have their default age set.
+ * Wrapper for an object you want to store in a cache for a period of time.
+ *
* @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id$
*/
-public class CachedObject
- implements Serializable
+
+public class CachedObject implements Serializable
{
+ /*
+ * TODO: The old Turbine version you could set the default age from Turbine.
+ * What we need is a CachedObjectFactory that would generate CachedObject's
+ * that could then have their default age set.
+ */
/**
* Serialization key
@@ -54,42 +55,42 @@
/** Default age (30 minutes). */
private long defaultage = 1800000;
- /** When created. **/
+ /** When created. * */
protected long created = 0;
- /** When it expires. **/
+ /** When it expires. * */
private long expires = 0;
/** Is this object stale/expired? */
private boolean stale = false;
-
/**
- * Constructor; sets the object to expire in the default time (30
- * minutes).
- *
- * @param o The object you want to cache.
+ * Constructor; sets the object to expire in the default time (30 minutes).
+ *
+ * @param o
+ * The object you want to cache.
*/
public CachedObject(Object o)
{
this.contents = o;
- this.expires = defaultage;
+ this.expires = this.defaultage;
this.created = System.currentTimeMillis();
}
/**
* Constructor.
- *
- * @param o The object to cache.
- * @param expires How long before the object expires, in ms,
- * e.g. 1000 = 1 second.
+ *
+ * @param o
+ * The object to cache.
+ * @param expires
+ * How long before the object expires, in ms, e.g. 1000 = 1
+ * second.
*/
- public CachedObject(Object o,
- long expires)
+ public CachedObject(Object o, long expires)
{
- if ( expires == DEFAULT )
+ if (expires == DEFAULT)
{
- this.expires = defaultage;
+ this.expires = this.defaultage;
}
this.contents = o;
@@ -99,44 +100,45 @@
/**
* Returns the cached object.
- *
+ *
* @return The cached object.
*/
public Object getContents()
{
- return contents;
+ return this.contents;
}
/**
* Returns the creation time for the object.
- *
+ *
* @return When the object was created.
*/
public long getCreated()
{
- return created;
+ return this.created;
}
/**
* Returns the expiration time for the object.
- *
+ *
* @return When the object expires.
*/
public long getExpires()
{
- return expires;
+ return this.expires;
}
/**
* Set the expiration interval for the object.
- *
- * @param expires Expiration interval in millis ( 1 second = 1000 millis)
+ *
+ * @param expires
+ * Expiration interval in millis ( 1 second = 1000 millis)
*/
public void setExpires(long expires)
{
if (expires == DEFAULT)
{
- this.expires = defaultage;
+ this.expires = this.defaultage;
}
else
{
@@ -148,43 +150,44 @@
}
else
{
- setStale((System.currentTimeMillis() - created) > expires);
+ setStale((System.currentTimeMillis() - this.created) > expires);
}
}
/**
* Set the stale status for the object.
- *
- * @param stale Whether the object is stale or not.
+ *
+ * @param stale
+ * Whether the object is stale or not.
*/
- public synchronized void setStale ( boolean stale )
+ public synchronized void setStale(boolean stale)
{
this.stale = stale;
}
/**
* Get the stale status for the object.
- *
+ *
* @return Whether the object is stale or not.
*/
public synchronized boolean getStale()
{
- return stale;
+ return this.stale;
}
/**
* Is the object stale?
- *
+ *
* @return True if the object is stale.
*/
public synchronized boolean isStale()
{
- if(expires == FOREVER)
+ if (this.expires == FOREVER)
{
return false;
}
- setStale( (System.currentTimeMillis() - created) > expires );
+ setStale((System.currentTimeMillis() - this.created) > this.expires);
return getStale();
}
}
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java Sun Jun 22 02:31:47 2008
@@ -35,59 +35,62 @@
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
+
/**
- * This Service functions as a Global Cache. A global cache is a good
- * place to store items that you may need to access often but don't
- * necessarily need (or want) to fetch from the database everytime. A
- * good example would be a look up table of States that you store in a
- * database and use throughout your application. Since information
- * about States doesn't change very often, you could store this
- * information in the Global Cache and decrease the overhead of
+ * This Service functions as a Global Cache. A global cache is a good place to
+ * store items that you may need to access often but don't necessarily need (or
+ * want) to fetch from the database everytime. A good example would be a look up
+ * table of States that you store in a database and use throughout your
+ * application. Since information about States doesn't change very often, you
+ * could store this information in the Global Cache and decrease the overhead of
* hitting the database everytime you need State information.
- *
+ *
* @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>
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
- * * @version $Id$
+ * @version $Id$
*/
-public class DefaultGlobalCacheService
- extends AbstractLogEnabled
- implements GlobalCacheService, Runnable, Configurable, Initializable, Disposable, ThreadSafe
+public class DefaultGlobalCacheService extends AbstractLogEnabled implements
+ GlobalCacheService, Runnable, Configurable, Initializable, Disposable,
+ ThreadSafe
{
/**
- * Initial size of hash table
- * Value must be > 0.
- * Default = 20
- */
+ * 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
- */
+ * The property for the InitalCacheSize
+ */
public static final String INITIAL_CACHE_SIZE = "cacheInitialSize";
+
/**
- * The property for the Cache check frequency
- */
+ * The property for the Cache check frequency
+ */
public static final String CACHE_CHECK_FREQUENCY = "cacheCheckFrequency";
+
/**
- * Cache check frequency in Millis (1000 Millis = 1 second).
- * Value must be > 0.
- * Default = 5 seconds
- */
+ * Cache check frequency in Millis (1000 Millis = 1 second). Value must be >
+ * 0. Default = 5 seconds
+ */
public static final long DEFAULT_CACHE_CHECK_FREQUENCY = 5000; // 5 seconds
- /** The cache. **/
+
+ /** The cache. * */
protected Hashtable cache = null;
+
/**
* cacheCheckFrequency (default - 5 seconds)
*/
private long cacheCheckFrequency;
- /**
- * cacheInitialSize (default - 20)
- */
- private int cacheInitialSize;
+ /**
+ * cacheInitialSize (default - 20)
+ */
+ private int cacheInitialSize;
+
/** thread for removing stale items from the cache */
private Thread housekeeping;
@@ -95,31 +98,31 @@
private boolean continueThread;
/**
- * @return
+ * Get the Cache Check Frequency in milliseconds
+ *
+ * @return the time between two cache check runs in milliseconds
*/
public long getCacheCheckFrequency()
{
- return cacheCheckFrequency;
+ return this.cacheCheckFrequency;
}
- public DefaultGlobalCacheService()
- {
- }
- /**
- * Returns an item from the cache.
/**
- * Returns an item from the cache. RefreshableCachedObject will be
- * refreshed if it is expired and not untouched.
- *
- * @param id The key of the stored object.
+ * Returns an item from the cache. /** Returns an item from the cache.
+ * RefreshableCachedObject will be refreshed if it is expired and not
+ * untouched.
+ *
+ * @param id
+ * The key of the stored object.
* @return The object from the cache.
- * @exception ObjectExpiredException, when either the object is
- * not in the cache or it has expired.
+ * @exception ObjectExpiredException,
+ * when either the object is not in the cache or it has
+ * expired.
*/
public CachedObject getObject(String id) throws ObjectExpiredException
{
CachedObject obj = null;
- obj = (CachedObject) cache.get(id);
+ obj = (CachedObject) this.cache.get(id);
if (obj == null)
{
// Not in the cache.
@@ -130,12 +133,16 @@
if (obj instanceof RefreshableCachedObject)
{
RefreshableCachedObject rco = (RefreshableCachedObject) obj;
- if (rco.isUntouched()) // Do not refresh an object that has exceeded TimeToLive
+ if (rco.isUntouched())
+ {
throw new ObjectExpiredException();
+ }
// Refresh Object
rco.refresh();
- if (rco.isStale()) // Object is Expired.
+ if (rco.isStale())
+ {
throw new ObjectExpiredException();
+ }
}
else
{
@@ -151,107 +158,123 @@
}
return obj;
}
+
/**
* Adds an object to the cache.
- *
- * @param id The key to store the object by.
- * @param o The object to cache.
+ *
+ * @param id
+ * The key to store the object by.
+ * @param o
+ * The object to cache.
*/
public void addObject(String id, CachedObject o)
{
// If the cache already contains the key, remove it and add
// the fresh one.
- if (cache.containsKey(id))
+ if (this.cache.containsKey(id))
{
- cache.remove(id);
+ this.cache.remove(id);
}
- cache.put(id, o);
+ this.cache.put(id, o);
}
+
/**
* Removes an object from the cache.
- *
- * @param id The String id for the object.
+ *
+ * @param id
+ * The String id for the object.
*/
public void removeObject(String id)
{
- cache.remove(id);
+ this.cache.remove(id);
}
/**
* Returns a copy of keys to objects in the cache as a list.
- *
+ *
* Note that keys to expired objects are not returned.
- *
- * @return A List of <code>String</code>'s representing the keys to objects
- * in the cache.
- */
- public List getKeys() {
- ArrayList keys = new ArrayList(cache.size());
- synchronized (this) {
- for (Iterator itr = cache.keySet().iterator(); itr.hasNext();)
+ *
+ * @return A List of <code>String</code>'s representing the keys to
+ * objects in the cache.
+ */
+ public List getKeys()
+ {
+ ArrayList keys = new ArrayList(this.cache.size());
+ synchronized (this)
+ {
+ for (Iterator itr = this.cache.keySet().iterator(); itr.hasNext();)
{
String key = (String) itr.next();
- try {
- /* CachedObject obj = */ getObject(key);
- } catch (ObjectExpiredException oee) {
+ try
+ {
+ /* CachedObject obj = */getObject(key);
+ }
+ catch (ObjectExpiredException oee)
+ {
// this is OK we just do not want this key
continue;
}
keys.add(new String(key));
}
}
- return (List)keys;
+ return keys;
}
/**
- * Returns a copy of the non-expired CachedObjects
- * in the cache as a list.
- *
- * @return A List of <code>CachedObject</code> objects
- * held in the cache
+ * Returns a copy of the non-expired CachedObjects in the cache as a list.
+ *
+ * @return A List of <code>CachedObject</code> objects held in the cache
*/
- public List getCachedObjects() {
- ArrayList objects = new ArrayList(cache.size());
- synchronized (this) {
- for (Iterator itr = cache.keySet().iterator(); itr.hasNext();)
+ public List getCachedObjects()
+ {
+ ArrayList objects = new ArrayList(this.cache.size());
+ synchronized (this)
+ {
+ for (Iterator itr = this.cache.keySet().iterator(); itr.hasNext();)
{
String key = (String) itr.next();
CachedObject obj = null;
- try {
+ try
+ {
obj = getObject(key);
- } catch (ObjectExpiredException oee) {
+ }
+ catch (ObjectExpiredException oee)
+ {
// this is OK we just do not want this object
continue;
}
objects.add(obj);
}
}
- return (List)objects;
+ return objects;
}
-
/**
- * Circle through the cache and remove stale objects. Frequency
- * is determined by the cacheCheckFrequency property.
+ * Circle through the cache and remove stale objects. Frequency is
+ * determined by the cacheCheckFrequency property.
*/
public void run()
{
- while (continueThread)
+ while (this.continueThread)
{
// Sleep for amount of time set in cacheCheckFrequency -
// default = 5 seconds.
try
{
- Thread.sleep(cacheCheckFrequency);
+ Thread.sleep(this.cacheCheckFrequency);
}
catch (InterruptedException exc)
{
- if (!continueThread) return;
+ if (!this.continueThread)
+ {
+ return;
+ }
}
clearCache();
}
}
+
/**
* Iterate through the cache and remove or refresh stale objects.
*/
@@ -262,52 +285,58 @@
// change the Hashtable while enumerating over it.
synchronized (this)
{
- for (Enumeration e = cache.keys(); e.hasMoreElements();)
+ for (Enumeration e = this.cache.keys(); e.hasMoreElements();)
{
String key = (String) e.nextElement();
- CachedObject co = (CachedObject) cache.get(key);
+ CachedObject co = (CachedObject) this.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
+ {
+ this.cache.remove(key);
+ }
+ else if (rco.isStale())
+ {
// to prolong holding the lock on this object
refreshThese.add(key);
+ }
}
else if (co.isStale())
{
- cache.remove(key);
+ this.cache.remove(key);
}
}
}
for (Iterator i = refreshThese.iterator(); i.hasNext();)
{
String key = (String) i.next();
- CachedObject co = (CachedObject) cache.get(key);
+ CachedObject co = (CachedObject) this.cache.get(key);
RefreshableCachedObject rco = (RefreshableCachedObject) co;
rco.refresh();
}
}
+
/**
* Returns the number of objects currently stored in the cache
- *
+ *
* @return int number of object in the cache
*/
public int getNumberOfObjects()
{
- return cache.size();
+ return this.cache.size();
}
+
/**
* Returns the current size of the cache.
- *
+ *
* @return int representing current cache size in number of bytes
*/
public int getCacheSize() throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
- out.writeObject(cache);
+ out.writeObject(this.cache);
out.flush();
//
// Subtract 4 bytes from the length, because the serialization
@@ -317,6 +346,7 @@
int objectsize = baos.toByteArray().length - 4;
return objectsize;
}
+
/**
* Flush the cache of all objects.
*/
@@ -324,10 +354,10 @@
{
synchronized (this)
{
- for (Enumeration e = cache.keys(); e.hasMoreElements();)
+ for (Enumeration e = this.cache.keys(); e.hasMoreElements();)
{
String key = (String) e.nextElement();
- cache.remove(key);
+ this.cache.remove(key);
}
}
}
@@ -338,8 +368,10 @@
*/
public void configure(Configuration conf) throws ConfigurationException
{
- cacheCheckFrequency = conf.getAttributeAsLong(CACHE_CHECK_FREQUENCY, DEFAULT_CACHE_CHECK_FREQUENCY);
- cacheInitialSize = conf.getAttributeAsInteger(INITIAL_CACHE_SIZE, DEFAULT_INITIAL_CACHE_SIZE);
+ this.cacheCheckFrequency = conf.getAttributeAsLong(
+ CACHE_CHECK_FREQUENCY, DEFAULT_CACHE_CHECK_FREQUENCY);
+ this.cacheInitialSize = conf.getAttributeAsInteger(INITIAL_CACHE_SIZE,
+ DEFAULT_INITIAL_CACHE_SIZE);
}
/**
@@ -349,20 +381,21 @@
{
try
{
- cache = new Hashtable(cacheInitialSize);
+ this.cache = new Hashtable(this.cacheInitialSize);
// Start housekeeping thread.
- continueThread = true;
- housekeeping = new Thread(this);
+ this.continueThread = true;
+ this.housekeeping = new Thread(this);
// Indicate that this is a system thread. JVM will quit only when
// there are no more active user threads. Settings threads spawned
// internally by Turbine as daemons allows commandline applications
// using Turbine to terminate in an orderly manner.
- housekeeping.setDaemon(true);
- housekeeping.start();
+ this.housekeeping.setDaemon(true);
+ this.housekeeping.start();
}
catch (Exception e)
{
- throw new Exception("DefaultGlobalCacheService failed to initialize", e);
+ throw new Exception(
+ "DefaultGlobalCacheService failed to initialize", e);
}
}
@@ -371,12 +404,13 @@
*/
public void dispose()
{
- continueThread = false;
- housekeeping.interrupt();
+ this.continueThread = false;
+ this.housekeeping.interrupt();
}
/**
* The name used to specify this component in TurbineResources.properties
+ *
* @deprecated part of the pre-avalon compatibility layer
*/
protected String getName()
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java Sun Jun 22 02:31:47 2008
@@ -22,19 +22,17 @@
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
-
/**
* @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
- *
+ *
*/
public interface EHCacheService
{
- /** avalon idiom */
- public static final String ROLE = EHCacheService.class.getName();
-
- public CacheManager getCacheManager();
+ /** avalon idiom */
+ public static final String ROLE = EHCacheService.class.getName();
- public Cache getCache(String cache);
+ public CacheManager getCacheManager();
+ public Cache getCache(String cache);
}
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java Sun Jun 22 02:31:47 2008
@@ -26,78 +26,78 @@
/**
* GlobalCacheService interface.
- *
- * @todo This GlobalCacheService should be usable by both the DefaultEHCacheService
- * and the DefaultGlobalCache. The CacheObject class from GCS and the Element class
- * from EHCS are very similar.
- *
+ *
* @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
* @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
* @version $Id$
*/
-public interface GlobalCacheService
- extends Component
+public interface GlobalCacheService extends Component
{
+ /*
+ * TODO: This GlobalCacheService should be usable by both the
+ * DefaultEHCacheService and the DefaultGlobalCache. The CacheObject class
+ * from GCS and the Element class from EHCS are very similar.
+ */
+
/** Avalon role - used to id the component within the manager */
String ROLE = GlobalCacheService.class.getName();
-
/**
* Gets a cached object given its id (a String).
- *
- * @param id The String id for the object.
+ *
+ * @param id
+ * The String id for the object.
* @return A CachedObject.
- * @exception ObjectExpiredException, if the object has expired in
- * the cache.
+ * @exception ObjectExpiredException,
+ * if the object has expired in the cache.
*/
- CachedObject getObject(String id)
- throws ObjectExpiredException;
+ CachedObject getObject(String id) throws ObjectExpiredException;
/**
* Adds an object to the cache.
- *
- * @param id The String id for the object.
- * @param o The object to add to the cache.
+ *
+ * @param id
+ * The String id for the object.
+ * @param o
+ * The object to add to the cache.
*/
void addObject(String id, CachedObject o);
/**
* Removes an object from the cache.
- *
- * @param id The String id for the object.
+ *
+ * @param id
+ * The String id for the object.
*/
void removeObject(String id);
/**
* Returns a copy of keys to objects in the cache as a list.
- *
+ *
* Note that keys to expired objects are not returned.
- *
- * @return A List of <code>String</code>'s representing the keys to objects
- * in the cache.
+ *
+ * @return A List of <code>String</code>'s representing the keys to
+ * objects in the cache.
*/
public List getKeys();
-
/**
- * Returns a copy of the non-expired CachedObjects
- * in the cache as a list.
- *
- * @return A List of <code>CachedObject</code> objects
- * held in the cache
+ * Returns a copy of the non-expired CachedObjects in the cache as a list.
+ *
+ * @return A List of <code>CachedObject</code> objects held in the cache
*/
public List getCachedObjects();
-
/**
* Returns the current size of the cache.
+ *
* @return int representing current cache size in number of bytes
*/
- int getCacheSize()
- throws IOException;
+ int getCacheSize() throws IOException;
/**
* Returns the number of objects in the cache.
+ *
* @return int The current number of objects in the cache.
*/
int getNumberOfObjects();
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/ObjectExpiredException.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/ObjectExpiredException.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/ObjectExpiredException.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/ObjectExpiredException.java Sun Jun 22 02:31:47 2008
@@ -20,14 +20,13 @@
*/
/**
- * Exception thrown when an item either does not exist or has expired
- * from the GlobalCache.
- *
+ * Exception thrown when an item either does not exist or has expired from the
+ * GlobalCache.
+ *
* @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
* @version $Id$
*/
-public class ObjectExpiredException
- extends Exception
+public class ObjectExpiredException extends Exception
{
/**
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/Refreshable.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/Refreshable.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/Refreshable.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/Refreshable.java Sun Jun 22 02:31:47 2008
@@ -20,9 +20,9 @@
*/
/**
- * This is the interface for objects you want to refresh themselves
- * from the cache.
- *
+ * This is the interface for objects you want to refresh themselves from the
+ * cache.
+ *
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @version $Id$
*/
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/RefreshableCachedObject.java Sun Jun 22 02:31:47 2008
@@ -20,23 +20,22 @@
*/
/**
- * The idea of the RefreshableCachedObject is that, rather than
- * removing items from the cache when they become stale, we'll tell them to
- * refresh themselves instead. That way they'll always be in the
- * cache, and the code to refresh them will be run by the background
- * thread rather than by a user request thread. You can also set a TTL (Time
- * To Live) for the object. This way, if the object hasn't been touched
- * for the TTL period, then it will be removed from the cache.
- *
- * This extends CachedObject and provides a method for refreshing the
- * cached object, and resetting its expire time.
- *
+ * The idea of the RefreshableCachedObject is that, rather than removing items
+ * from the cache when they become stale, we'll tell them to refresh themselves
+ * instead. That way they'll always be in the cache, and the code to refresh
+ * them will be run by the background thread rather than by a user request
+ * thread. You can also set a TTL (Time To Live) for the object. This way, if
+ * the object hasn't been touched for the TTL period, then it will be removed
+ * from the cache.
+ *
+ * This extends CachedObject and provides a method for refreshing the cached
+ * object, and resetting its expire time.
+ *
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @version $Id$
*/
-public class RefreshableCachedObject
- extends CachedObject
+public class RefreshableCachedObject extends CachedObject
{
/**
@@ -56,35 +55,37 @@
private long lastAccess;
/**
- * Constructor; sets the object to expire in the default time (30
- * minutes).
- *
- * @param o The object you want to cache.
+ * Constructor; sets the object to expire in the default time (30 minutes).
+ *
+ * @param o
+ * The object you want to cache.
*/
public RefreshableCachedObject(Refreshable o)
{
super(o);
- lastAccess = System.currentTimeMillis();
+ this.lastAccess = System.currentTimeMillis();
}
/**
* Constructor.
- *
- * @param o The object to cache.
- * @param expires How long before the object expires, in ms,
- * e.g. 1000 = 1 second.
+ *
+ * @param o
+ * The object to cache.
+ * @param expires
+ * How long before the object expires, in ms, e.g. 1000 = 1
+ * second.
*/
- public RefreshableCachedObject(Refreshable o,
- long expires)
+ public RefreshableCachedObject(Refreshable o, long expires)
{
super(o, expires);
- lastAccess = System.currentTimeMillis();
+ this.lastAccess = System.currentTimeMillis();
}
/**
* Sets the timeToLive value
- *
- * @param timeToLive the new Value in milliseconds
+ *
+ * @param timeToLive
+ * the new Value in milliseconds
*/
public synchronized void setTTL(long timeToLive)
{
@@ -93,35 +94,41 @@
/**
* Gets the timeToLive value.
- *
+ *
* @return The current timeToLive value (in milliseconds)
*/
public synchronized long getTTL()
{
- return timeToLive;
+ return this.timeToLive;
}
/**
- * Sets the last acccess time to the current time.
+ * Sets the last access time to the current time.
*/
public synchronized void touch()
{
- lastAccess = System.currentTimeMillis();
+ this.lastAccess = System.currentTimeMillis();
}
/**
- * Returns true if the object hasn't been touched
- * in the previous TTL period.
+ * Returns true if the object hasn't been touched in the previous TTL
+ * period.
*/
public synchronized boolean isUntouched()
{
- if (timeToLive < 0)
+ if (this.timeToLive < 0)
+ {
return false;
+ }
- if (lastAccess + timeToLive < System.currentTimeMillis())
+ if (this.lastAccess + this.timeToLive < System.currentTimeMillis())
+ {
return true;
+ }
else
+ {
return false;
+ }
}
/**
@@ -132,7 +139,7 @@
Refreshable r = (Refreshable) getContents();
synchronized (this)
{
- created = System.currentTimeMillis();
+ this.created = System.currentTimeMillis();
r.refresh();
}
}
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java Sun Jun 22 02:31:47 2008
@@ -34,20 +34,20 @@
* @author <a href="mailto:epughNOSPAM@opensourceconnections.com">Eric Pugh </a>
*
*/
-public class DefaultEHCacheService extends AbstractLogEnabled
- implements EHCacheService, Disposable, Initializable, ThreadSafe
+public class DefaultEHCacheService extends AbstractLogEnabled implements
+ EHCacheService, Disposable, Initializable, ThreadSafe
{
private CacheManager cacheManager;
public CacheManager getCacheManager()
{
- return cacheManager;
+ return this.cacheManager;
}
public Cache getCache(String cacheName)
{
- return cacheManager.getCache(cacheName);
+ return this.cacheManager.getCache(cacheName);
}
/**
@@ -56,7 +56,7 @@
public void initialize() throws Exception
{
- cacheManager = CacheManager.create();
+ this.cacheManager = CacheManager.create();
getLogger().debug("EHCacheService started!");
}
@@ -65,8 +65,8 @@
*/
public void dispose()
{
- cacheManager.shutdown();
- cacheManager = null;
+ this.cacheManager.shutdown();
+ this.cacheManager = null;
getLogger().debug("EHCacheService stopped!");
}
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/JCSCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/JCSCacheService.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/JCSCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/JCSCacheService.java Sun Jun 22 02:31:47 2008
@@ -45,18 +45,17 @@
/**
* Default implementation of JCSCacheService
- *
+ *
* @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
* @version $Id:$
*/
-public class JCSCacheService
- extends AbstractLogEnabled
- implements GlobalCacheService, Runnable, Configurable, Disposable, Initializable, ThreadSafe
+public class JCSCacheService extends AbstractLogEnabled implements
+ GlobalCacheService, Runnable, Configurable, Disposable, Initializable,
+ ThreadSafe
{
/**
- * Cache check frequency in Millis (1000 Millis = 1 second).
- * Value must be > 0.
- * Default = 5 seconds
+ * Cache check frequency in Millis (1000 Millis = 1 second). Value must be >
+ * 0. Default = 5 seconds
*/
public static final long DEFAULT_CACHE_CHECK_FREQUENCY = 5000; // 5 seconds
@@ -98,9 +97,11 @@
*/
public void configure(Configuration config) throws ConfigurationException
{
- cacheCheckFrequency = config.getChild("cacheCheckFrequency").getValueAsLong(DEFAULT_CACHE_CHECK_FREQUENCY);
- region = config.getChild("region").getValue("fulcrum");
- configFile = config.getChild("configurationFile").getValue("/cache.ccf");
+ this.cacheCheckFrequency = config.getChild("cacheCheckFrequency")
+ .getValueAsLong(DEFAULT_CACHE_CHECK_FREQUENCY);
+ this.region = config.getChild("region").getValue("fulcrum");
+ this.configFile = config.getChild("configurationFile").getValue(
+ "/cache.ccf");
}
/**
@@ -108,20 +109,20 @@
*/
public void initialize() throws Exception
{
- JCS.setConfigFilename(configFile);
- cacheManager = JCS.getInstance(region);
+ JCS.setConfigFilename(this.configFile);
+ this.cacheManager = JCS.getInstance(this.region);
// Start housekeeping thread.
- continueThread = true;
- refreshing = new Thread(this);
+ this.continueThread = true;
+ this.refreshing = new Thread(this);
// Indicate that this is a system thread. JVM will quit only when
// there are no more active user threads. Settings threads spawned
// internally by Turbine as daemons allows commandline applications
// using Turbine to terminate in an orderly manner.
- refreshing.setDaemon(true);
- refreshing.setName("JCSCacheService Refreshing");
- refreshing.start();
+ this.refreshing.setDaemon(true);
+ this.refreshing.setName("JCSCacheService Refreshing");
+ this.refreshing.start();
getLogger().debug("JCSCacheService started.");
}
@@ -131,12 +132,12 @@
*/
public void dispose()
{
- continueThread = false;
- refreshing.interrupt();
+ this.continueThread = false;
+ this.refreshing.interrupt();
+
+ this.cacheManager.dispose();
+ this.cacheManager = null;
- cacheManager.dispose();
- cacheManager = null;
-
getLogger().debug("JCSCacheService stopped.");
}
@@ -145,7 +146,8 @@
*/
public CachedObject getObject(String id) throws ObjectExpiredException
{
- CachedObject obj = (CachedObject) cacheManager.getFromGroup(id, group);
+ CachedObject obj = (CachedObject) this.cacheManager.getFromGroup(id,
+ group);
if (obj == null)
{
@@ -193,7 +195,8 @@
}
/**
- * @see org.apache.fulcrum.cache.GlobalCacheService#addObject(java.lang.String, org.apache.fulcrum.cache.CachedObject)
+ * @see org.apache.fulcrum.cache.GlobalCacheService#addObject(java.lang.String,
+ * org.apache.fulcrum.cache.CachedObject)
*/
public void addObject(String id, CachedObject o)
{
@@ -201,10 +204,15 @@
{
if (!(o.getContents() instanceof Serializable))
{
- getLogger().warn("Object with id [" + id + "] is not serializable. Expect problems with auxiliary caches.");
+ getLogger()
+ .warn(
+ "Object with id ["
+ + id
+ + "] is not serializable. Expect problems with auxiliary caches.");
}
- ElementAttributes attrib = (ElementAttributes)cacheManager.getDefaultElementAttributes();
+ ElementAttributes attrib = (ElementAttributes) this.cacheManager
+ .getDefaultElementAttributes();
if (o instanceof RefreshableCachedObject)
{
@@ -213,13 +221,13 @@
else
{
attrib.setIsEternal(false);
- attrib.setMaxLifeSeconds((o.getExpires() + 500)/ 1000);
+ attrib.setMaxLifeSeconds((o.getExpires() + 500) / 1000);
}
attrib.setLastAccessTimeNow();
attrib.setCreateTime();
- cacheManager.putInGroup(id, group, o, attrib);
+ this.cacheManager.putInGroup(id, group, o, attrib);
}
catch (CacheException e)
{
@@ -232,7 +240,7 @@
*/
public void removeObject(String id)
{
- cacheManager.remove(id, group);
+ this.cacheManager.remove(id, group);
}
/**
@@ -242,7 +250,7 @@
{
ArrayList keys = new ArrayList();
- keys.addAll(cacheManager.getGroupKeys(group));
+ keys.addAll(this.cacheManager.getGroupKeys(group));
return keys;
}
@@ -253,9 +261,10 @@
{
ArrayList values = new ArrayList();
- for (Iterator i = cacheManager.getGroupKeys(group).iterator(); i.hasNext();)
+ for (Iterator i = this.cacheManager.getGroupKeys(group).iterator(); i
+ .hasNext();)
{
- Object o = cacheManager.getFromGroup(i.next(), group);
+ Object o = this.cacheManager.getFromGroup(i.next(), group);
if (o != null)
{
values.add(o);
@@ -266,28 +275,32 @@
}
/**
- * Circle through the cache and refresh stale objects. Frequency
- * is determined by the cacheCheckFrequency property.
+ * Circle through the cache and refresh stale objects. Frequency is
+ * determined by the cacheCheckFrequency property.
*/
public void run()
{
- while (continueThread)
+ while (this.continueThread)
{
// Sleep for amount of time set in cacheCheckFrequency -
// default = 5 seconds.
try
{
- Thread.sleep(cacheCheckFrequency);
+ Thread.sleep(this.cacheCheckFrequency);
}
catch (InterruptedException exc)
{
- if (!continueThread) return;
+ if (!this.continueThread)
+ {
+ return;
+ }
}
- for (Iterator i = cacheManager.getGroupKeys(group).iterator(); i.hasNext();)
+ for (Iterator i = this.cacheManager.getGroupKeys(group).iterator(); i
+ .hasNext();)
{
- String key= (String)i.next();
- Object o = cacheManager.getFromGroup(key, group);
+ String key = (String) i.next();
+ Object o = this.cacheManager.getFromGroup(key, group);
if (o == null)
{
removeObject(key);
@@ -299,7 +312,7 @@
RefreshableCachedObject rco = (RefreshableCachedObject) o;
if (rco.isUntouched())
{
- cacheManager.remove(key, group);
+ this.cacheManager.remove(key, group);
}
else if (rco.isStale())
{
@@ -319,11 +332,11 @@
// This is evil!
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
- Set keys = cacheManager.getGroupKeys(group);
+ Set keys = this.cacheManager.getGroupKeys(group);
for (Iterator i = keys.iterator(); i.hasNext();)
{
- out.writeObject(cacheManager.getFromGroup(i.next(), group));
+ out.writeObject(this.cacheManager.getFromGroup(i.next(), group));
}
out.flush();
@@ -344,9 +357,10 @@
{
int count = 0;
- for (Iterator i = cacheManager.getGroupKeys(group).iterator(); i.hasNext();)
+ for (Iterator i = this.cacheManager.getGroupKeys(group).iterator(); i
+ .hasNext();)
{
- if (cacheManager.getFromGroup(i.next(), group) != null)
+ if (this.cacheManager.getFromGroup(i.next(), group) != null)
{
count++;
}
@@ -360,6 +374,6 @@
*/
public void flushCache()
{
- cacheManager.invalidateGroup(group);
+ this.cacheManager.invalidateGroup(group);
}
}
Modified: turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java?rev=670326&r1=670325&r2=670326&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java (original)
+++ turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java Sun Jun 22 02:31:47 2008
@@ -18,6 +18,7 @@
*/
package org.apache.fulcrum.cache;
+
// Cactus and Junit imports
import java.util.ConcurrentModificationException;
@@ -29,9 +30,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fulcrum.testcontainer.BaseUnitTest;
+
/**
* CacheTest
- *
+ *
* @author <a href="paulsp@apache.org">Paul Spencer</a>
* @author <a href="epugh@upstate.com">Eric Pugh</a>
* @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
@@ -41,16 +43,20 @@
{
private GlobalCacheService globalCache = null;
+
private static final String cacheKey = "CacheKey";
+
private static final String cacheKey_2 = "CacheKey_2";
+
public static final String SKIP_TESTS_KEY = "fulcrum.cache.skip.long.tests";
- private static final Log LOG = LogFactory.getLog(CacheTest.class);
+ private static final Log LOG = LogFactory.getLog(CacheTest.class);
/**
* Defines the testcase name for JUnit.
- *
- * @param name the testcase's name.
+ *
+ * @param name
+ * the testcase's name.
*/
public CacheTest(String name)
{
@@ -62,7 +68,8 @@
super.setUp();
try
{
- globalCache = (GlobalCacheService) this.lookup(GlobalCacheService.ROLE);
+ this.globalCache = (GlobalCacheService) this
+ .lookup(GlobalCacheService.ROLE);
}
catch (ComponentException e)
{
@@ -70,122 +77,131 @@
fail(e.getMessage());
}
}
+
/**
* Simple test that verify an object can be created and deleted.
+ *
* @throws Exception
*/
public void testSimpleAddGetCacheObject() throws Exception
{
- String testString = new String("This is a test");
+ String testString = "This is a test";
Object retrievedObject = null;
CachedObject cacheObject1 = null;
// Create object
cacheObject1 = new CachedObject(testString);
assertNotNull("Failed to create a cachable object 1", cacheObject1);
// Add object to cache
- globalCache.addObject(cacheKey, cacheObject1);
+ this.globalCache.addObject(cacheKey, cacheObject1);
// Get object from cache
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1", retrievedObject);
- assertTrue("Did not retrieved a correct, expected cached object 1", retrievedObject == cacheObject1);
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrievea cached object 1", retrievedObject);
+ assertSame("Did not retrieve a correct, expected cached object 1",
+ retrievedObject, cacheObject1);
// Remove object from cache
- globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey);
// Verify object removed from cache
retrievedObject = null;
cacheObject1 = null;
try
{
- retrievedObject = globalCache.getObject(cacheKey);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNull(
- "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException",
- retrievedObject);
- assertNotNull("Did not get expected ObjectExpiredException retrieving a deleted object", retrievedObject);
+ "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException",
+ retrievedObject);
+ assertNotNull(
+ "Did not get expected ObjectExpiredException retrieving a deleted object",
+ retrievedObject);
}
catch (ObjectExpiredException e)
{
assertNull(
- "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- catch (Exception e)
- {
- throw e;
+ "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception",
+ retrievedObject);
}
// Remove object from cache that does NOT exist in the cache
- globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey);
}
+
/**
* Simple test that adds, retrieves, and deletes 2 object.
- *
+ *
* @throws Exception
*/
public void test2ObjectAddGetCachedObject() throws Exception
{
- String testString = new String("This is a test");
+ String testString = "This is a test";
Object retrievedObject = null;
CachedObject cacheObject1 = null;
CachedObject cacheObject2 = null;
// Create and add Object #1
cacheObject1 = new CachedObject(testString);
assertNotNull("Failed to create a cachable object 1", cacheObject1);
- globalCache.addObject(cacheKey, cacheObject1);
- retrievedObject = globalCache.getObject(cacheKey);
+ this.globalCache.addObject(cacheKey, cacheObject1);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNotNull("Did not retrieved a cached object 1", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject1, retrievedObject);
+ assertEquals("Did not retrieved correct cached object", cacheObject1,
+ retrievedObject);
// Create and add Object #2
cacheObject2 = new CachedObject(testString);
assertNotNull("Failed to create a cachable object 2", cacheObject2);
- globalCache.addObject(cacheKey_2, cacheObject2);
- retrievedObject = globalCache.getObject(cacheKey_2);
+ this.globalCache.addObject(cacheKey_2, cacheObject2);
+ retrievedObject = this.globalCache.getObject(cacheKey_2);
assertNotNull("Did not retrieved a cached object 2", retrievedObject);
- assertEquals("Did not retrieved correct cached object 2", cacheObject2, retrievedObject);
+ assertEquals("Did not retrieved correct cached object 2", cacheObject2,
+ retrievedObject);
// Get object #1
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #2", retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #2", cacheObject1, retrievedObject);
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrieved a cached object 1. Attempt #2",
+ retrievedObject);
+ assertEquals("Did not retrieved correct cached object 1. Attempt #2",
+ cacheObject1, retrievedObject);
// Get object #1
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #3", retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #3", cacheObject1, retrievedObject);
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrieved a cached object 1. Attempt #3",
+ retrievedObject);
+ assertEquals("Did not retrieved correct cached object 1. Attempt #3",
+ cacheObject1, retrievedObject);
// Get object #2
- retrievedObject = globalCache.getObject(cacheKey_2);
- assertNotNull("Did not retrieved a cached object 2. Attempt #2", retrievedObject);
- assertEquals("Did not retrieved correct cached object 2 Attempt #2", cacheObject2, retrievedObject);
+ retrievedObject = this.globalCache.getObject(cacheKey_2);
+ assertNotNull("Did not retrieved a cached object 2. Attempt #2",
+ retrievedObject);
+ assertEquals("Did not retrieved correct cached object 2 Attempt #2",
+ cacheObject2, retrievedObject);
// Remove objects
- globalCache.removeObject(cacheKey);
- globalCache.removeObject(cacheKey_2);
+ this.globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey_2);
}
+
/**
- * Verify that an object will throw the ObjectExpiredException
- * when it now longer exists in cache.
- *
+ * Verify that an object will throw the ObjectExpiredException when it now
+ * longer exists in cache.
+ *
* @throws Exception
*/
public void testObjectExpiration() throws Exception
{
- String testString = new String("This is a test");
+ String testString = "This is a test";
Object retrievedObject = null;
CachedObject cacheObject = null;
// Create and add Object that expires in 1000 millis (1 second)
cacheObject = new CachedObject(testString, 1000);
assertNotNull("Failed to create a cachable object", cacheObject);
long addTime = System.currentTimeMillis();
- globalCache.addObject(cacheKey, cacheObject);
+ this.globalCache.addObject(cacheKey, cacheObject);
// Try to get un-expired object
try
{
- retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject, retrievedObject);
+ assertEquals("Did not retrieved correct cached object",
+ cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
{
- assertTrue("Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false);
- }
- catch (Exception e)
- {
- throw e;
+ assertTrue("Object expired early ( "
+ + (System.currentTimeMillis() - addTime) + " millis)",
+ false);
}
// Sleep 1500 Millis (1.5 seconds)
Thread.sleep(1500);
@@ -193,79 +209,85 @@
try
{
retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNull(
- "Retrieved the expired cached object and did not get expected ObjectExpiredException",
- retrievedObject);
- assertNotNull("Did not get expected ObjectExpiredException retrieving an expired object", retrievedObject);
+ "Retrieved the expired cached object and did not get expected ObjectExpiredException",
+ retrievedObject);
+ assertNotNull(
+ "Did not get expected ObjectExpiredException retrieving an expired object",
+ retrievedObject);
}
catch (ObjectExpiredException e)
{
assertNull(
- "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- catch (Exception e)
- {
- throw e;
+ "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
+ retrievedObject);
}
// Remove objects
- globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey);
}
+
/**
* Verify the all object will be flushed from the cache.
- *
+ *
* This test can take server minutes.
- *
+ *
* @throws Exception
*/
public void testCacheFlush() throws Exception
{
- String testString = new String("This is a test");
+ String testString = "This is a test";
CachedObject cacheObject = null;
// Create and add Object that expires in 1 turbine Refresh + 1 millis
cacheObject = new CachedObject(testString, (getCacheRefresh() * 5) + 1);
assertNotNull("Failed to create a cachable object", cacheObject);
- globalCache.addObject(cacheKey, cacheObject);
+ this.globalCache.addObject(cacheKey, cacheObject);
// 1 Refresh
Thread.sleep(getCacheRefresh() + 1);
- assertTrue("No object in cache before flush", (0 < globalCache.getNumberOfObjects()));
+ assertTrue("No object in cache before flush", (0 < this.globalCache
+ .getNumberOfObjects()));
// Flush Cache
- globalCache.flushCache();
+ this.globalCache.flushCache();
// Wait 15 seconds, 3 Refresh
Thread.sleep((getCacheRefresh() * 2) + 1);
- assertEquals("After refresh", 0, globalCache.getNumberOfObjects());
+ assertEquals("After refresh", 0, this.globalCache.getNumberOfObjects());
// Remove objects
- globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey);
}
+
/**
* Verify the Cache count is correct.
+ *
* @throws Exception
*/
public void testObjectCount() throws Exception
{
- assertNotNull("Could not retrive cache service.", globalCache);
+ assertNotNull("Could not retrive cache service.", this.globalCache);
// Create and add Object that expires in 1.5 turbine Refresh
long expireTime = getCacheRefresh() + getCacheRefresh() / 2;
- CachedObject cacheObject = new CachedObject("This is a test", expireTime);
+ CachedObject cacheObject = new CachedObject("This is a test",
+ expireTime);
assertNotNull("Failed to create a cachable object", cacheObject);
- globalCache.addObject(cacheKey, cacheObject);
- assertEquals("After adding 1 Object", 1, globalCache.getNumberOfObjects());
+ this.globalCache.addObject(cacheKey, cacheObject);
+ assertEquals("After adding 1 Object", 1, this.globalCache
+ .getNumberOfObjects());
// Wait until we're passed 1 refresh, but not half way.
Thread.sleep(getCacheRefresh() + getCacheRefresh() / 3);
- assertEquals("After one refresh", 1, globalCache.getNumberOfObjects());
+ assertEquals("After one refresh", 1, this.globalCache
+ .getNumberOfObjects());
// Wait until we're passed 2 more refreshes
Thread.sleep((getCacheRefresh() * 2) + getCacheRefresh() / 3);
- assertEquals("After three refreshes", 0, globalCache.getNumberOfObjects());
+ assertEquals("After three refreshes", 0, this.globalCache
+ .getNumberOfObjects());
}
+
/**
- * Verfy a refreshable object will refreshed in the following cases:
- * o The object is retrieved via getObject an it is stale.
- * o The object is determied to be stale during a cache
- * refresh
- *
+ * Verfy a refreshable object will refreshed in the following cases: o The
+ * object is retrieved via getObject an it is stale. o The object is
+ * determied to be stale during a cache refresh
+ *
* This test can take serveral minutes.
- *
+ *
* @throws Exception
*/
public void testRefreshableObject() throws Exception
@@ -273,25 +295,24 @@
Object retrievedObject = null;
RefreshableCachedObject cacheObject = null;
// Create and add Object that expires in TEST_EXPIRETIME millis.
- cacheObject = new RefreshableCachedObject(new RefreshableObject(), getTestExpireTime());
+ cacheObject = new RefreshableCachedObject(new RefreshableObject(),
+ getTestExpireTime());
assertNotNull("Failed to create a cachable object", cacheObject);
long addTime = System.currentTimeMillis();
- globalCache.addObject(cacheKey, cacheObject);
+ this.globalCache.addObject(cacheKey, cacheObject);
// Try to get un-expired object
try
{
- retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject, retrievedObject);
+ assertEquals("Did not retrieved correct cached object",
+ cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
{
- assertTrue("Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false);
- }
- catch (Exception e)
- {
- throw e;
+ assertTrue("Object expired early ( "
+ + (System.currentTimeMillis() - addTime) + " millis)",
+ false);
}
// Wait 1 Turbine cache refresh + 1 second.
Thread.sleep(getTestExpireTime() + 1000);
@@ -299,30 +320,25 @@
try
{
retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object, after sleep", retrievedObject);
- assertNotNull(
- "Cached object has no contents, after sleep.",
- ((RefreshableCachedObject) retrievedObject).getContents());
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrieved a cached object, after sleep",
+ retrievedObject);
+ assertNotNull("Cached object has no contents, after sleep.",
+ ((RefreshableCachedObject) retrievedObject).getContents());
assertTrue(
- "Object did not refresh.",
- (((RefreshableObject) ((RefreshableCachedObject) retrievedObject).getContents()).getRefreshCount()
- > 0));
+ "Object did not refresh.",
+ (((RefreshableObject) ((RefreshableCachedObject) retrievedObject)
+ .getContents()).getRefreshCount() > 0));
}
catch (ObjectExpiredException e)
{
- assertTrue(
- "Received unexpected ObjectExpiredException exception "
+ assertTrue("Received unexpected ObjectExpiredException exception "
+ "when retrieving refreshable object after ( "
- + (System.currentTimeMillis() - addTime)
- + " millis)",
- false);
- }
- catch (Exception e)
- {
- throw e;
+ + (System.currentTimeMillis() - addTime) + " millis)",
+ false);
}
- // See if object will expires (testing every second for 100 seconds. It sould not!
+ // See if object will expires (testing every second for 100 seconds. It
+ // sould not!
for (int i = 0; i < 100; i++)
{
Thread.sleep(1000); // Sleep 0.5 seconds
@@ -330,79 +346,80 @@
try
{
retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object, after sleep", retrievedObject);
- assertNotNull(
- "Cached object has no contents, after sleep.",
- ((RefreshableCachedObject) retrievedObject).getContents());
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrieved a cached object, after sleep",
+ retrievedObject);
+ assertNotNull("Cached object has no contents, after sleep.",
+ ((RefreshableCachedObject) retrievedObject)
+ .getContents());
assertTrue(
- "Object did not refresh.",
- (((RefreshableObject) ((RefreshableCachedObject) retrievedObject).getContents()).getRefreshCount()
- > 0));
+ "Object did not refresh.",
+ (((RefreshableObject) ((RefreshableCachedObject) retrievedObject)
+ .getContents()).getRefreshCount() > 0));
}
catch (ObjectExpiredException e)
{
assertTrue(
- "Received unexpected ObjectExpiredException exception "
- + "when retrieving refreshable object after ( "
- + (System.currentTimeMillis() - addTime)
- + " millis)",
- false);
- }
- catch (Exception e)
- {
- throw e;
+ "Received unexpected ObjectExpiredException exception "
+ + "when retrieving refreshable object after ( "
+ + (System.currentTimeMillis() - addTime)
+ + " millis)", false);
}
}
// Remove objects
- globalCache.removeObject(cacheKey);
+ this.globalCache.removeObject(cacheKey);
}
+
/**
- * Verify a cached object will be delete after it has been
- * untouched beyond it's TimeToLive.
- *
+ * Verify a cached object will be delete after it has been untouched beyond
+ * it's TimeToLive.
+ *
* This test can take serveral minutes.
- *
+ *
* @throws Exception
*/
public void testRefreshableTimeToLive() throws Exception
{
- String skipTestsProperty = System.getProperty(SKIP_TESTS_KEY,"false");
- LOG.info("What is the value of the skipTestsProperty:" + skipTestsProperty);
- if(Boolean.valueOf(skipTestsProperty).booleanValue()){
- LOG.warn("Skipping testRefreshableTimeToLive test due to property " + SKIP_TESTS_KEY + " being true.");
+ String skipTestsProperty = System.getProperty(SKIP_TESTS_KEY, "false");
+ LOG.info("What is the value of the skipTestsProperty:"
+ + skipTestsProperty);
+ if (Boolean.valueOf(skipTestsProperty).booleanValue())
+ {
+ LOG.warn("Skipping testRefreshableTimeToLive test due to property "
+ + SKIP_TESTS_KEY + " being true.");
return;
}
- else {
- LOG.warn("Running testRefreshableTimeToLive test due to property " + SKIP_TESTS_KEY + " being false.");
+ else
+ {
+ LOG.warn("Running testRefreshableTimeToLive test due to property "
+ + SKIP_TESTS_KEY + " being false.");
}
Object retrievedObject = null;
RefreshableCachedObject cacheObject = null;
// Create and add Object that expires in TEST_EXPIRETIME millis.
- cacheObject = new RefreshableCachedObject(new RefreshableObject(), getTestExpireTime());
+ cacheObject = new RefreshableCachedObject(new RefreshableObject(),
+ getTestExpireTime());
assertNotNull("Failed to create a cachable object", cacheObject);
cacheObject.setTTL(getTestExpireTime());
// Verify TimeToLive was set
- assertEquals("Returned TimeToLive", getTestExpireTime(), cacheObject.getTTL());
+ assertEquals("Returned TimeToLive", getTestExpireTime(), cacheObject
+ .getTTL());
// Add object to Cache
long addTime = System.currentTimeMillis();
- globalCache.addObject(cacheKey, cacheObject);
+ this.globalCache.addObject(cacheKey, cacheObject);
// Try to get un-expired object
try
{
- retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
+ retrievedObject = this.globalCache.getObject(cacheKey);
assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject, retrievedObject);
+ assertEquals("Did not retrieved correct cached object",
+ cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
{
- fail("Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)");
- }
- catch (Exception e)
- {
- throw e;
+ fail("Object expired early ( "
+ + (System.currentTimeMillis() - addTime) + " millis)");
}
// Wait long enough to allow object to expire, but do not exceed TTL
long timeout = getTestExpireTime() - 0000;
@@ -411,32 +428,25 @@
try
{
retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object, after sleep", retrievedObject);
- assertNotNull(
- "Cached object has no contents, after sleep.",
- ((RefreshableCachedObject) retrievedObject).getContents());
- /*
- * @todo this is not working for some reason
- *
- * assertTrue(
- * "Object did not refresh.",
- * (((RefreshableObject) ((RefreshableCachedObject) retrievedObject).getContents()).getRefreshCount()
- * > 0));
- */
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNotNull("Did not retrieve a cached object, after sleep",
+ retrievedObject);
+ assertNotNull("Cached object has no contents, after sleep.",
+ ((RefreshableCachedObject) retrievedObject).getContents());
+ /*
+ * @todo this is not working for some reason
+ *
+ * assertTrue( "Object did not refresh.", (((RefreshableObject)
+ * ((RefreshableCachedObject)
+ * retrievedObject).getContents()).getRefreshCount() > 0));
+ */
}
catch (ObjectExpiredException e)
{
- assertTrue(
- "Received unexpected ObjectExpiredException exception "
+ assertTrue("Received unexpected ObjectExpiredException exception "
+ "when retrieving refreshable object after ( "
- + (System.currentTimeMillis() - addTime)
- + " millis)",
- false);
- }
- catch (Exception e)
- {
- throw e;
+ + (System.currentTimeMillis() - addTime) + " millis)",
+ false);
}
// Wait long enough to allow object to expire and exceed TTL
Thread.sleep(getTestExpireTime() + 5000);
@@ -444,107 +454,125 @@
try
{
retrievedObject = null;
- retrievedObject = globalCache.getObject(cacheKey);
- assertNull("Retrieved a cached object, after exceeding TimeToLive", retrievedObject);
+ retrievedObject = this.globalCache.getObject(cacheKey);
+ assertNull("Retrieved a cached object, after exceeding TimeToLive",
+ retrievedObject);
}
catch (ObjectExpiredException e)
{
assertNull(
- "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- catch (Exception e)
- {
- throw e;
+ "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
+ retrievedObject);
}
}
+
/**
* Test that we can get a list of the keys in the cache
- *
+ *
* @return
*/
- public void testCacheGetKeyList() {
- globalCache.flushCache();
- globalCache.addObject("date1", new CachedObject(new Date()));
- globalCache.addObject("date2", new CachedObject(new Date()));
- globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get key list back.", (globalCache.getKeys() != null));
- List keys = globalCache.getKeys();
- for (Iterator itr = keys.iterator(); itr.hasNext();) {
+ public void testCacheGetKeyList()
+ {
+ this.globalCache.flushCache();
+ this.globalCache.addObject("date1", new CachedObject(new Date()));
+ this.globalCache.addObject("date2", new CachedObject(new Date()));
+ this.globalCache.addObject("date3", new CachedObject(new Date()));
+ assertTrue("Did not get key list back.",
+ (this.globalCache.getKeys() != null));
+ List keys = this.globalCache.getKeys();
+ for (Iterator itr = keys.iterator(); itr.hasNext();)
+ {
Object key = itr.next();
- assertTrue("Key was not an instance of String.", (key instanceof String));
+ assertTrue("Key was not an instance of String.",
+ (key instanceof String));
}
}
/**
* Test that we can get a list of the keys in the cache
- *
+ *
* @return
*/
- public void testCacheGetCachedObjects() {
- globalCache.flushCache();
- globalCache.addObject("date1", new CachedObject(new Date()));
- globalCache.addObject("date2", new CachedObject(new Date()));
- globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get object list back.", (globalCache.getCachedObjects() != null));
- List objects = globalCache.getCachedObjects();
- for (Iterator itr = objects.iterator(); itr.hasNext();) {
+ public void testCacheGetCachedObjects()
+ {
+ this.globalCache.flushCache();
+ this.globalCache.addObject("date1", new CachedObject(new Date()));
+ this.globalCache.addObject("date2", new CachedObject(new Date()));
+ this.globalCache.addObject("date3", new CachedObject(new Date()));
+ assertTrue("Did not get object list back.", (this.globalCache
+ .getCachedObjects() != null));
+ List objects = this.globalCache.getCachedObjects();
+ for (Iterator itr = objects.iterator(); itr.hasNext();)
+ {
Object obj = itr.next();
assertNotNull("Object was null.", obj);
- assertTrue("Object was not an instance of CachedObject", (obj instanceof CachedObject));
+ assertTrue("Object was not an instance of CachedObject",
+ (obj instanceof CachedObject));
}
}
/**
* Test that the retrieved list is safe from
- * ConcurrentModificationException's being thrown if the cache
- * is updated while we are iterating over the List.
- *
+ * ConcurrentModificationException's being thrown if the cache is updated
+ * while we are iterating over the List.
+ *
* @return
*/
- public void testCacheModification() {
- globalCache.flushCache();
- globalCache.addObject("date1", new CachedObject(new Date()));
- globalCache.addObject("date2", new CachedObject(new Date()));
- globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get key list back.", (globalCache.getKeys() != null));
- List keys = globalCache.getKeys();
- try {
- for (Iterator itr = keys.iterator(); itr.hasNext();) {
- Object key = itr.next();
- globalCache.addObject("date4", new CachedObject(new Date()));
- }
- } catch (ConcurrentModificationException cme)
+ public void testCacheModification()
+ {
+ this.globalCache.flushCache();
+ this.globalCache.addObject("date1", new CachedObject(new Date()));
+ this.globalCache.addObject("date2", new CachedObject(new Date()));
+ this.globalCache.addObject("date3", new CachedObject(new Date()));
+ assertTrue("Did not get key list back.",
+ (this.globalCache.getKeys() != null));
+ List keys = this.globalCache.getKeys();
+ try
+ {
+ for (Iterator itr = keys.iterator(); itr.hasNext();)
+ {
+ Object key = itr.next();
+ this.globalCache.addObject("date4",
+ new CachedObject(new Date()));
+ }
+ }
+ catch (ConcurrentModificationException cme)
{
fail("Caught ConcurrentModificationException adding to cache.");
}
- List objects = globalCache.getCachedObjects();
- try {
- for (Iterator itr = objects.iterator(); itr.hasNext();) {
- Object obj = itr.next();
- globalCache.addObject("date4", new CachedObject(new Date()));
- }
- } catch (ConcurrentModificationException cme)
+ List objects = this.globalCache.getCachedObjects();
+ try
+ {
+ for (Iterator itr = objects.iterator(); itr.hasNext();)
+ {
+ Object obj = itr.next();
+ this.globalCache.addObject("date4",
+ new CachedObject(new Date()));
+ }
+ }
+ catch (ConcurrentModificationException cme)
{
fail("Caught ConcurrentModificationException adding to cache.");
}
}
-
/**
* Down cast the interface to the concreate object in order to grab the
* cache check frequency.
+ *
* @return the refresh requency in milliseconds
*/
private long getCacheRefresh()
{
- return ((DefaultGlobalCacheService) globalCache).getCacheCheckFrequency()*1000;
+ return ((DefaultGlobalCacheService) this.globalCache)
+ .getCacheCheckFrequency() * 1000;
}
/**
* How long until it expires
+ *
* @return the cache refresh plus 1000.
*/
private long getTestExpireTime()
@@ -552,17 +580,13 @@
return getCacheRefresh() + 1000;
}
-
- private long getTestTimeToLive()
- {
- return getTestExpireTime()*5;
- }
/**
* Simple object that can be refreshed
*/
class RefreshableObject implements Refreshable
{
private int refreshCount = 0;
+
/**
* Increment the refresh counter
*/
@@ -570,9 +594,10 @@
{
this.refreshCount++;
}
+
/**
* Reutrn the number of time this object has been refreshed
- *
+ *
* @return Number of times refresh() has been called
*/
public int getRefreshCount()