|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.util.Cache
This is a generic caching system allowing LRU, time sensitive, time limited and pooling caches.
Usages:
Cache fileCache = new cache( 100); // If more than 10, close oldest
fileCache.ageingCache( 12 * 60, true); // files will be reloaded every 12 hours
Cache sessionCache = new cache( 1000); // sessions will expire, not get pushed out because there are too many
sessionCache.ageingCache( 20, false); // sessions expire after 20 minutes
if (! sessionCache.full()) sessionCache.add( key, session);
sessionCache.setExpiry( 60);
if (logout()) sessionCache.expire( key);
Cache zipCache = new cache( "zip.cache.size", 10); // get size from system properties file (or default if no property)
ZipFile zipFile = (ZipFile) zipCache.get( zipFileName);
if (zipFile == null)
{
zipFile = openZipFile( zipFileName);
cache.add( zipFileName, zipFile);
}
use( zipFile);
finally { cache.unlock( zipFileName); }
Cache pool = new cache( 100); // up to 100 pooled connections can be open instanteously
pool.ageingCache( 5, false); // Any connection unused for 5 minutes will be closed.
//pool.ageingCache( 60, true); // connections will be closed and reopened if over 1 hours old
Connection connection = pool.get(); // get the LRU entry from the pool
if (connection == null) pool.add( new Connection()); // no more, create a new one
use( connection);
cache.unlock( connection);
| Constructor Summary | |
Cache(int maximumWeight)
Initialise cache with maximum number of entries before the oldest are cleared. |
|
Cache(String property,
int maximumSize)
Initialise cache with maximum number of entries from the properties file before the oldest are cleared. |
|
Cache(String property,
int expiryMinutes,
boolean fixedExpiryTime)
Initialise time-sensitive cache with the default amount of time before an entry expires. |
|
| Method Summary | |
void |
add(Closeable object)
For pools - a special sort of cache where items are retrieved on LRU basis. |
boolean |
add(int key,
Closeable object)
Add an object to the cache with an integer key. |
boolean |
add(int key,
Object object)
Add an object to the cache with an integer key. |
boolean |
add(Object key,
Closeable object)
Add an object to the cache. |
boolean |
add(Object key,
Object object)
Add an object to the cache. |
void |
ageingCache(int expiryMinutes,
boolean fixedExpiryTime)
Call to create an ageing cache. |
int |
clear()
Delete all entries in the cache and remove them. |
boolean |
close()
Delete all entries from cache then close cache so that all resources can be released. |
static boolean |
closeAll()
If some caches hold critical data that needs to be preserved, either close the specific cache with close() or close all caches before stopping the program. |
boolean |
delete(Object key)
Delete and remove an object from the cache. |
boolean |
exists(Object key)
Check to see if an item exists in the cache. |
boolean |
expire(Object key)
Forced delete for ageing caches - removes even if not expired. |
boolean |
full()
See if the cache is full. |
Object |
get()
Retrieve the most used item from the cache (pooling). |
Object |
get(Object key)
Retrieve an object from the cache. |
boolean |
isAgeingCache()
See if cache items can expire due to age as well as on LRU basis. |
void |
setCacheSize(int size)
Reset the maximum number of entries in the cache. |
boolean |
setExpiry(Object key,
int minutes)
Set the expiry time for an entry. |
int |
size()
Return the number of active items currently in the cache pool. |
boolean |
unlock(Object key)
Unlock an object previously retrieved by get(). |
boolean |
unlockQuietly(Object key)
Unlock an object previously retrieved by get(). |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public Cache(String property,
int maximumSize)
property - an integer system property entry holding the size when it
starts flushing.maximumSize - size to make cache if not in properties file
public Cache(String property,
int expiryMinutes,
boolean fixedExpiryTime)
property - an integer system property entry holding the size when it
starts flushing.expiryMinutes - time before cache item expires and is removed.fixedExpiryTime - true means time from cache entry, false for time from last
retrieval.public Cache(int maximumWeight)
maximumWeight - the size when the cache starts flushing.| Method Detail |
public void ageingCache(int expiryMinutes,
boolean fixedExpiryTime)
expiryMinutes - time after creation/access that an item is considered stainfixedExpiryTime - true to make expiry happen at that time after creation. Otherwise
expiry will be reset every time the item is unlocked (not quietly).public boolean isAgeingCache()
public boolean exists(Object key)
key - of item to look for in the cache.
public Object get()
public Object get(Object key)
key - of cached item to retrieve.
public boolean unlock(Object key)
key - of cached item to unlock.
public boolean unlockQuietly(Object key)
key - of cached item to unlock.
public boolean full()
if (! cache.full()) cache.add( key, item);
public void add(Closeable object)
object - to add
public boolean add(Object key,
Closeable object)
key - cache access key for objectobject - to add
public boolean add(Object key,
Object object)
key - cache access key for objectobject - to add
public boolean add(int key,
Closeable object)
key - cache access key for objectobject - to add
public boolean add(int key,
Object object)
key - cache access key for objectobject - to add
public boolean setExpiry(Object key,
int minutes)
key - to finding the entry.minutes - after which the item is considered expired
public boolean delete(Object key)
key - of cache entry to delete.
public boolean expire(Object key)
key - of cached entry to expure and delete.
public int clear()
public boolean close()
public static boolean closeAll()
public static void main( String[] args )
{
try
{
// ...
}
finally
{ Cache.closeAll(); }
}
public void setCacheSize(int size)
size - to set the cache to.public int size()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||