Page 3 of 5
Before proceeding with a discussion of Requirement 4, I'll briefly mention that we will satisfy Requirement 5 by creating an anonymous thread in the cache manager; this thread starts in the static block. Also, we satisfy Requirement 6 by identifying the points where code would later be added to implement the LRU and LFU algorithms. I will go into more detail about these requirements later in the article.
Now, back to Requirement 4, where things become interesting. If every cached object must determine for itself whether it is expired, then you must have a way to ask the object if it's expired. That means that objects in the cache must all conform to certain rules; you accomplish that in Java by implementing an interface.
Let's begin with the rules that govern the objects placed in the cache.
isExpired(), which returns a Boolean value.
getIdentifier(), which returns an object that distinguishes the object from all others in the cache.
Note: Before jumping straight into the code, you must understand that you can implement a cache in many ways. I have found more than a dozen different implementations. Enhydra and Caucho provide excellent resources that contain several cache implementations.
You'll find the interface code for this article's caching service in Listing 1.
Listing 1. Cacheable.java
/**
* Title: Caching
Description: This interface defines the methods, which must be implemented
by
all objects wishing to be placed in the cache.
*
* Copyright: Copyright (c) 2001
* Company: JavaWorld
* FileName: Cacheable.java
@author Jonathan Lurie
@version 1.0
*/
public interface Cacheable
{
/* By requiring all objects to determine their own expirations, the
algorithm is abstracted from the caching service, thereby providing maximum
flexibility since each object can adopt a different expiration strategy.
*/
public boolean isExpired();
/* This method will ensure that the caching service is not responsible for
uniquely identifying objects placed in the cache.
*/
public Object getIdentifier();
}
Any object placed in the cache -- a String, for example -- must be wrapped inside an object that implements the Cacheable interface. Listing 2 is an example of a generic wrapper class called CachedObject; it can contain any object needed to be placed in the caching service. Note that this wrapper class implements the Cacheable interface defined in Listing 1.
Listing 2. CachedManagerTestProgram.java
/**
* Title: Caching
* Description: A Generic Cache Object wrapper. Implements the Cacheable
interface
* uses a TimeToLive stategy for CacheObject expiration.
* Copyright: Copyright (c) 2001
* Company: JavaWorld
* Filename: CacheManagerTestProgram.java
* @author Jonathan Lurie
* @version 1.0
*/
public class CachedObject implements Cacheable
{
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* This variable will be used to determine if the object is expired.
*/
private java.util.Date dateofExpiration = null;
private Object identifier = null;
/* This contains the real "value". This is the object which needs to
be
shared.
*/
public Object object = null;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public CachedObject(Object obj, Object id, int minutesToLive)
{
this.object = obj;
this.identifier = id;
// minutesToLive of 0 means it lives on indefinitely.
if (minutesToLive != 0)
{
dateofExpiration = new java.util.Date();
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(dateofExpiration);
cal.add(cal.MINUTE, minutesToLive);
dateofExpiration = cal.getTime();
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public boolean isExpired()
{
// Remember if the minutes to live is zero then it lives forever!
if (dateofExpiration != null)
{
// date of expiration is compared.
if (dateofExpiration.before(new java.util.Date()))
{
System.out.println("CachedResultSet.isExpired: Expired from
Cache! EXPIRE TIME: " + dateofExpiration.toString() + " CURRENT TIME: " +
(new java.util.Date()).toString());
return true;
}
else
{
System.out.println("CachedResultSet.isExpired: Expired not from
Cache!");
return false;
}
}
else // This means it lives forever!
return false;
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public Object getIdentifier()
{
return identifier;
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
The CachedObject class exposes a constructor method that takes three parameters:
Great!By Anonymous on April 30, 2009, 8:40 amvery basic level approach. clean approach for deciding. actually i was chosing between different softwares/caching techniques in market. this article gave me...
Reply | Read entire comment
View all comments