rabbit.cache.ncache
Class NCache<K,V>

java.lang.Object
  extended by rabbit.cache.ncache.NCache<K,V>
Type Parameters:
K - the key type of the cache
V - the data resource
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

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.
 CacheConfiguration getCacheConfiguration()
          Get the cache configuration for this cache.
 int getCleanLoopTime()
          Get how long time the cleaner sleeps between cleanups.
 long getCurrentSize()
          Get the current size of the cache
 Iterable<rabbit.cache.ncache.NCacheEntry<K,V>> getEntries()
          Get the CacheEntries in the cache.
 CacheEntry<K,V> getEntry(K k)
          Get the CacheEntry assosiated with given object.
 File getEntryName(long id, boolean real, String extension)
          Get the file name for a cache entry.
 Logger getLogger()
          Get the logger of 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 setCleanLoopTime(int newCleanLoopTime)
          Set how long time the cleaner sleeps between cleanups.
 void setup(SProperties config)
          Configure the cache system from the given config.
 void start()
          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 IOException
Create a cache that uses default values. Note that you must call start 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:
IOException - if the cache file directory can not be configured
Method Detail

start

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

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

getCacheConfiguration

public CacheConfiguration getCacheConfiguration()
Description copied from interface: Cache
Get the cache configuration for this cache.

Specified by:
getCacheConfiguration in interface Cache<K,V>
Returns:
the current configuration of the cache

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 File getEntryName(long id,
                         boolean real,
                         String extension)
Description copied from interface: Cache
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 for 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.

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 Iterable<rabbit.cache.ncache.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 - if the new cache can not be configured correctly

getLogger

public 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