net.sf.spif
Class StoreCache

java.lang.Object
  |
  +--net.sf.spif.StoreChain
        |
        +--net.sf.spif.StoreCache

public class StoreCache
extends StoreChain

A caching store chain element. This will cache some number of last recently accessed elements passing through the cache.

This class is backed by a Cache.

Note that straightforward setup of a cache may in theory break rollback semantics, but only if the rollback exception is thrown after leaving this bean, i.e. not when writing to persistent storage, but when committing (usually when leaving the store session bean). If the cache is reading through for write, if will on such cases just expose read-only uncommitted data, which is seldom a problem, but if the cache is set up to not read trhough on writes, uncommitted values may be the basis for subsequent writes. This may or may not be a problem, depending on the application (and on whether committ exceptions is actually technically plausible). If you use a system where it is, and you need this kind of security, you should wrap the session bean starting the transaction in another session bean which cleans up the cache state.

Version:
$Id: StoreCache.java,v 1.2 2003/02/05 22:15:44 bratseth Exp $
Author:
Jon S Bratseth

Field Summary
 
Fields inherited from class net.sf.spif.StoreChain
chained
 
Constructor Summary
StoreCache(boolean readThroughForWrite)
          Creates a store cache with the default size (1000)
StoreCache(int size)
          Creates a store cache with a size which will read through for write
StoreCache(int size, boolean readThroughForWrite)
          Creates a store cache with a size
 
Method Summary
 void clear()
          Forgets everything
 java.lang.Object get(Id id, boolean writeable)
          Returns an object.
 java.lang.Object getCached(Id id)
          Returns the value bound to this id in the cache, i.e no forwarding.
 void printContents()
          Prints the contents of the cache for debugging
 Id put(Id id, java.lang.Object object)
          Puts a changed or new object into the store.
 boolean remove(Id id)
          Removes and propagates
 int size()
           
 
Methods inherited from class net.sf.spif.StoreChain
equals, getChained, getChained, getChained, hashCode, setChained
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StoreCache

public StoreCache(boolean readThroughForWrite)
Creates a store cache with the default size (1000)

Parameters:
readThroughForWrite - if true this cache will propagate get(id,true) requests through the cache. If the content of the underlying may change without coming through this cache, this should be true. Otherwise it is safe to use false.

StoreCache

public StoreCache(int size,
                  boolean readThroughForWrite)
Creates a store cache with a size


StoreCache

public StoreCache(int size)
Creates a store cache with a size which will read through for write

Method Detail

get

public java.lang.Object get(Id id,
                            boolean writeable)
Returns an object. The object will be fecthed from the cache, and the request not propagated only if writeable is false and the object is in the cache.

Overrides:
get in class StoreChain
Parameters:
id - the id of the object to return
Returns:
the object, or null if the object could not be retrieved or if the input id was null

getCached

public java.lang.Object getCached(Id id)
Returns the value bound to this id in the cache, i.e no forwarding. If there's no value in the cache, null is returned.


put

public Id put(Id id,
              java.lang.Object object)
Puts a changed or new object into the store. This calls are always propagated by the cache.

Overrides:
put in class StoreChain
Parameters:
id - the id of the object to put. May be null
object - the object to put. May be null
Returns:
the id of this object. This will usually be (and is in this default implementation) the input id, unless the given id is null and there is an id generating service on the store chain

remove

public boolean remove(Id id)
Removes and propagates

Overrides:
remove in class StoreChain
Parameters:
id - the id of the object to remove
Returns:
true if the object did not exists

size

public int size()

clear

public void clear()
Forgets everything

Overrides:
clear in class StoreChain

printContents

public void printContents()
Prints the contents of the cache for debugging



Spif 1.01 (2003-04-03). http://spif.sf.net