Class DefaultCache<K extends Serializable,V extends Serializable>
- The size of the object (in bytes).
- A pointer to the node in the linked list that maintains accessed order for the object. Keeping a reference to the node lets us avoid linear scans of the linked list.
- A pointer to the node in the linked list that maintains the age of the object in cache. Keeping a reference to the node lets us avoid linear scans of the linked list.
To get an object from cache, a hash lookup is performed to get a reference to the CacheObject that wraps the real object we are looking for. The object is subsequently moved to the front of the accessed linked list and any necessary cache cleanups are performed. Cache deletion and expiration is performed as needed.
- Author:
- Matt Tucker
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jivesoftware.util.cache.Cache
Cache.CapacityUnit
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final boolean
The map the keys and values are stored in.protected static final long
protected static final String
protected static final String
Fields inherited from interface org.jivesoftware.util.cache.Cache
secretKey, secretValue
-
Method Summary
Modifier and TypeMethodDescriptionaddClusteredCacheEntryListener
(ClusteredCacheEntryListener<K, V> listener, boolean includeValues, boolean includeEventsFromLocalNode) Registers a listener to receive entry events for this cache.void
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.long
getCacheCulls
(Duration duration) long
Returns the number of cache hits.long
Returns the number of cache misses.Defines the unit used to calculate the capacity of the cache, which for all instances of this class is byte-size based.Returns an existingLock
on the specified key or creates a new one if none was found.long
Returns the size of the cache contents in bytes.long
Returns the maximum size of the cache (in bytes).long
Returns the maximum number of milliseconds that any object can live in cache.getName()
Returns the name of this cache.boolean
isEmpty()
keySet()
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.void
void
removeClusteredCacheEntryListener
(String listenerId) Removes a previously registered event listener.void
setMaxCacheSize
(long maxSize) Sets the maximum size of the cache in bytes.void
setMaxLifetime
(long maxLifetime) Sets the maximum number of milliseconds that any object can live in cache.void
Sets the name of this cache.int
size()
values()
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.jivesoftware.util.cache.Cache
getCacheSizeRemark, getMaxCacheSizeRemark, isKeySecret, isValueSecret, setSecretKey, setSecretValue
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
NULL_KEY_IS_NOT_ALLOWED
- See Also:
-
NULL_VALUE_IS_NOT_ALLOWED
- See Also:
-
allowNull
protected static final boolean allowNull -
MAX_CULL_COUNT_PERIOD
protected static final long MAX_CULL_COUNT_PERIOD -
map
protected Map<K extends Serializable,org.jivesoftware.util.cache.DefaultCache.CacheObject<V extends Serializable>> mapThe map the keys and values are stored in.
-
-
Method Details
-
put
- Specified by:
put
in interfaceMap<K extends Serializable,
V extends Serializable>
-
get
- Specified by:
get
in interfaceMap<K extends Serializable,
V extends Serializable>
-
remove
- Specified by:
remove
in interfaceMap<K extends Serializable,
V extends Serializable>
-
clear
public void clear()- Specified by:
clear
in interfaceMap<K extends Serializable,
V extends Serializable>
-
size
public int size()- Specified by:
size
in interfaceMap<K extends Serializable,
V extends Serializable>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceMap<K extends Serializable,
V extends Serializable>
-
values
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.values()
implementation, the collection returned from this method cannot be modified.- Specified by:
values
in interfaceCache<K extends Serializable,
V extends Serializable> - Specified by:
values
in interfaceMap<K extends Serializable,
V extends Serializable> - Returns:
- an unmodifiable collection view of the values contained in this map
-
containsKey
- Specified by:
containsKey
in interfaceMap<K extends Serializable,
V extends Serializable>
-
putAll
- Specified by:
putAll
in interfaceMap<K extends Serializable,
V extends Serializable>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K extends Serializable,
V extends Serializable>
-
entrySet
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.entrySet()
implementation, the set returned from this method cannot be modified.- Specified by:
entrySet
in interfaceCache<K extends Serializable,
V extends Serializable> - Specified by:
entrySet
in interfaceMap<K extends Serializable,
V extends Serializable> - Returns:
- an unmodifiable set view of the mappings contained in this map
-
keySet
Description copied from interface:Cache
IMPORTANT: Unlike the standardMap.keySet()
implementation, the set returned from this method cannot be modified.- Specified by:
keySet
in interfaceCache<K extends Serializable,
V extends Serializable> - Specified by:
keySet
in interfaceMap<K extends Serializable,
V extends Serializable> - Returns:
- an unmodifiable set view of the keys contained in this map
-
getLock
Description copied from interface:Cache
Returns an existingLock
on the specified key or creates a new one if none was found. This operation is thread safe. Successive calls with the same key may or may not return the sameLock
. However, different threads asking for the same Lock at the same time will get the same Lock object.The supplied cache may or may not be used depending whether the server is running on cluster mode or not. When not running as part of a cluster then the lock will be unrelated to the cache and will only be visible in this JVM.
- Specified by:
getLock
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
key
- the object that defines the visibility or scope of the lock.- Returns:
- an existing lock on the specified key or creates a new one if none was found.
-
getCapacityUnit
Defines the unit used to calculate the capacity of the cache, which for all instances of this class is byte-size based.- Specified by:
getCapacityUnit
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the unit to be used to calculate the capacity of this cache, which will be
Cache.CapacityUnit.BYTES
.
-
getName
Returns the name of this cache. The name is completely arbitrary and used only for display to administrators.- Specified by:
getName
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the name of this cache.
-
setName
Sets the name of this cache.- Specified by:
setName
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
name
- the name of this cache.
-
getCacheHits
public long getCacheHits()Returns the number of cache hits. A cache hit occurs every time the get method is called and the cache contains the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheHits
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the number of cache hits.
-
getCacheMisses
public long getCacheMisses()Returns the number of cache misses. A cache miss occurs every time the get method is called and the cache does not contain the requested object.Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.
- Specified by:
getCacheMisses
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the number of cache hits.
-
getLongCacheSize
public long getLongCacheSize()Returns the size of the cache contents in bytes. This value is only a rough approximation, so cache users should expect that actual VM memory used by the cache could be significantly higher than the value reported by this method.- Specified by:
getLongCacheSize
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the size of the cache contents in bytes.
-
getMaxCacheSize
public long getMaxCacheSize()Returns the maximum size of the cache (in bytes). If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.- Specified by:
getMaxCacheSize
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the maximum size of the cache (-1 indicates unlimited max size).
-
setMaxCacheSize
public void setMaxCacheSize(long maxSize) Sets the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created
- Specified by:
setMaxCacheSize
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
maxSize
- the maximum size of the cache in bytes.
-
getMaxLifetime
public long getMaxLifetime()Returns the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.- Specified by:
getMaxLifetime
in interfaceCache<K extends Serializable,
V extends Serializable> - Returns:
- the maximum number of milliseconds before objects are expired.
-
setMaxLifetime
public void setMaxLifetime(long maxLifetime) Sets the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.- Specified by:
setMaxLifetime
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
maxLifetime
- the maximum number of milliseconds before objects are expired.
-
getCacheCulls
-
addClusteredCacheEntryListener
public String addClusteredCacheEntryListener(@Nonnull ClusteredCacheEntryListener<K, V> listener, boolean includeValues, boolean includeEventsFromLocalNode) Description copied from interface:Cache
Registers a listener to receive entry events for this cache. To optimize the amount of data that is being processed, this method allows the listener to be registered in a way that suppresses values from being sent to it. In that case, only keys will be included in the event listener invocation.- Specified by:
addClusteredCacheEntryListener
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
listener
- the listener to be registered.includeValues
- defines if the listener should receive the values associated with the events.includeEventsFromLocalNode
- defines if the listener should be invoked for events that originate on the local node.- Returns:
- a unique identifier for the listener which is used as a key to remove the listener
-
removeClusteredCacheEntryListener
Description copied from interface:Cache
Removes a previously registered event listener.- Specified by:
removeClusteredCacheEntryListener
in interfaceCache<K extends Serializable,
V extends Serializable> - Parameters:
listenerId
- the identifier of the listener to be removed.
-