org.nees.util.cache.burst
Class Cache

java.lang.Object
  extended byorg.nees.util.cache.burst.Cache
Direct Known Subclasses:
MetadataClassCache

public class Cache
extends java.lang.Object

A cache, backed by an expensive backing store. Each object has a TTL and fetches result in batch requests against the backing store that pre-fetch a fixed-length batch of objects that are due to expire.

This kind of caching strategy is optimized for bursty access patterns.

Does nothing to control cache size; will grow indefinitely.


Nested Class Summary
static interface Cache.BatchStore
          Implement this to provide a backing store to a cache.
protected  class Cache.Entry
           
protected  class Cache.ExpirationTimeComparator
           
 
Constructor Summary
Cache(Cache.BatchStore s)
           
 
Method Summary
 void clear()
          Clears the cache.
 java.lang.Object[] fetch(java.lang.Object[] k, long ttl)
          Get objects from the backing store.
 java.lang.Object fetch(java.lang.Object k, long ttl)
          Get an object from the backing store
 java.lang.Object get(java.lang.Object k, long ttl)
          Get an object from the cache, or from the backing store if it's expired hasn't been fetched before.
 int getBatchSize()
          Get the number of objects to retrieve each batch, if necessary
 long getPrefetchInterval()
          Get the prefetch interval
protected  Cache.BatchStore getStore()
           
 void put(java.lang.Object k, java.lang.Object v, long ttl)
           
 void setBatchSize(int bs)
          Set the number of objects to retrieve each batch, if necessary
 void setPrefetchInterval(long ms)
          Set the prefetch interval.
protected  void setStore(Cache.BatchStore s)
           
 void shrink(double factor)
          Shrinks the cache by discarding n of the closest-to-expiring objects where n is a given fraction of the cache's total size.
 int size()
          Get the size of the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(Cache.BatchStore s)
Method Detail

size

public int size()
Get the size of the cache.


setBatchSize

public void setBatchSize(int bs)
Set the number of objects to retrieve each batch, if necessary

Parameters:
bs - the batch size

getBatchSize

public int getBatchSize()
Get the number of objects to retrieve each batch, if necessary

Returns:
the batchSize

setPrefetchInterval

public void setPrefetchInterval(long ms)
Set the prefetch interval. This is the amount of time before an object expires to retrieve it, if there is enough room left in the batch to do so.

Parameters:
ms - the prefetch interval in ms

getPrefetchInterval

public long getPrefetchInterval()
Get the prefetch interval

Returns:
the prefetch interval

setStore

protected void setStore(Cache.BatchStore s)

getStore

protected Cache.BatchStore getStore()

put

public void put(java.lang.Object k,
                java.lang.Object v,
                long ttl)
Parameters:
ttl - time-to-live in ms

get

public java.lang.Object get(java.lang.Object k,
                            long ttl)
Get an object from the cache, or from the backing store if it's expired hasn't been fetched before. Also pre-fetch other about-to-expire or expired objects, up the batch size.

Parameters:
k - the key
ttl - the ttl, if the object has never been fetched before

fetch

public java.lang.Object[] fetch(java.lang.Object[] k,
                                long ttl)
Get objects from the backing store.

Parameters:
k - the keys
ttl - the time-to-live for the objects.

fetch

public java.lang.Object fetch(java.lang.Object k,
                              long ttl)
Get an object from the backing store


clear

public void clear()
Clears the cache.


shrink

public void shrink(double factor)
Shrinks the cache by discarding n of the closest-to-expiring objects where n is a given fraction of the cache's total size.

Parameters:
factor - the proportion of the total size to discard.