common-util

local.tomas.util.cache
Class CacheMap<K,V>

java.lang.Object
  extended by local.tomas.util.cache.CacheMap<K,V>
Type Parameters:
K - type of object key
V - type of objects

public class CacheMap<K,V>
extends Object

A fixed size cache of objects.

Author:
tomas.teubner

Constructor Summary
CacheMap(CacheBackend<K,V> backend)
          Construct cache with specified backend and a default size of 1000.
CacheMap(CacheBackend<K,V> backend, int cacheSize)
          Construct cache with specified backend and size.
 
Method Summary
protected  void finalize()
           
 V get(K key)
          Get object by key.
 V getOrCreate(K key)
          Get object by key or create new object if it does not exist.
 void load()
          Load objects from backend.
 void put(K key, V value)
          Put object into cache.
 void remove(K key)
          Remove object from cache.
 void unload()
          Save all object in backend.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheMap

public CacheMap(CacheBackend<K,V> backend,
                int cacheSize)
Construct cache with specified backend and size.

Parameters:
backend - the backend
cacheSize - the size

CacheMap

public CacheMap(CacheBackend<K,V> backend)
Construct cache with specified backend and a default size of 1000.

Parameters:
backend - the backend
Method Detail

get

public V get(K key)
      throws CacheException
Get object by key.

Parameters:
key - the key
Returns:
the object
Throws:
CacheException - on backend exceptions

put

public void put(K key,
                V value)
         throws CacheException
Put object into cache.

Parameters:
key - object key
value - object value
Throws:
CacheException - on backend exceptions

getOrCreate

public V getOrCreate(K key)
              throws CacheException
Get object by key or create new object if it does not exist.

Parameters:
key - object key
Returns:
existing or new object
Throws:
CacheException - on backend exceptions

remove

public void remove(K key)
            throws CacheException
Remove object from cache.

Parameters:
key - object key
Throws:
CacheException - on backend exceptions

load

public void load()
          throws CacheException
Load objects from backend.

Throws:
CacheException - on backend exceptions

unload

public void unload()
            throws CacheException
Save all object in backend.

Throws:
CacheException - on backend exceptions

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

common-util

Copyright © 2005-2007 Tomas Teubner. All Rights Reserved.