public class Cache<K,V> extends Object implements CacheStatistics
This is the root class for all the cache in Jahia.
This cache can handle synchronization messages with other Jahia server instances by using JMS messages. This synchronization is automatically initialized and used when the JMS synchronization is activated in the Jahia configuration file.
Each cache must has a distinct name, and the
associated description is only for debugging purpose or for
monitoring display. Each cache uses a MRU (Most Recent Used) list to determine which
elements will be removed from the cache when the cache limit has been reached. In this
case, the least used cache entry will be removed.
Each object inserted in the cache will be wrapped into a
CacheEntry instance, which contains
among other information, the entry's expiration date and last accessed date.
Using the getCacheEntry
method will retrieve the cache entry instance and not the object stored into
the entry instance. To access the stored object instance, the
get method should be used instead or use
the getter methods of the CacheEntry
class.
Caches can only be retrieved and created through the
CacheFactory class, which is responsible
for managing all the caches.
CacheFactory,
CacheEntry| Modifier | Constructor and Description |
|---|---|
protected |
Cache(String name,
CacheImplementation<K,CacheEntry<V>> cacheImplementation)
Creates a new
Cache instance. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsKey(K entryKey)
Checks if the specified
entryKey is present in the cache. |
void |
flush()
Flushs all the cache entries.
|
void |
flush(boolean propagate)
Flushs all the cache entries.
|
void |
flushGroup(String groupName) |
V |
get(K entryKey)
Fetchs the cache entry associated to the
entryKey and returns the
object stored in fetched cache entry. |
double |
getCacheEfficiency()
Retrieves the percentage of cache hit that could successfully be served.
|
CacheEntry<V> |
getCacheEntry(K entryKey)
Retrieve the cache entry associated to the
entryKey argument or
null when the requested cache entry is not available, or when the
entryKey argument is null. |
CacheImplementation<K,CacheEntry<V>> |
getCacheImplementation() |
long |
getGroupsKeysTotal()
Get the total number of keys in all the groups of this cache.
|
long |
getGroupsSize()
Get the number of groups for this cache.
|
Collection<K> |
getKeys()
Returns a Collection of all cache keys at the moment (may change in the next moment)
Warning, if expiration times were set, they are NOT checked by this method, so make
sure you perform a get and check that it is not null when using this method.
|
String |
getName()
Retrieves the cache name.
|
long |
getSuccessHits()
Retrieves the number of cache hits that could successfully be served.
|
long |
getTotalHits()
Retrieves the total number of cache hits.
|
boolean |
isEmpty()
Return true if there are no entries in the cache.
|
void |
put(K entryKey,
V entryObj)
Add a new object into the cache.
|
void |
put(K entryKey,
V entryObj,
boolean propagate)
Add a new object into the cache.
|
void |
putCacheEntry(K entryKey,
CacheEntry<V> entry,
boolean propagate)
Add a new entry into the cache.
|
void |
remove(K entryKey)
Removes the cache entry associate to the key
entryKey. |
int |
size()
Return the current number of entries in the cache.
|
protected Cache(String name, CacheImplementation<K,CacheEntry<V>> cacheImplementation)
Creates a new Cache instance.
name - the cache namepublic CacheEntry<V> getCacheEntry(K entryKey)
Retrieve the cache entry associated to the entryKey argument or
null when the requested cache entry is not available, or when the
entryKey argument is null.
The last accessed date of the returned cache entry will be updated to the current
date (& time) and it's set as the most recent used entry in the cache's MRU list.
Expired cache entries will automatically be removed from the cache and
null will be returned as result.
entryKey - the cache entry key. null keys will
return null.public V get(K entryKey)
Fetchs the cache entry associated to the entryKey and returns the
object stored in fetched cache entry. null is returned when the
entryKey is null, or when no cache entry could be found
for the specified entryKey.
entryKey - the key associated to the requested object. A null
key will return in a null result.entryKey cache entry.public void put(K entryKey, V entryObj)
Add a new object into the cache. This method encapsulates automatically the specified
entryObj object into a new
CacheEntry instance and associated the
new cache entry to the entryKey key. If there is already an entry
associated with the entryKey key in the cache, that entry will be removed
and replaced with the new specified entry.
entryKey - the object's associated key, null is not allowedentryObj - the reference to the object to be cached.public void put(K entryKey, V entryObj, boolean propagate)
Add a new object into the cache. This method encapsulates automatically the specified
entryObj object into a new
CacheEntry instance and associated the
new cache entry to the entryKey key. If there is already an entry
associated with the entryKey key in the cache, that entry will be removed
and replaced with the new specified entry.
entryKey - the object's associated key, null is not allowedentryObj - the reference to the object to be cached.propagate - specifies whether the cache update should be sent
across the cluster. If you don't know what this means, set it to true.
Set it to false it you want to update the cache ONLY locally, for example
when implementing a cache listener method.public void putCacheEntry(K entryKey, CacheEntry<V> entry, boolean propagate)
Add a new entry into the cache.
The process will be ignored when either entryKey and/or
entry is/are null. If there is already an entry
associated with the entryKey key in the cache, that entry will be removed
and replaced with the new specified entry.
entryKey - the cache entry key, null is not allowed.entry - the reference to the entry to be cached, null is not
allowed.propagate - specifies whether the cache update should be sent
across the cluster. If you don't know what this means, set it to true.
Set it to false it you want to update the cache ONLY locally, for example
when implementing a cache listener method.public void remove(K entryKey)
Removes the cache entry associate to the key entryKey. The removal
operation is canceled in case the entryKey is null; does
nothing when the entryKey is unknown in the cache.
entryKey - the cache entry key, null is not allowedpublic final boolean isEmpty()
Return true if there are no entries in the cache.
public final int size()
Return the current number of entries in the cache.
public final long getGroupsSize()
public final long getGroupsKeysTotal()
public final String getName()
Retrieves the cache name.
public final void flush()
Flushs all the cache entries. By sending the flushing event to all the cache's listeners.
public void flush(boolean propagate)
Flushs all the cache entries. When sendToListeners is
true, the event is send to the cache's listeners.
public final void flushGroup(String groupName)
public final long getSuccessHits()
CacheStatisticsgetSuccessHits in interface CacheStatisticspublic final long getTotalHits()
CacheStatisticsgetTotalHits in interface CacheStatisticspublic double getCacheEfficiency()
CacheStatisticsgetCacheEfficiency in interface CacheStatisticspublic final boolean containsKey(K entryKey)
entryKey is present in the cache.entryKey - the entry key to be checked.true when the entry key is present in the cache,
otherwise return falsepublic final Collection<K> getKeys()
public CacheImplementation<K,CacheEntry<V>> getCacheImplementation()
Copyright © 2004–2020 Jahia Solutions Group SA. All rights reserved.