Package freemarker.cache
Class MruCacheStorage
- java.lang.Object
-
- freemarker.cache.MruCacheStorage
-
- All Implemented Interfaces:
CacheStorage
public class MruCacheStorage extends Object implements CacheStorage
A cache storage that implements a two-level Most Recently Used cache. In the first level, items are strongly referenced up to the specified maximum. When the maximum is exceeded, the least recently used item is moved into the second level cache, where they are softly referenced, up to another specified maximum. When the second level maximum is also exceeded, the least recently used item is discarded altogether. This cache storage is a generalization of bothStrongCacheStorageandSoftCacheStorage- the effect of both of them can be achieved by setting one maximum to zero and the other to the largest positive integer. On the other hand, if you wish to use this storage in a strong-only mode, or in a soft-only mode, you might consider usingStrongCacheStorageorSoftCacheStorageinstead, as they can be used byTemplateCacheconcurrently without any synchronization on a 5.0 or later JRE. This class is NOT thread-safe. If it is accessed from multiple threads concurrently, proper synchronization must be provided by the callers. Note thatTemplateCache, the natural user of this class provides the necessary synchronizations when it uses the class. Also you might consider whether you need this sort of a mixed storage at all in your solution, as in most cases SoftCacheStorage can also be sufficient. SoftCacheStorage will use Java soft references, and they already use access timestamps internally to bias the garbage collector against clearing recently used references, so you can get reasonably good (and memory-sensitive) most-recently-used caching throughSoftCacheStorageas well.
-
-
Constructor Summary
Constructors Constructor Description MruCacheStorage(int maxStrongSize, int maxSoftSize)Creates a new MRU cache storage with specified maximum cache sizes.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all values from this cacheObjectget(Object key)Retrieve a cached value associated with a keyvoidput(Object key, Object value)Associates a key with a cached valuevoidremove(Object key)Removes the value associated with a key, if it exists.
-
-
-
Constructor Detail
-
MruCacheStorage
public MruCacheStorage(int maxStrongSize, int maxSoftSize)Creates a new MRU cache storage with specified maximum cache sizes. Each cache size can vary between 0 andInteger.MAX_VALUE.- Parameters:
maxStrongSize- the maximum number of strongly referenced templatesmaxSoftSize- the maximum number of softly referenced templates
-
-
Method Detail
-
get
public Object get(Object key)
Description copied from interface:CacheStorageRetrieve a cached value associated with a key- Specified by:
getin interfaceCacheStorage- Parameters:
key- the key for retrieving an associated value- Returns:
- the cached value associated with the key, or null if no value is associated with the key.
-
put
public void put(Object key, Object value)
Description copied from interface:CacheStorageAssociates a key with a cached value- Specified by:
putin interfaceCacheStorage- Parameters:
key- the key to associate with a valuevalue- the value associated with the key.
-
remove
public void remove(Object key)
Description copied from interface:CacheStorageRemoves the value associated with a key, if it exists. If it doesn't exist, this method does nothing.- Specified by:
removein interfaceCacheStorage- Parameters:
key- the key whose associated value is removed from the cache.
-
clear
public void clear()
Description copied from interface:CacheStorageRemoves all values from this cache- Specified by:
clearin interfaceCacheStorage
-
-