Package org.jboss.util
Class LRUCachePolicy
- java.lang.Object
-
- org.jboss.util.LRUCachePolicy
-
- All Implemented Interfaces:
CachePolicy
public class LRUCachePolicy extends java.lang.Object implements CachePolicy
Implementation of a Least Recently Used cache policy.- Version:
- $Revision$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LRUCachePolicy.LRUCacheEntry
Double linked cell used as entry in the cache list.class
LRUCachePolicy.LRUList
Double queued list used to store cache entries.
-
Field Summary
Fields Modifier and Type Field Description protected LRUCachePolicy.LRUList
m_list
The linked list used to implement the LRU algorithmprotected java.util.Map
m_map
The map holding the cached objectsprotected int
m_maxCapacity
The maximum capacity of this cacheprotected int
m_minCapacity
The minimum capacity of this cache
-
Constructor Summary
Constructors Constructor Description LRUCachePolicy()
Creates a LRU cache policy object with zero cache capacity.LRUCachePolicy(int min, int max)
Creates a LRU cache policy object with the specified minimum and maximum capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
ageOut(LRUCachePolicy.LRUCacheEntry entry)
Callback method called when the cache algorithm ages out of the cache the given entry.protected void
cacheMiss()
Callback method called when a cache miss happens.void
create()
Initializes the cache, creating all required objects and initializing their values.protected LRUCachePolicy.LRUCacheEntry
createCacheEntry(java.lang.Object key, java.lang.Object value)
Factory method for cache entriesprotected LRUCachePolicy.LRUList
createList()
Factory method for the linked list used by this cache implementation.protected java.util.Map
createMap()
Create map holding entries.void
destroy()
Destroys the cache that is now unusable.void
flush()
Flushes the cached objects from the cache.java.lang.Object
get(java.lang.Object key)
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.void
insert(java.lang.Object key, java.lang.Object o)
Inserts the specified object into the cache following the implemented policy.java.lang.Object
peek(java.lang.Object key)
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.void
remove(java.lang.Object key)
Remove the cached object paired with the specified key.int
size()
void
start()
Starts this cache that is now ready to be used.void
stop()
Stops this cache thusflush()
ing all cached objects.
-
-
-
Field Detail
-
m_map
protected java.util.Map m_map
The map holding the cached objects
-
m_list
protected LRUCachePolicy.LRUList m_list
The linked list used to implement the LRU algorithm
-
m_maxCapacity
protected int m_maxCapacity
The maximum capacity of this cache
-
m_minCapacity
protected int m_minCapacity
The minimum capacity of this cache
-
-
Constructor Detail
-
LRUCachePolicy
public LRUCachePolicy()
Creates a LRU cache policy object with zero cache capacity.- See Also:
create()
-
LRUCachePolicy
public LRUCachePolicy(int min, int max)
Creates a LRU cache policy object with the specified minimum and maximum capacity.- Parameters:
min
-max
-- See Also:
create()
-
-
Method Detail
-
createMap
protected java.util.Map createMap()
Create map holding entries.- Returns:
- the map
-
create
public void create()
Initializes the cache, creating all required objects and initializing their values.- Specified by:
create
in interfaceCachePolicy
- See Also:
start()
,destroy()
-
start
public void start()
Starts this cache that is now ready to be used.- Specified by:
start
in interfaceCachePolicy
- See Also:
create()
,stop()
-
stop
public void stop()
Stops this cache thusflush()
ing all cached objects.
After this method is called, a call tostart()
will restart the cache.- Specified by:
stop
in interfaceCachePolicy
- See Also:
start()
,destroy()
-
destroy
public void destroy()
Destroys the cache that is now unusable.
To have it working again it must be re-create()
ed and re-start()
ed.- Specified by:
destroy
in interfaceCachePolicy
- See Also:
create()
-
get
public java.lang.Object get(java.lang.Object key)
Description copied from interface:CachePolicy
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
Implementations of this method must have complexity of order O(1). Differently fromCachePolicy.peek(java.lang.Object)
this method not only return whether the object is present in the cache or not, but also applies the implemented policy that will "refresh" the cached object in the cache, because this cached object was really requested.- Specified by:
get
in interfaceCachePolicy
- Parameters:
key
- the key paired with the object- Returns:
- the object
- See Also:
CachePolicy.peek(java.lang.Object)
-
peek
public java.lang.Object peek(java.lang.Object key)
Description copied from interface:CachePolicy
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
Implementations of this method must have complexity of order O(1). This method should not apply the implemented caching policy to the object paired with the given key, so that a client can query if an object is cached without "refresh" its cache status. Real requests for the object must be done usingCachePolicy.get(java.lang.Object)
.- Specified by:
peek
in interfaceCachePolicy
- Parameters:
key
- the key paired with the object- Returns:
- the object
- See Also:
CachePolicy.get(java.lang.Object)
-
insert
public void insert(java.lang.Object key, java.lang.Object o)
Description copied from interface:CachePolicy
Inserts the specified object into the cache following the implemented policy.
Implementations of this method must have complexity of order O(1).- Specified by:
insert
in interfaceCachePolicy
- Parameters:
key
- the key paired with the objecto
- the object to cache- See Also:
CachePolicy.remove(java.lang.Object)
-
remove
public void remove(java.lang.Object key)
Description copied from interface:CachePolicy
Remove the cached object paired with the specified key.
Implementations of this method must have complexity of order O(1).- Specified by:
remove
in interfaceCachePolicy
- Parameters:
key
- the key paired with the object- See Also:
CachePolicy.insert(java.lang.Object, java.lang.Object)
-
flush
public void flush()
Description copied from interface:CachePolicy
Flushes the cached objects from the cache.- Specified by:
flush
in interfaceCachePolicy
-
size
public int size()
- Specified by:
size
in interfaceCachePolicy
- Returns:
- the size of the cache
-
createList
protected LRUCachePolicy.LRUList createList()
Factory method for the linked list used by this cache implementation.- Returns:
- the lru list
-
ageOut
protected void ageOut(LRUCachePolicy.LRUCacheEntry entry)
Callback method called when the cache algorithm ages out of the cache the given entry.
The implementation here is removing the given entry from the cache.- Parameters:
entry
-
-
cacheMiss
protected void cacheMiss()
Callback method called when a cache miss happens.
-
createCacheEntry
protected LRUCachePolicy.LRUCacheEntry createCacheEntry(java.lang.Object key, java.lang.Object value)
Factory method for cache entries- Parameters:
key
-value
-- Returns:
- the entry
-
-