com.opensymphony.oscache.base.algorithm
public class: LRUCache [javadoc |
source]
java.lang.Object
com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache
com.opensymphony.oscache.base.algorithm.LRUCache
LRU (Least Recently Used) algorithm for the cache.
Since release 2.3 this class requires Java 1.4
to use the LinkedHashSet. Use prior OSCache release which
require the Jakarta commons-collections SequencedHashMap
class or the LinkedList class if neither of the above
classes are available.
No synchronization is required in this class since the
AbstractConcurrentReadCache already takes care of any
synchronization requirements.
- version:
$ - Revision: 427 $
- author:
< - a href="mailto:salaman@teknos.com">Victor Salaman
- author:
< - a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard
- author:
< - a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin
- author:
< - a href="mailto:chris@swebtec.com">Chris Miller
| Method from com.opensymphony.oscache.base.algorithm.LRUCache Detail: |
protected void itemPut(Object key) {
// Since this entry was just accessed, move it to the back of the list.
synchronized (list) { // A further fix for CACHE-44
list.remove(key);
list.add(key);
}
}
An object was put in the cache. This implementation adds/moves the
key to the end of the list. |
protected void itemRemoved(Object key) {
list.remove(key);
}
Remove specified key since that object has been removed from the cache. |
protected void itemRetrieved(Object key) {
// Prevent list operations during remove
while (removeInProgress) {
try {
Thread.sleep(5);
} catch (InterruptedException ie) {
}
}
// We need to synchronize here because AbstractConcurrentReadCache
// doesn't prevent multiple threads from calling this method simultaneously.
synchronized (list) {
list.remove(key);
list.add(key);
}
}
An item was retrieved from the list. The LRU implementation moves
the retrieved item's key to the front of the list. |
protected Object removeItem() {
Object toRemove = null;
removeInProgress = true;
try {
while (toRemove == null) {
try {
toRemove = removeFirst();
} catch (Exception e) {
// List is empty.
// this is theorically possible if we have more than the size concurrent
// thread in getItem. Remove completed but add not done yet.
// We simply wait for add to complete.
do {
try {
Thread.sleep(5);
} catch (InterruptedException ie) {
}
} while (list.isEmpty());
}
}
} finally {
removeInProgress = false;
}
return toRemove;
}
An item needs to be removed from the cache. The LRU implementation
removes the first element in the list (ie, the item that was least-recently
accessed). |