rabbit.cache
Class NCache<K,V>

java.lang.Object
  extended by rabbit.cache.NCache<K,V>
All Implemented Interfaces:
Runnable, Cache<K,V>

public class NCache<K,V>
extends Object
implements Cache<K,V>, 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

Field Summary
 boolean running
           
 
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.
 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
 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.
 String getEntryName(long id, boolean real, 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.
 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 setCacheDir(String newDir)
          Sets the cachedir.
 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()
           
 void stop()
          Stop this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

running

public boolean running
Constructor Detail

NCache

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

Throws:
IOException
Method Detail

startCleaner

public void startCleaner()

getCacheDir

public 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.

setCacheDir

public void setCacheDir(String newDir)
                 throws IOException
Sets the cachedir. This will flush the cache and make it try to read in the cache from the new dir.

Parameters:
newDir - the name of the new directory to use.
Throws:
IOException

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.


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

getEntryName

public String getEntryName(long id,
                           boolean real,
                           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.

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>

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>

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

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>
Throws:
CacheException

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

clear

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

Specified by:
clear in interface Cache<K,V>
Throws:
CacheException

getEntries

public 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 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 IOException
Configure the cache system from the given config.

Parameters:
config - the properties describing the cache settings.
Throws:
IOException

getLogger

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

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