Cache Managment
The class that will be cached.
-----------------------------------------------------
class MyObject{
private MyObjectKey myObjectKey;
public MyObject(MyObjectKey key){
myObjectKey = key;
}
public MyObjectKey getKey(){
return myObjectKey;
}
}
-----------------------------------------------------
The key class for the MyObject class.
-----------------------------------------------------
class MyObjectKey{
String key;
public MyObjectKey(String key){
this.key = key;
}
public int hashCode(){ return key.hashCode();}
public boolean equals(Object obj){
return (obj instanceof MyObjectKey && key.equals(((MyObjectKey)(obj)).key));
}
}
-----------------------------------------------------
The class that creates (intstantiates) the MyObject
classes for a client but does not use any caching.
-----------------------------------------------------
class MyObjectCreator{
public MyObject createMyObject(MyObjectKey key){
System.out.println("creating new object");
/* imagine the MyObject class is build up
from data base calls and is quite large.
*/
return new MyObject(key);
}
}
-----------------------------------------------------
The CacheManager class.
This is the class that will be utilized by a client.
This class caches MyObjects only creating them if
a MyObject does not exist with the given key.
Notice CacheManager is a MyObjectCreator so a client
need not be aware that a cache manager is being
utilized.
-----------------------------------------------------
class CacheManager extends MyObjectCreator{
private Cache cache = new Cache();
public MyObject createMyObject(MyObjectKey key){
System.out.println("checking cache");
MyObject temp = cache.getObject(key);
if(temp == null){
temp = super.createMyObject(key);
cache.setObject(temp);
}
return temp;
}
}
-----------------------------------------------------
The actual Cache class utilized by CacheManager.
-----------------------------------------------------
class Cache{
private Hashtable cache = new Hashtable();
public MyObject getObject(MyObjectKey key){
return (MyObject)cache.get(key);
}
public void setObject(MyObject myObject){
cache.put(myObject.getKey(),myObject);
}
}
-----------------------------------------------------
The client class, a test stub to run it all.
Remember the CacheManager is a MyObjectCreator.
As a result if there were MyObjectCreator accessors
(getters and setters) in this class (it was a true client
instead of a test stub) it would be unaware of any change
when utilizing the cache system.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main");
MyObjectCreator myObjectCreator = new CacheManager();
myObjectCreator.createMyObject(new MyObjectKey("one"));
myObjectCreator.createMyObject(new MyObjectKey("two"));
myObjectCreator.createMyObject(new MyObjectKey("three"));
myObjectCreator.createMyObject(new MyObjectKey("one"));
myObjectCreator.createMyObject(new MyObjectKey("two"));
myObjectCreator.createMyObject(new MyObjectKey("three"));
}
}
-----------------------------------------------------
Here is a tar with the above java classes in it.
cacheManagment.tar