com.caucho.server.distcache
Class CacheImpl

java.lang.Object
  extended by com.caucho.server.distcache.CacheImpl
All Implemented Interfaces:
ByteStreamCache, ObjectCache, java.io.Closeable, java.lang.Iterable, Cache, CacheLifecycle

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

Implements the distributed cache


Nested Class Summary
protected static class CacheImpl.CacheEntrySetIterator<K,V>
          Provides an iterator over the entries in the the local cache.
 
Nested classes/interfaces inherited from interface javax.cache.Cache
Cache.Entry<K,V>, Cache.EntryProcessor<K,V>, Cache.MutableEntry<K,V>
 
Constructor Summary
CacheImpl(CacheManagerImpl localManager, java.lang.String name, java.lang.String guid, CacheConfig config)
           
 
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()
           
 void compareAndPut(HashKey key, HashKey value, long valueLength, long version)
           
 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.
 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()
           
 CacheManager getCacheManager()
           
 CacheConfiguration getConfiguration()
           
 DataStore getDataStore()
           
protected  DistCacheEntry getDistCacheEntry(HashKey key)
          Returns the CacheKeyEntry for the given key.
protected  DistCacheEntry getDistCacheEntry(java.lang.Object key)
          Returns the CacheKeyEntry for the given key.
 java.lang.Object getExact(java.lang.Object key)
          Returns the object with the given key always checking the backing store .
 long getExpireTimeout()
          The maximum valid time for an item.
 ExtCacheEntry getExtCacheEntry(HashKey key)
           
 ExtCacheEntry getExtCacheEntry(java.lang.Object key)
          Returns the cache entry for the object with the given key.
 java.lang.String getGuid()
           
 long getIdleTimeout()
          The maximum idle time for an item, which is typically used for temporary data like sessions.
 long getIdleTimeoutWindow()
          Returns the idle check window, used to minimize traffic when updating access times.
 HashKey getKeyHash(java.lang.Object key)
          Returns the hash of the given key
 byte[] getKeyHash(java.lang.String name)
           
 long getLeaseTimeout()
          The lease timeout is the time a server can use the local version if it owns it, before a timeout.
 ExtCacheEntry getLiveCacheEntry(java.lang.Object key)
          Returns the entry for the given key, returning the live item.
 long getLocalReadTimeout()
          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()
           
 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)
           
 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 flags)
          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)
           
 HashKey saveData(java.io.InputStream is)
           
 void saveData(java.lang.Object value)
           
 void setManager(CacheStoreManager manager)
           
 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

CacheImpl

public CacheImpl(CacheManagerImpl localManager,
                 java.lang.String name,
                 java.lang.String guid,
                 CacheConfig config)
Method Detail

getName

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

Specified by:
getName in interface Cache

getCacheManager

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

getGuid

public java.lang.String getGuid()

getExpireTimeout

public long getExpireTimeout()
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.


getIdleTimeout

public long getIdleTimeout()
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.


getIdleTimeoutWindow

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


getLeaseTimeout

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


getLocalReadTimeout

public long getLocalReadTimeout()
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


createIfAbsent

public CacheImpl createIfAbsent()

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 always checking the backing store .

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

put

public ExtCacheEntry put(java.lang.Object key,
                         java.io.InputStream is,
                         long accessedExpireTimeout,
                         long modifiedExpireTimeout,
                         int flags)
                  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

compareAndPut

public void compareAndPut(HashKey key,
                          HashKey value,
                          long valueLength,
                          long version)

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

getLiveCacheEntry

public ExtCacheEntry getLiveCacheEntry(java.lang.Object key)
Returns the entry for the given key, returning the live item.


getDistCacheEntry

protected DistCacheEntry getDistCacheEntry(java.lang.Object key)
Returns the CacheKeyEntry for the given key.


getDistCacheEntry

protected DistCacheEntry getDistCacheEntry(HashKey key)
Returns the CacheKeyEntry for the given key.


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:

isBackup

public boolean isBackup()

isTriplicate

public boolean isTriplicate()

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)

saveData

public HashKey saveData(java.io.InputStream is)
                 throws java.io.IOException
Throws:
java.io.IOException

setManager

public void setManager(CacheStoreManager manager)

getMBean

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

toString

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

getConfiguration

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

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

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