freemarker.cache
Class MruCacheStorage
java.lang.Object
freemarker.cache.MruCacheStorage
- All Implemented Interfaces:
- CacheStorage
public class MruCacheStorage
- extends java.lang.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 both StrongCacheStorage
and
SoftCacheStorage
- 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 using StrongCacheStorage
or
SoftCacheStorage
instead, as they can be used by
TemplateCache
concurrently 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 that TemplateCache
, 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 through
SoftCacheStorage
as well.
- Version:
- $Id: MruCacheStorage.java,v 1.7.2.1 2007/04/02 13:07:32 szegedia Exp $
- Author:
- Attila Szegedi
Constructor Summary |
MruCacheStorage(int maxStrongSize,
int maxSoftSize)
Creates a new MRU cache storage with specified maximum cache sizes. |
Method Summary |
void |
clear()
|
java.lang.Object |
get(java.lang.Object key)
|
void |
put(java.lang.Object key,
java.lang.Object value)
|
void |
remove(java.lang.Object key)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
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 and
Integer.MAX_VALUE
.
- Parameters:
maxStrongSize
- the maximum number of strongly referenced templatesmaxSoftSize
- the maximum number of softly referenced templates
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
get
in interface CacheStorage
put
public void put(java.lang.Object key,
java.lang.Object value)
- Specified by:
put
in interface CacheStorage
remove
public void remove(java.lang.Object key)
- Specified by:
remove
in interface CacheStorage
clear
public void clear()
- Specified by:
clear
in interface CacheStorage