public class Cache extends AbstractCache implements java.io.Serializable
Clients of this service must first implement CacheAdapter
,
which will be called by this class to maintain elements as related to
keys. The Cache class will then take care of maintaining the cache.
Most recent changes address multithreading problems. Specifically, when multiple threads are all requesting the object for a single key for the first time, the adapter is only asked to do the work once. Likewise, the same object is guaranteed to be returned to each thread in this scenario.
This cache class may be used in non-pass-through manner, by simply not setting the cacheAdapter property. Memory restrictions will be ignored, and get() calls will return null Previously, if multiple threads all asked for an object that wasn't in cache and all used the same key, each thread would receive functionally similar but unique objects from the cache. This is a performance hit, and can also cause havoc with applications that expect all threads to receive a single object from cache.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CLASS_VERSION |
SERVICE_INFO_KEY
DEFAULT_LOG_TRACE_STATUS
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
Constructor and Description |
---|
Cache()
Constructor
|
Modifier and Type | Method and Description |
---|---|
boolean |
contains(java.lang.Object pKey)
Checks for the presence of the object with the specified key, without
invoking the CacheAdapter.
|
boolean |
contains(java.lang.Object[] pKeys)
Returns false unless all specified keys refer to objects in the cache.
|
java.lang.String |
dump() |
void |
dump(java.io.PrintStream aStream)
meant to be a debugging aid, will print current key/value pairs the mMap hashtable
to the supplied PrintStream, a prime example of which is System.out
|
void |
flush()
Flushes all entries from the cache, leaving it empty.
|
java.lang.Object |
get(java.lang.Object pKey)
Searches the cache for the object with the specified key.
|
java.lang.Object[] |
get(java.lang.Object[] pKeys)
Searches the cache for the objects with the specified keys.
|
java.lang.Object |
get(java.lang.Object pKey,
boolean pCreate)
Searches the cache for the object with the specified key.
|
int |
getAccessCount()
Returns the total number of accesses
|
java.util.Iterator |
getAllElements()
Returns all elements in this Cache.
|
java.util.Iterator |
getAllKeys()
Returns all elements in this Cache.
|
java.util.Iterator |
getAllKeysOrdered(boolean pLatestFirst)
Returns all elements in this Cache ordered by entry creation time.
|
boolean |
getEnablePartialKeyInvalidation()
Whether this cache should invalidate by part of the key.
|
int |
getHitCount()
Returns the number of cache hits
|
double |
getHitRatio()
Returns the number of cache hits / total number of cache accesses
|
int[] |
getKeyPartsForInvalidation()
Which key parts should be used for invalidation.
|
int |
getNumCacheEntries()
Returns the number of entries currently in the cache
|
int |
getNumKeyParts()
The number of key parts this cache expects.
|
int |
getTimeoutCount()
Returns the number of times entries were invalidated because they
were out-of-date.
|
double |
getUsedCapacity()
Returns the ratio of cache entries to maximum cache entries.
|
int |
getUsedMemory()
Returns the amount of memory taken up by the entries in the cache
(includes keys).
|
java.util.Iterator |
getValidKeysOrdered(boolean pLatestFirst)
Returns all elements in this Cache ordered by entry creation time.
|
protected boolean |
hasExpiredValue(atg.service.cache.Cache.Entry pEntry)
Return whether the specified entry has expired.
|
protected boolean |
hasOtherwiseExpired(java.lang.Object pKey,
java.lang.Object pValue)
Provide an extension point for adding additional expiration
logic.
|
protected boolean |
isExpiredValueStillGood(java.lang.Object key,
java.lang.Object pValue)
Return whether the expired value is still good.
|
static void |
main(java.lang.String[] args) |
void |
put(java.lang.Object pKey,
java.lang.Object pValue)
Puts an entry into the cache, placing that entry at the front of
the LRU list.
|
boolean |
remove(java.lang.Object pKey)
Invalidates the entry with the given key.
|
boolean |
remove(java.lang.Object pKey,
java.lang.Object pNewValue)
Invalidates the entry with the given key.
|
int |
removeByPartialKey(int pPartialKeyIndex,
java.lang.Object pPartialKeyValue)
Removes by partial key.
|
void |
setEnablePartialKeyInvalidation(boolean pEnablePartialKeyInvalidation)
Whether this cache should invalidate by part of the key.
|
void |
setKeyPartsForInvalidation(int[] pKeyPartsForInvalidation)
Which key parts should be used for invalidation.
|
void |
setNumKeyParts(int pNumKeyParts)
The number of key parts this cache expects.
|
java.lang.Object |
setWaiterAndGetValue(java.lang.Object pKey,
boolean pCacheIt)
this method just gets an object from a cacheadapter,
but in addition sets waiter semaphores to prevent the duplication
of cache adapter work.
|
getCacheAdapter, getCacheRemovalListener, getMaximumCacheEntries, getMaximumCacheSize, getMaximumEntryLifetime, getMaximumEntrySize, getSize, getUsedMemoryCapacity, isCaching, setCacheAdapter, setCacheRemovalListener, setMaximumCacheEntries, setMaximumCacheSize, setMaximumEntryLifetime, setMaximumEntrySize
addLogListener, createAdminServlet, doStartService, doStopService, getAbsoluteName, getAdminServlet, getAdminServletOutputStreamEncoding, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isAdminServletUseServletOutputStream, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, reResolveThis, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setAdminServletOutputStreamEncoding, setAdminServletUseServletOutputStream, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogDebugTrace, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
public static final java.lang.String CLASS_VERSION
public java.lang.Object setWaiterAndGetValue(java.lang.Object pKey, boolean pCacheIt) throws java.lang.Exception
pKey
- the key to use for fetching from the CacheAdapterpCacheIt
- whether to put the object in the cache.java.lang.Exception
public boolean contains(java.lang.Object pKey)
AbstractCache
contains
in class AbstractCache
pKey
- the key to check for in our cachepublic boolean contains(java.lang.Object[] pKeys)
AbstractCache
contains
in class AbstractCache
pKeys
- the keys to check for in our cache.public void dump(java.io.PrintStream aStream)
public java.lang.String dump()
public java.lang.Object get(java.lang.Object pKey) throws java.lang.Exception
get
in class AbstractCache
pKey
- the key whose value should be returned.java.lang.Exception
- on an error (such an error occurring
when a CacheAdapter fetches a missing value).public java.lang.Object get(java.lang.Object pKey, boolean pCreate) throws java.lang.Exception
get
in class AbstractCache
pKey
- the key of the value to fetchpCreate
- whether to create a missing value.java.lang.Exception
- on an error (such an error occurring
when a CacheAdapter fetches a missing value).protected boolean hasExpiredValue(atg.service.cache.Cache.Entry pEntry)
protected boolean hasOtherwiseExpired(java.lang.Object pKey, java.lang.Object pValue)
hasOtherwiseExpired
in class AbstractCache
pKey
- the key of the possibly expired cache entrypValue
- pValue the value of possibly expired cache entrypublic java.lang.Object[] get(java.lang.Object[] pKeys) throws java.lang.Exception
get
in class AbstractCache
pKeys
- the keys whose values should be returned.java.lang.Exception
- on an error (such an error occurring
when a CacheAdapter fetches a missing value).protected boolean isExpiredValueStillGood(java.lang.Object key, java.lang.Object pValue)
isExpiredValueStillGood
in class AbstractCache
pKey
- the key the corresponds to the valuepValue
- the value to checkpublic void put(java.lang.Object pKey, java.lang.Object pValue)
put
in class AbstractCache
pKey
- the key to put in the cache.pValue
- the value to put in the cache associated with pKey.public boolean remove(java.lang.Object pKey)
remove
in class AbstractCache
pKey
- the key of the entry to remove.public boolean remove(java.lang.Object pKey, java.lang.Object pNewValue)
public void flush()
flush
in class AbstractCache
public int removeByPartialKey(int pPartialKeyIndex, java.lang.Object pPartialKeyValue)
pPartialKeyIndex
- index of the partial key to remove, should be >= 0 and < numKeyPartspPartialKeyValue
- value of the partial key to removepublic boolean getEnablePartialKeyInvalidation()
public void setEnablePartialKeyInvalidation(boolean pEnablePartialKeyInvalidation)
public int getNumKeyParts()
public void setNumKeyParts(int pNumKeyParts)
public int[] getKeyPartsForInvalidation()
public void setKeyPartsForInvalidation(int[] pKeyPartsForInvalidation)
public int getNumCacheEntries()
getNumCacheEntries
in class AbstractCache
public double getUsedCapacity()
getUsedCapacity
in class AbstractCache
public int getUsedMemory()
getUsedMemory
in class AbstractCache
public int getAccessCount()
getAccessCount
in class AbstractCache
public int getHitCount()
getHitCount
in class AbstractCache
public double getHitRatio()
getHitRatio
in class AbstractCache
public int getTimeoutCount()
getTimeoutCount
in class AbstractCache
public java.util.Iterator getAllElements()
getAllElements
in class AbstractCache
public java.util.Iterator getAllKeys()
getAllKeys
in class AbstractCache
public java.util.Iterator getValidKeysOrdered(boolean pLatestFirst)
getValidKeysOrdered
in class AbstractCache
pLatestFirst
- whether to return the latest first.public java.util.Iterator getAllKeysOrdered(boolean pLatestFirst)
public static void main(java.lang.String[] args) throws java.lang.InterruptedException
java.lang.InterruptedException