rabbit.cache
Class NCache<K,V>

java.lang.Object
  extended by rabbit.cache.NCache<K,V>
Type Parameters:
K - the key type of the cache
V - the data resource
All Implemented Interfaces:
java.lang.Runnable, Cache<K,V>

public class NCache<K,V>
extends java.lang.Object
implements Cache<K,V>, java.lang.Runnable

The NCache is like a Map in lookup/insert/delete The NCache is persistent over sessions (saves itself to disk). The NCache is selfcleaning, that is it removes old stuff.

Author:
Robert Olofsson

Constructor Summary
NCache(SProperties props, FileHandler<K> fhk, FileHandler<V> fhv)
          Create a cache that uses default values.
 
Method Summary
 void addEntry(CacheEntry<K,V> ent)
          Insert a CacheEntry into the cache.
 void clear()
          Clear the Cache from files.
 void entryChanged(CacheEntry<K,V> ent, K newKey, V newHook)
          Signal that a cache entry have changed.
 void flush()
          Make sure that the cache is written to the disk.
 java.net.URL getCacheDir()
          Get the location where this cache stores its files.
 long getCacheTime()
          Get the number of miliseconds the cache stores things usually.
 int getCleanLoopTime()
          Get how long time the cleaner sleeps between cleanups.
 long getCurrentSize()
          Get the current size of the cache
 java.util.Collection<rabbit.cache.NCacheEntry<K,V>> getEntries()
          Get the CacheEntries in the cache.
 CacheEntry<K,V> getEntry(K k)
          Get the CacheEntry assosiated with given object.
 java.lang.String getEntryName(long id, boolean real, java.lang.String extension)
          Get the file name for a cache entry.
 FileHandler<V> getHookFileHandler()
          Get the file handler for the values.
 FileHandler<K> getKeyFileHandler()
          Get the file handler for the keys.
 java.util.logging.Logger getLogger()
          Get the logger of this cache
 long getMaxSize()
          Get the maximum size for this cache.
 long getNumberOfEntries()
          Get the current number of entries in the cache.
 CacheEntry<K,V> newEntry(K k)
          Reserve space for a CacheEntry with key o.
 void remove(K k)
          Remove the Entry with key k from the cache.
 void run()
          Loop in a cleaning loop.
 void setCacheTime(long newCacheTime)
          Set the standard expiry-time for CacheEntries
 void setCleanLoopTime(int newCleanLoopTime)
          Set how long time the cleaner sleeps between cleanups.
 void setMaxSize(long newMaxSize)
          Set the maximum size for this cache.
 void setup(SProperties config)
          Configure the cache system from the given config.
 void startCleaner()
          Start the thread that cleans the cache.
 void stop()
          Stop this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NCache

public NCache(SProperties props,
              FileHandler<K> fhk,
              FileHandler<V> fhv)
       throws java.io.IOException
Create a cache that uses default values. Note that you must call startCleaner to have the cache fully up.

Parameters:
props - the configuration of the cache
fhk - the FileHandler for the cache keys
fhv - the FileHandler for the cache values
Throws:
java.io.IOException - if the cache file directory can not be configured
Method Detail

startCleaner

public void startCleaner()
Start the thread that cleans the cache.


getCacheDir

public java.net.URL getCacheDir()
Description copied from interface: Cache
Get the location where this cache stores its files.

Specified by:
getCacheDir in interface Cache<K,V>
Returns:
the location, null if no physical location is used.

getMaxSize

public long getMaxSize()
Get the maximum size for this cache.

Specified by:
getMaxSize in interface Cache<K,V>
Returns:
the maximum size in bytes this cache.

setMaxSize

public void setMaxSize(long newMaxSize)
Set the maximum size for this cache.

Specified by:
setMaxSize in interface Cache<K,V>
Parameters:
newMaxSize - the new maximum size for the cache.

getCacheTime

public long getCacheTime()
Get the number of miliseconds the cache stores things usually. This is the standard expiretime for objects, but you can set it for CacheEntries individially if you want to. NOTE 1: dont trust that an object will be in the cache this long. NOTE 2: dont trust that an object will be removed from the cache when it expires.

Specified by:
getCacheTime in interface Cache<K,V>
Returns:
the number of miliseconds objects are stored normally.

setCacheTime

public void setCacheTime(long newCacheTime)
Set the standard expiry-time for CacheEntries

Specified by:
setCacheTime in interface Cache<K,V>
Parameters:
newCacheTime - the number of miliseconds to keep objects normally.

getCleanLoopTime

public int getCleanLoopTime()
Get how long time the cleaner sleeps between cleanups.

Returns:
the number of millis between cleanups

setCleanLoopTime

public void setCleanLoopTime(int newCleanLoopTime)
Set how long time the cleaner sleeps between cleanups.

Parameters:
newCleanLoopTime - the number of miliseconds to sleep.

getCurrentSize

public long getCurrentSize()
Get the current size of the cache

Specified by:
getCurrentSize in interface Cache<K,V>
Returns:
the current size of the cache in bytes.

getNumberOfEntries

public long getNumberOfEntries()
Get the current number of entries in the cache.

Specified by:
getNumberOfEntries in interface Cache<K,V>
Returns:
the current number of entries in the cache.

getEntry

public CacheEntry<K,V> getEntry(K k)
                         throws CacheException
Get the CacheEntry assosiated with given object.

Specified by:
getEntry in interface Cache<K,V>
Parameters:
k - the key.
Returns:
the CacheEntry or null (if not found).
Throws:
CacheException - upon failure to get the key

getEntryName

public java.lang.String getEntryName(long id,
                                     boolean real,
                                     java.lang.String extension)
Get the file name for a cache entry.

Specified by:
getEntryName in interface Cache<K,V>
Parameters:
id - the id of the cache entry
real - false if this is a temporary cache file, true if it is a realized entry.
extension - the cache entry extension.
Returns:
the file name of the new entry

newEntry

public CacheEntry<K,V> newEntry(K k)
Reserve space for a CacheEntry with key o.

Specified by:
newEntry in interface Cache<K,V>
Parameters:
k - the key for the CacheEntry.
Returns:
a new CacheEntry initialized for the cache.

getKeyFileHandler

public FileHandler<K> getKeyFileHandler()
Description copied from interface: Cache
Get the file handler for the keys.

Specified by:
getKeyFileHandler in interface Cache<K,V>
Returns:
the FileHandler for the key objects

getHookFileHandler

public FileHandler<V> getHookFileHandler()
Description copied from interface: Cache
Get the file handler for the values.

Specified by:
getHookFileHandler in interface Cache<K,V>
Returns:
the FileHandler for the values

addEntry

public void addEntry(CacheEntry<K,V> ent)
              throws CacheException
Insert a CacheEntry into the cache.

Specified by:
addEntry in interface Cache<K,V>
Parameters:
ent - the CacheEntry to store.
Throws:
CacheException - if adding the entry fails

entryChanged

public void entryChanged(CacheEntry<K,V> ent,
                         K newKey,
                         V newHook)
                  throws CacheException
Signal that a cache entry have changed.

Specified by:
entryChanged in interface Cache<K,V>
Parameters:
ent - the CacheEntry that changed
newKey - the new key of the entry
newHook - the new value
Throws:
CacheException - if updating the cache fails

remove

public void remove(K k)
            throws CacheException
Remove the Entry with key k from the cache.

Specified by:
remove in interface Cache<K,V>
Parameters:
k - the key for the CacheEntry.
Throws:
CacheException - if removal fails

clear

public void clear()
           throws CacheException
Clear the Cache from files.

Specified by:
clear in interface Cache<K,V>
Throws:
CacheException - if the clear operation failed

getEntries

public java.util.Collection<rabbit.cache.NCacheEntry<K,V>> getEntries()
Get the CacheEntries in the cache. Note! some entries may be invalid if you have a corruct cache.

Specified by:
getEntries in interface Cache<K,V>
Returns:
a Collection of the CacheEntries.

flush

public void flush()
Make sure that the cache is written to the disk.

Specified by:
flush in interface Cache<K,V>

run

public void run()
Loop in a cleaning loop.

Specified by:
run in interface java.lang.Runnable

stop

public void stop()
Description copied from interface: Cache
Stop this cache. If this cache is using any cleaner threads they have to be stopped when this method is called.

Specified by:
stop in interface Cache<K,V>

setup

public void setup(SProperties config)
           throws java.io.IOException
Configure the cache system from the given config.

Parameters:
config - the properties describing the cache settings.
Throws:
java.io.IOException - if the new cache can not be configured correctly

getLogger

public java.util.logging.Logger getLogger()
Description copied from interface: Cache
Get the logger of this cache

Specified by:
getLogger in interface Cache<K,V>
Returns:
the Logger used by the cache