【Java开源代码栏目提醒】:本文主要为网学会员提供TransactionHistoryCache.java,希望对需要TransactionHistoryCache.java网友有所帮助,学习一下!
import java.util.Hashtable;
import java.util.ArrayList;
/**
* Instances of this class are responsible for maintaining a
* cache of objects for a CacheManager object.
*/
class TransactionHistoryCache {
private static final int TIME_TO_LIVE
= 15*60*1000; // 15 Minutes
/**
* We use a linked list to determine the least recently
* used TransactionHistory. The cache that itself is
* implemented by a Hashtable object. The Hashtable
* values are linked list objects that refer to the actual
* TransactionHistory object.
*/
private Hashtable cache = new Hashtable();
/**
* This is the head of the linked list that refers to the
* most recently used TransactionHistory.
*/
private LinkedList mru = null;
/**
* This is the end of the linked list that refers to the
* least recently used TransactionHistory.
*/
private LinkedList lru = null;
/**
* The maximum number of TransactionHistory objects that may
* be in the cache.
*/
private final int MAX_CACHE_SIZE = 80;
/**
* The number of TransactionHistory objects currently in the
* cache.
*/
private int currentCacheSize = 0;
/**
* The object that schedules and performs the removal of
* transaction histories from the cache.
*/
private HistoryDeletionScheduler deletionScheduler
= new HistoryDeletionScheduler();
/**
* Objects are passed to this method for addition to the
* cache. However, this method is not required to actually
* add an object to the cache if that is contrary to its
* policy for what object should be added. This method may
* also remove objects already in the cache in order to
* make room for new objects.
* @param history The TransactionHistory that is being
* proposed as an addition to the cache.
*/
public void addHistory(TransactionHistory history) {
ItemID id = history.getID();
if (cache.get(id) == null) { // if history not in cache
// Add history to cache, making it the most
// recently used.
if (currentCacheSize == 0) {
// treate empty cache as a special case
lru = mru = new LinkedList();
mru.profile = history;
} else { // currentCacheSize > 0
LinkedList newLink;
if (currentCacheSize >= MAX_CACHE_SIZE) {
// remove least recently used
// TransactionHistory from the cache.
newLink = lru;
lru = newLink.previous;
cache.remove(id);
currentCacheSize--;
lru.next = null;
} else {
newLink = new LinkedList();
} // if >= MAX_CACHE_SIZE
newLink.profile = history;
newLink.next = mru;
mru.previous = newLink;
newLink.previous = null;
mru = newLink;
} // if 0
// put the now most recently used history in the
// cache.
mru.expirationTime
= System.currentTimeMillis()+TIME_TO_LIVE;
cache.put(id, mru);
currentCacheSize++;
deletionScheduler.scheduleRemoval(mru);
} else { // history already in cache
// addEmployee shouldn't be called when the object
// is already in the cache. Since that has
// happened, do a fetch so that the object becomes
// the most recently used.
fetchHistory(id);
} // if cache.get(id)
} // addHistory(TransactionHistory)
private void remove(LinkedList node) {
if (mru==node) {
mru = node.next;
} // if mru
if (lru==node) {
lru = node.previous;
} // if
if (node.next!=null) {
node.next.previous = node.previous;
} // if node.next
if (node.previous!=null) {
node.previous.next = node.next;
} // if node.previous
cache.remove(node.profile.getID());
} // remove(LinkedList)
/**
* Return the TransactionHistory associated with the given
* ItemID in the cache or null if no TransactionHistory is
* associated with the given ItemID.
* @param id the ItemID to retrieve a transaction history
* for.
*/
public TransactionHistory fetchHistory(ItemID id) {
LinkedList foundLink = (LinkedList)cache.get(id);
if (foundLink == null)
return null;
if (mru != foundLink) {
if ( foundLink == lru ) {
lru = foundLink.previous;
lru.next = null;
} // if lru
if (foundLink.previous != null)
foundLink.previous.next = foundLink.next;
if (foundLink.next != null)
foundLink.next.previous = foundLink.previous;
mru.previous = foundLink;
foundLink.previous = null;
foundLink.next = mru;
mru = foundLink;
} // if currentCacheSize > 1
return foundLink.profile;
} // fetchHistory(ItemID)
/**
* private doublely linked list class for managing list of
* most recently used transaction histories.
*/
private class LinkedList {
TransactionHistory profile;
LinkedList previous;
LinkedList next;
long expirationTime;
} // class LinkedList
/**
* Private class responsible for deleting objects from the
* cache when their time comes.
*/
private class HistoryDeletionScheduler