LRU Cache : Cache « Development Class « Java






LRU Cache

    
/*
 * XAPool: Open Source XA JDBC Pool
 * Copyright (C) 2003 Objectweb.org
 * Initial Developer: Lutris Technologies Inc.
 * Contact: xapool-public@lists.debian-sf.objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Enumeration;

/**
 * Simple implementation of a cache, using Least Recently Used algorithm
 * for discarding members when the cache fills up
 */
public class LRUCache {
    /** The cache */
    private Hashtable cache = new Hashtable();
    /** The linked list to keep track of least/most recently used */
    private LinkedList lru = new LinkedList();
    /** The maximum size of the cache */
    private int maxSize;


    /**
     * Constructor
     */
    public LRUCache(int maxSize) {
        this.maxSize = maxSize;
    }

    public int LRUSize() {
        return lru.size();

    }
    public int cacheSize() {
        return cache.size();

    }

    /**
     * Puts a new object in the cache.  If the cache is full, it removes
     * the least recently used object.  The new object becomes the most
     * recently used object.
     */
    public void put(Object key, Object value) {
        List removed = new ArrayList();
        synchronized (this) {
            // make room if needed
            while (cache.size() + 1 > maxSize) {
                removed.add(removeLRU());
            }
            // remove the key from the list if it's in the cache already
      Object cacheValue = cache.get(key);
      if (cacheValue != null) {
    if (cacheValue != value)
        removed.add(cacheValue);
    
                lru.remove(key);
            }
            // the last item in the list is the most recently used
            lru.addLast(key);
            // put it in the actual cache
            cache.put(key, value);
        }
        cleanupAll(removed);
    }

    /**
     * Gets an object from the cache.  This object is set to be the
     * most recenty used
     */
    public synchronized Object get(Object key) {
        // check for existence in cache
        if (!cache.containsKey(key)) {
            return null;
        }
        // set to most recently used
        lru.remove(key);
        lru.addLast(key);
        // return the object
        return cache.get(key);
    }

    /**
     * Removes the object from the cache and the lru list
     */
    public synchronized Object remove(Object key) {
        // check for existence in cache
        if (!cache.containsKey(key)) {
            return null;
        }
        // remove from lru list
        lru.remove(key);
        // remove from cache
        Object obj = cache.remove(key);
        return obj;
    }

    private synchronized Object removeLRU() {
        Object obj = cache.remove(lru.getFirst());
        lru.removeFirst();
        return obj;
    }

    /**
     * Resize the cache
     */
    public void resize(int newSize) {
        if (newSize <= 0) {
            return;
        }
        List removed = new ArrayList();
        synchronized (this) {
            maxSize = newSize;
            while (cache.size() > maxSize) {
                removed.add(removeLRU());
            }
        }
        cleanupAll(removed);
    }

    private void cleanupAll(List removed) {
        Iterator it = removed.iterator();
        while (it.hasNext()) {
            cleanupObject(it.next());
        }
    }
    
    /**
     * Override this method to do special cleanup on an object,
     * such as closing a statement or a connection
     */
    protected void cleanupObject(Object o) {}

    public void cleanupAll() {
        for (Enumeration enumeration = cache.keys();enumeration.hasMoreElements();) {

            Object o = remove(enumeration.nextElement());
            cleanupObject(o);

        }
    }

  
}

   
    
    
    
  








Related examples in the same category

1.A LRU (Least Recently Used) cache replacement policy
2.A Map that is size-limited using an LRU algorithm
3.A random cache replacement policy
4.A second chance FIFO (First In First Out) cache replacement policy
5.An LRU (Least Recently Used) cache replacement policy
6.Async LRU List
7.FIFO First In First Out cache replacement policy
8.Implementation of a Least Recently Used cache policy
9.Generic LRU Cache
10.A Least Recently Used Cache
11.The class that implements a simple LRU cache
12.Map implementation for cache usage
13.Weak Cache Map
14.Provider for the application cache directories.
15.Fixed length cache with a LRU replacement policy.
16.A small LRU object cache.
17.A least recently used (LRU) cache.
18.LRU Cache 2
19.A cache that purges values according to their frequency and recency of use and other qualitative values.
20.A thread-safe cache that keeps its values as java.lang.ref.SoftReference so that the cache is, in effect, managed by the JVM and kept as small as is required
21.Cache LRU
22.A FastCache is a map implemented with soft references, optimistic copy-on-write updates, and approximate count-based pruning.
23.A HardFastCache is a map implemented with hard references, optimistic copy-on-write updates, and approximate count-based pruning.