com.caucho.server.distcache
Class CacheImpl<K,V>

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

public class CacheImpl<K,V>
extends java.lang.Object
implements ObjectCache<K,V>, ByteStreamCache, 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>
 
Constructor Summary
CacheImpl(CacheManagerImpl localManager, java.lang.String name, java.lang.String managerName, 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()
           
 boolean compareAndRemove(java.lang.Object key, long version)
          Removes the entry from the cache if the current entry matches the version.
 boolean compareVersionAndPut(java.lang.Object key, long version, java.lang.Object value)
          Updates the cache if the old version matches the current version.
 boolean containsKey(java.lang.Object key)
          Returns true if an entry for the item is found in the cache.
 CacheImpl createIfAbsent()
           
 V 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.Set keys)
          Returns a new map of the items found in the central cache.
 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.
 CacheHandle getCacheHandle()
           
 HashKey getCacheKey()
           
 CacheManager getCacheManager()
           
 CacheConfig getConfig()
           
 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.
 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.
 java.lang.String getManagerName()
           
 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()
           
 long 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(long valueDataId)
           
 boolean isTriplicate()
           
 java.util.Iterator<Cache.Entry<K,V>> iterator()
           
 java.util.concurrent.Future<V> load(K key)
           
 java.util.concurrent.Future<java.util.Map<K,? extends V>> loadAll(java.util.Set<? extends K> keys)
           
 boolean loadData(long valueDataId, WriteStream os)
           
 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.
 void put(K key, V value)
          Puts a new item in the cache.
 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).
 ExtCacheEntry put(java.lang.Object key, java.io.InputStream is, long accessedExpireTimeout, long modifiedExpireTimeout, long lastAccessTime, long lastModifiedTime)
          Puts a new item in the cache with a custom idle timeout (used for sessions).
 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 putIfNew(java.lang.Object key, MnodeUpdate update, java.io.InputStream is)
          Updates the cache if the old version matches the current value.
 boolean registerCacheEntryListener(CacheEntryListener<? super K,? super V> listener)
          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)
           
 long saveData(StreamSource source, int length)
           
 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 managerName,
                 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<K,V>

getManagerName

public java.lang.String getManagerName()

getCacheManager

public CacheManager getCacheManager()
Specified by:
getCacheManager in interface Cache<K,V>

getGuid

public java.lang.String getGuid()

getConfig

public CacheConfig getConfig()

getCacheHandle

public CacheHandle getCacheHandle()

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 V get(java.lang.Object key)
Returns the object with the given key, checking the backing store if necessary.

Specified by:
get in interface Cache<K,V>

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<K,V>

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<K,V>

getCacheEntry

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


put

public void put(K key,
                V value)
Puts a new item in the cache.

Specified by:
put in interface Cache<K,V>
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

put

public ExtCacheEntry put(java.lang.Object key,
                         java.io.InputStream is,
                         long accessedExpireTimeout,
                         long modifiedExpireTimeout,
                         long lastAccessTime,
                         long lastModifiedTime)
                  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<K,V>
Parameters:
key - the key of the item to put
value - the value of the item to put

compareVersionAndPut

public boolean compareVersionAndPut(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:
compareVersionAndPut in interface ObjectCache<K,V>
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

putIfNew

public boolean putIfNew(java.lang.Object key,
                        MnodeUpdate update,
                        java.io.InputStream is)
                 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:
putIfNew 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

putIfAbsent

public boolean putIfAbsent(java.lang.Object key,
                           java.lang.Object value)
                    throws CacheException
Specified by:
putIfAbsent in interface Cache<K,V>
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<K,V>
Throws:
CacheException

replace

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

getAndReplace

public java.lang.Object getAndReplace(java.lang.Object key,
                                      java.lang.Object value)
                               throws CacheException
Specified by:
getAndReplace in interface Cache<K,V>
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<K,V>
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<K,V>
Returns:
true if the object existed

getAndRemove

public java.lang.Object getAndRemove(java.lang.Object key)
                              throws CacheException
Specified by:
getAndRemove in interface Cache<K,V>
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<K,V>

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 final DistCacheEntry getDistCacheEntry(HashKey key)
Returns the CacheKeyEntry for the given key.


getAll

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

Specified by:
getAll in interface Cache<K,V>

registerCacheEntryListener

public boolean registerCacheEntryListener(CacheEntryListener<? super K,? super V> listener)
Adds a listener to the cache.

Specified by:
registerCacheEntryListener in interface Cache<K,V>

unregisterCacheEntryListener

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

Specified by:
unregisterCacheEntryListener in interface Cache<K,V>

getStatistics

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

Specified by:
getStatistics in interface Cache<K,V>

putAll

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

Specified by:
putAll in interface Cache<K,V>

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<K,V>
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.


isClosed

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

Specified by:
isClosed in interface ObjectCache<K,V>

close

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

loadData

public boolean loadData(long valueDataId,
                        WriteStream os)
                 throws java.io.IOException
Throws:
java.io.IOException

saveData

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

isDataAvailable

public boolean isDataAvailable(long valueDataId)

getKeyHash

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

getValueHash

public long getValueHash(java.lang.Object value)

getMnodeStore

public MnodeStore getMnodeStore()

getDataStore

public DataStore getDataStore()

getMBean

public CacheMXBean getMBean()
Specified by:
getMBean in interface Cache<K,V>

getConfiguration

public CacheConfiguration getConfiguration()
Specified by:
getConfiguration in interface Cache<K,V>

invokeEntryProcessor

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

load

public java.util.concurrent.Future<V> load(K key)
                                    throws CacheException
Specified by:
load in interface Cache<K,V>
Throws:
CacheException

loadAll

public java.util.concurrent.Future<java.util.Map<K,? extends V>> loadAll(java.util.Set<? extends K> keys)
                                                                  throws CacheException
Specified by:
loadAll in interface Cache<K,V>
Throws:
CacheException

removeAll

public void removeAll(java.util.Set keys)
Specified by:
removeAll in interface Cache<K,V>

removeAll

public void removeAll()
               throws CacheException
Specified by:
removeAll in interface Cache<K,V>
Throws:
CacheException

iterator

public java.util.Iterator<Cache.Entry<K,V>> iterator()
Specified by:
iterator in interface java.lang.Iterable<Cache.Entry<K,V>>
Specified by:
iterator in interface Cache<K,V>

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<K,V>

toString

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