com.caucho.distcache
Class AbstractCache

java.lang.Object
  extended by com.caucho.distcache.AbstractCache
All Implemented Interfaces:
ByteStreamCache, ObjectCache, ResinCacheBuilder, java.io.Closeable, java.lang.Iterable, Cache, CacheLifecycle
Direct Known Subclasses:
ClusterCache, LocalCache, TransientCache

public class AbstractCache
extends java.lang.Object
implements ObjectCache, ByteStreamCache, ResinCacheBuilder, java.io.Closeable

Implements the distributed cache


Nested Class Summary
 
Nested classes/interfaces inherited from interface javax.cache.Cache
Cache.Entry<K,V>, Cache.EntryProcessor<K,V>, Cache.MutableEntry<K,V>
 
Nested classes/interfaces inherited from interface com.caucho.distcache.ResinCacheBuilder
ResinCacheBuilder.Persistence, ResinCacheBuilder.Scope
 
Constructor Summary
AbstractCache()
           
 
Method Summary
protected  java.lang.Object cacheLoader(java.lang.Object key)
          Places an item in the cache from the loader unless the item is in cache already.
 void close()
           
 boolean compareAndPut(java.lang.Object key, long version, java.io.InputStream inputStream)
          Updates the cache if the old version matches the current value.
 boolean compareAndPut(java.lang.Object key, long version, java.lang.Object value)
          Updates the cache if the old version matches the current version.
 boolean compareAndRemove(java.lang.Object key, long version)
          Removes the entry from the cache if the current entry matches the version.
 boolean containsKey(java.lang.Object key)
          Returns true if an entry for the item is found in the cache.
 CacheImpl createIfAbsent()
           
 java.lang.Object get(java.lang.Object key)
          Returns the object with the given key, checking the backing store if necessary.
 boolean get(java.lang.Object key, java.io.OutputStream os)
          Fills an output stream with the value for a key.
 long getAccessedExpireTimeout()
          The maximum idle time for an item, which is typically used for temporary data like sessions.
 long getAccessedExpireTimeoutWindow()
          Returns the idle check window, used to minimize traffic when updating access times.
 java.util.Map getAll(java.util.Collection keys)
          Returns a new map of the items found in the central cache.
 java.util.Map getAll(java.util.Set keys)
           
 java.lang.Object getAndPut(java.lang.Object key, java.lang.Object value)
          Puts a new item in the cache.
 java.lang.Object getAndRemove(java.lang.Object key)
           
 java.lang.Object getAndReplace(java.lang.Object key, java.lang.Object value)
           
 Cache.Entry getCacheEntry(java.lang.Object key)
          Returns the cache entry for the object with the given key.
 HashKey getCacheKey()
           
 CacheManagerFacade getCacheManager()
           
 CacheConfig getConfig()
           
 CacheConfiguration getConfiguration()
           
 DataStore getDataStore()
           
 java.lang.Object getExact(java.lang.Object key)
          Returns the object with the given key, checking the backing store if necessary.
 ExtCacheEntry getExtCacheEntry(HashKey key)
           
 ExtCacheEntry getExtCacheEntry(java.lang.Object key)
          Returns the cache entry for the object with the given key.
 HashKey getKeyHash(java.lang.Object key)
          Returns the hash of the given key
 byte[] getKeyHash(java.lang.String name)
           
 long getLeaseExpireTimeout()
          The lease timeout is the time a server can use the local version if it owns it, before a timeout.
 long getLocalExpireTimeout()
          The local read timeout is how long a local copy of a cache item can be reused before checking with the master copy.
 CacheMXBean getMBean()
           
 MnodeStore getMnodeStore()
           
 long getModifiedExpireTimeout()
          The maximum valid time for an item.
 java.lang.String getName()
          Returns the name of the cache.
 ExtCacheEntry getStatCacheEntry(java.lang.Object key)
           
 CacheStatistics getStatistics()
          Returns the CacheStatistics for this cache.
 Status getStatus()
           
 byte[] getValueHash(java.lang.Object value)
           
 void init()
          Initialize the cache.
 java.lang.Object invokeEntryProcessor(java.lang.Object key, Cache.EntryProcessor entryProcessor)
           
 boolean isBackup()
           
 boolean isClosed()
          Returns true if the cache is closed
 boolean isDataAvailable(HashKey valueKey)
           
 boolean isTriplicate()
           
 java.util.Iterator iterator()
           
 java.util.concurrent.Future load(java.lang.Object key)
           
 java.util.concurrent.Future loadAll(java.util.Set keys)
           
 boolean loadData(HashKey valueHash, WriteStream os)
           
protected  void notifyClear(java.lang.Object key)
           
protected  void notifyEvict(java.lang.Object key)
           
protected  void notifyLoad(java.lang.Object key)
           
protected  void notifyPut(java.lang.Object key)
           
protected  void notifyRemove(java.lang.Object key)
           
 java.lang.Object peek(java.lang.Object key)
          Returns the object with the given key without checking the backing store.
 ExtCacheEntry peekExtCacheEntry(java.lang.Object key)
          Returns the cache entry for the object with the given key.
 ExtCacheEntry put(java.lang.Object key, java.io.InputStream is, long accessedExpireTimeout, long modifiedExpireTimeout)
          Puts a new item in the cache with a custom idle timeout (used for sessions).
 ExtCacheEntry put(java.lang.Object key, java.io.InputStream is, long accessedExpireTimeout, long modifiedExpireTimeout, int userFlags)
          Puts a new item in the cache with a custom idle timeout (used for sessions).
 void put(java.lang.Object key, java.lang.Object value)
          Puts a new item in the cache.
 void putAll(java.util.Map map)
          Puts each item in the map into the cache.
 boolean putIfAbsent(java.lang.Object key, java.lang.Object value)
           
 boolean registerCacheEntryListener(CacheEntryListener listener, Filter filter)
          Adds a listener to the cache.
 boolean remove(java.lang.Object key)
          Removes the entry from the cache.
 boolean remove(java.lang.Object key, java.lang.Object oldValue)
          Removes the entry from the cache.
 void removeAll()
           
 void removeAll(java.util.Set keys)
           
 boolean replace(java.lang.Object key, java.lang.Object value)
           
 boolean replace(java.lang.Object key, java.lang.Object oldValue, java.lang.Object value)
           
 boolean saveData(HashKey valueHash, StreamSource source, int length)
           
 void saveData(java.lang.Object value)
           
 void setAccessedExpireTimeout(Period period)
          The maximum idle time for an item, which is typically used for temporary data like sessions.
 void setAccessedExpireTimeoutMillis(long timeout)
          Sets the idle timeout in milliseconds
 void setAccessedExpireTimeoutWindow(Period period)
          Sets the idle timeout windows
 void setCacheLoader(CacheLoader loader)
          Sets the CacheLoader that the Cache can then use to populate cache misses from a reference store (database).
 void setCacheManager(CacheManagerFacade cacheManager)
           
 void setEngine(CacheEngine engine)
           
 void setExpireTimeout(Period expireTimeout)
          Backwards compat.
 void setIdleTimeout(Period period)
           
 void setLeaseExpireTimeout(Period period)
          The lease timeout is the time a server can use the local version if it owns it, before a timeout.
 void setLeaseExpireTimeoutMillis(long timeout)
          The lease timeout is the time a server can use the local version if it owns it, before a timeout.
 void setLeaseTimeout(Period period)
           
 void setLocalExpireTimeout(Period period)
          The local read timeout sets how long a local copy of a cache item can be reused before checking with the master copy.
 void setLocalExpireTimeoutMillis(long period)
          The local read timeout sets how long a local copy of a cache item can be reused before checking with the master copy.
 void setLocalReadTimeout(Period period)
          Backwards compat.
 void setManagerName(java.lang.String managerName)
           
 void setModifiedExpireTimeout(Period expireTimeout)
          The maximum valid time for a cached item before it expires.
 void setModifiedExpireTimeoutMillis(long expireTimeout)
          The maximum valid time for an item.
 void setName(java.lang.String name)
          Assigns the name of the cache.
 void setPersistenceMode(ResinCacheBuilder.Persistence persistence)
           
 void setScopeMode(ResinCacheBuilder.Scope scope)
           
 void setSerializer(CacheSerializer serializer)
          Assign the serializer used on values.
 void start()
           
 void stop()
           
 java.lang.String toString()
           
 boolean unregisterCacheEntryListener(CacheEntryListener listener)
          Removes a listener from the cache.
 java.lang.Object unwrap(java.lang.Class cl)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractCache

public AbstractCache()
Method Detail

getName

public java.lang.String getName()
Returns the name of the cache.

Specified by:
getName in interface Cache

setName

@Configurable
public void setName(java.lang.String name)
Assigns the name of the cache. A name is mandatory and must be unique among open caches.


setManagerName

@Configurable
public void setManagerName(java.lang.String managerName)

getCacheManager

public CacheManagerFacade getCacheManager()
Specified by:
getCacheManager in interface Cache

setCacheManager

public void setCacheManager(CacheManagerFacade cacheManager)

setCacheLoader

@Configurable
public void setCacheLoader(CacheLoader loader)
Sets the CacheLoader that the Cache can then use to populate cache misses from a reference store (database).


setSerializer

@Configurable
public void setSerializer(CacheSerializer serializer)
Assign the serializer used on values.


setEngine

public void setEngine(CacheEngine engine)

setAccessedExpireTimeout

@Configurable
public void setAccessedExpireTimeout(Period period)
The maximum idle time for an item, which is typically used for temporary data like sessions. For example, session data might be removed if idle over 30 minutes.

Cached data would have infinite idle time because it doesn't depend on how often it's accessed.

Default is infinite.


setIdleTimeout

@Configurable
public void setIdleTimeout(Period period)

getAccessedExpireTimeout

public long getAccessedExpireTimeout()
The maximum idle time for an item, which is typically used for temporary data like sessions. For example, session data might be removed if idle over 30 minutes.

Cached data would have infinite idle time because it doesn't depend on how often it's accessed.

Default is infinite.


setAccessedExpireTimeoutMillis

@Configurable
public void setAccessedExpireTimeoutMillis(long timeout)
Sets the idle timeout in milliseconds


getAccessedExpireTimeoutWindow

public long getAccessedExpireTimeoutWindow()
Returns the idle check window, used to minimize traffic when updating access times.


setAccessedExpireTimeoutWindow

public void setAccessedExpireTimeoutWindow(Period period)
Sets the idle timeout windows


getModifiedExpireTimeout

public long getModifiedExpireTimeout()
The maximum valid time for an item. Items stored in the cache for longer than the expire time are no longer valid and z null value will be returned for a get.

Default is infinite.


setModifiedExpireTimeout

@Configurable
public void setModifiedExpireTimeout(Period expireTimeout)
The maximum valid time for a cached item before it expires. Items stored in the cache for longer than the expire time are no longer valid and will return null from a get.

Default is infinite.


setExpireTimeout

@Configurable
public void setExpireTimeout(Period expireTimeout)
Backwards compat.


setModifiedExpireTimeoutMillis

@Configurable
public void setModifiedExpireTimeoutMillis(long expireTimeout)
The maximum valid time for an item. Items stored in the cache for longer than the expire time are no longer valid and will return null from a get.

Default is infinite.


getLeaseExpireTimeout

public long getLeaseExpireTimeout()
The lease timeout is the time a server can use the local version if it owns it, before a timeout.


setLeaseExpireTimeout

@Configurable
public void setLeaseExpireTimeout(Period period)
The lease timeout is the time a server can use the local version if it owns it, before a timeout.


setLeaseTimeout

@Configurable
public void setLeaseTimeout(Period period)

setLeaseExpireTimeoutMillis

@Configurable
public void setLeaseExpireTimeoutMillis(long timeout)
The lease timeout is the time a server can use the local version if it owns it, before a timeout.


getLocalExpireTimeout

public long getLocalExpireTimeout()
The local read timeout is how long a local copy of a cache item can be reused before checking with the master copy.

A read-only item could be infinite (-1). A slow changing item like a list of bulletin-board comments could be 10s. Even a relatively quickly changing item can be 10ms or 100ms.

The default is 10ms


setLocalExpireTimeout

@Configurable
public void setLocalExpireTimeout(Period period)
The local read timeout sets how long a local copy of a cache item can be reused before checking with the master copy.

A read-only item could be infinite (-1). A slow changing item like a list of bulletin-board comments could be 10s. Even a relatively quicky changing item can be 10ms or 100ms.

The default is 10ms


setLocalReadTimeout

@Configurable
public void setLocalReadTimeout(Period period)
Backwards compat.


setLocalExpireTimeoutMillis

@Configurable
public void setLocalExpireTimeoutMillis(long period)
The local read timeout sets how long a local copy of a cache item can be reused before checking with the master copy.

A read-only item could be infinite (-1). A slow changing item like a list of bulletin-board comments could be 10s. Even a relatively quicky changing item can be 10ms or 100ms.

The default is 10ms


setScopeMode

public void setScopeMode(ResinCacheBuilder.Scope scope)

isBackup

public boolean isBackup()

isTriplicate

public boolean isTriplicate()

setPersistenceMode

public void setPersistenceMode(ResinCacheBuilder.Persistence persistence)

peek

public java.lang.Object peek(java.lang.Object key)
Returns the object with the given key without checking the backing store.


getKeyHash

public HashKey getKeyHash(java.lang.Object key)
Returns the hash of the given key


get

public java.lang.Object get(java.lang.Object key)
Returns the object with the given key, checking the backing store if necessary.

Specified by:
get in interface Cache

getExact

public java.lang.Object getExact(java.lang.Object key)
Returns the object with the given key, checking the backing store if necessary.

Specified by:
getExact in interface ObjectCache

get

public boolean get(java.lang.Object key,
                   java.io.OutputStream os)
            throws java.io.IOException
Fills an output stream with the value for a key.

Specified by:
get in interface ByteStreamCache
Throws:
java.io.IOException

getExtCacheEntry

public ExtCacheEntry getExtCacheEntry(java.lang.Object key)
Returns the cache entry for the object with the given key.

Specified by:
getExtCacheEntry in interface ByteStreamCache
Specified by:
getExtCacheEntry in interface ObjectCache

getExtCacheEntry

public ExtCacheEntry getExtCacheEntry(HashKey key)

peekExtCacheEntry

public ExtCacheEntry peekExtCacheEntry(java.lang.Object key)
Returns the cache entry for the object with the given key.

Specified by:
peekExtCacheEntry in interface ByteStreamCache

getStatCacheEntry

public ExtCacheEntry getStatCacheEntry(java.lang.Object key)
Specified by:
getStatCacheEntry in interface ObjectCache

getCacheEntry

public Cache.Entry getCacheEntry(java.lang.Object key)
Returns the cache entry for the object with the given key.


put

public void put(java.lang.Object key,
                java.lang.Object value)
Puts a new item in the cache.

Specified by:
put in interface Cache
Parameters:
key - the key of the item to put
value - the value of the item to put

putIfAbsent

public boolean putIfAbsent(java.lang.Object key,
                           java.lang.Object value)
                    throws CacheException
Specified by:
putIfAbsent in interface Cache
Throws:
CacheException

replace

public boolean replace(java.lang.Object key,
                       java.lang.Object oldValue,
                       java.lang.Object value)
                throws CacheException
Specified by:
replace in interface Cache
Throws:
CacheException

replace

public boolean replace(java.lang.Object key,
                       java.lang.Object value)
                throws CacheException
Specified by:
replace in interface Cache
Throws:
CacheException

getAndReplace

public java.lang.Object getAndReplace(java.lang.Object key,
                                      java.lang.Object value)
                               throws CacheException
Specified by:
getAndReplace in interface Cache
Throws:
CacheException

remove

public boolean remove(java.lang.Object key)
Removes the entry from the cache.

Specified by:
remove in interface ByteStreamCache
Specified by:
remove in interface Cache
Returns:
true if the object existed

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object oldValue)
Removes the entry from the cache.

Specified by:
remove in interface Cache
Returns:
true if the object existed

getAndRemove

public java.lang.Object getAndRemove(java.lang.Object key)
                              throws CacheException
Specified by:
getAndRemove in interface Cache
Throws:
CacheException

compareAndRemove

public boolean compareAndRemove(java.lang.Object key,
                                long version)
Removes the entry from the cache if the current entry matches the version.

Specified by:
compareAndRemove in interface ByteStreamCache
Specified by:
compareAndRemove in interface ObjectCache

load

public java.util.concurrent.Future load(java.lang.Object key)
                                 throws CacheException
Specified by:
load in interface Cache
Throws:
CacheException

loadAll

public java.util.concurrent.Future loadAll(java.util.Set keys)
                                    throws CacheException
Specified by:
loadAll in interface Cache
Throws:
CacheException

removeAll

public void removeAll()
               throws CacheException
Specified by:
removeAll in interface Cache
Throws:
CacheException

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface Cache

getStatus

public Status getStatus()
Specified by:
getStatus in interface CacheLifecycle

start

public void start()
           throws CacheException
Specified by:
start in interface CacheLifecycle
Throws:
CacheException

stop

public void stop()
          throws CacheException
Specified by:
stop in interface CacheLifecycle
Throws:
CacheException

unwrap

public java.lang.Object unwrap(java.lang.Class cl)
Specified by:
unwrap in interface Cache

put

public ExtCacheEntry put(java.lang.Object key,
                         java.io.InputStream is,
                         long accessedExpireTimeout,
                         long modifiedExpireTimeout,
                         int userFlags)
                  throws java.io.IOException
Puts a new item in the cache with a custom idle timeout (used for sessions).

Specified by:
put in interface ByteStreamCache
Parameters:
key - the key of the item to put
is - the value of the item to put
idleTimeout - the idle timeout for the item
flags - the flags value (for memcache)
Throws:
java.io.IOException

put

public ExtCacheEntry put(java.lang.Object key,
                         java.io.InputStream is,
                         long accessedExpireTimeout,
                         long modifiedExpireTimeout)
                  throws java.io.IOException
Puts a new item in the cache with a custom idle timeout (used for sessions).

Specified by:
put in interface ByteStreamCache
Parameters:
key - the key of the item to put
is - the value of the item to put
idleTimeout - the idle timeout for the item
Throws:
java.io.IOException

getAndPut

public java.lang.Object getAndPut(java.lang.Object key,
                                  java.lang.Object value)
Puts a new item in the cache.

Specified by:
getAndPut in interface Cache
Parameters:
key - the key of the item to put
value - the value of the item to put

compareAndPut

public boolean compareAndPut(java.lang.Object key,
                             long version,
                             java.lang.Object value)
Updates the cache if the old version matches the current version. A zero value for the old value hash only adds the entry if it's new.

Specified by:
compareAndPut in interface ObjectCache
Parameters:
key - the key to compare
version - the version of the old value, returned by getEntry
value - the new value
Returns:
true if the update succeeds, false if it fails

compareAndPut

public boolean compareAndPut(java.lang.Object key,
                             long version,
                             java.io.InputStream inputStream)
                      throws java.io.IOException
Updates the cache if the old version matches the current value. A zero value for the old version only adds the entry if it's new.

Specified by:
compareAndPut in interface ByteStreamCache
Parameters:
key - the key to compare
version - the hash of the old version, returned by getEntry
inputStream - the new value
Returns:
true if the update succeeds, false if it fails
Throws:
java.io.IOException

getAll

public java.util.Map getAll(java.util.Collection keys)
Returns a new map of the items found in the central cache.


registerCacheEntryListener

public boolean registerCacheEntryListener(CacheEntryListener listener,
                                          Filter filter)
Adds a listener to the cache.

Specified by:
registerCacheEntryListener in interface Cache

unregisterCacheEntryListener

public boolean unregisterCacheEntryListener(CacheEntryListener listener)
Removes a listener from the cache.

Specified by:
unregisterCacheEntryListener in interface Cache

getStatistics

public CacheStatistics getStatistics()
Returns the CacheStatistics for this cache.

Specified by:
getStatistics in interface Cache

putAll

public void putAll(java.util.Map map)
Puts each item in the map into the cache.

Specified by:
putAll in interface Cache

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if an entry for the item is found in the cache.

Specified by:
containsKey in interface Cache
Parameters:
key -
Returns:

getCacheKey

public HashKey getCacheKey()

cacheLoader

protected java.lang.Object cacheLoader(java.lang.Object key)
Places an item in the cache from the loader unless the item is in cache already.


notifyLoad

protected void notifyLoad(java.lang.Object key)

notifyEvict

protected void notifyEvict(java.lang.Object key)

notifyClear

protected void notifyClear(java.lang.Object key)

notifyPut

protected void notifyPut(java.lang.Object key)

notifyRemove

protected void notifyRemove(java.lang.Object key)

isClosed

public boolean isClosed()
Description copied from interface: ObjectCache
Returns true if the cache is closed

Specified by:
isClosed in interface ObjectCache

close

public void close()
Specified by:
close in interface java.io.Closeable

loadData

public boolean loadData(HashKey valueHash,
                        WriteStream os)
                 throws java.io.IOException
Throws:
java.io.IOException

saveData

public boolean saveData(HashKey valueHash,
                        StreamSource source,
                        int length)
                 throws java.io.IOException
Throws:
java.io.IOException

isDataAvailable

public boolean isDataAvailable(HashKey valueKey)

getKeyHash

public byte[] getKeyHash(java.lang.String name)

getValueHash

public byte[] getValueHash(java.lang.Object value)

getMnodeStore

public MnodeStore getMnodeStore()

getDataStore

public DataStore getDataStore()

saveData

public void saveData(java.lang.Object value)

createIfAbsent

public CacheImpl createIfAbsent()

init

@PostConstruct
public void init()
Initialize the cache.


getConfiguration

public CacheConfiguration getConfiguration()
Specified by:
getConfiguration in interface Cache

getConfig

public CacheConfig getConfig()

getAll

public java.util.Map getAll(java.util.Set keys)
Specified by:
getAll in interface Cache

invokeEntryProcessor

public java.lang.Object invokeEntryProcessor(java.lang.Object key,
                                             Cache.EntryProcessor entryProcessor)
Specified by:
invokeEntryProcessor in interface Cache

removeAll

public void removeAll(java.util.Set keys)
Specified by:
removeAll in interface Cache

getMBean

public CacheMXBean getMBean()
Specified by:
getMBean in interface Cache

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object