利用LinkedHashMap简单实现基于LRU策略的缓存

为了提高访问效率,对资源的访问一般会考虑采取一定的缓存策略。基于访问时间的缓存策略:LRU (LeastRecentlyUsed)是一种应用广泛的缓存算法。该算法维护一个缓存项队列队列中的缓存项按每项的最后被访问时间排序。当缓存空间已满时将处于队尾即删除最后一次被访问时间距现在最久的项将新的区段放入队列首部。


缓存一般也是会被多线程访问,需要考虑线程访问安全问题,对于为考虑多线程访问而实现的LinkedHashMap可以参考http://code.google.com/p/concurrentlinkedhashmap/


Java程序中利用LinkedHashMap可以非常方便的实现基于LRU策略的缓存

public class QuestionBankCache{
private LinkedHashMap<String, QuestionBank> qbanksMap;
private static final int MAX_ENTRIES = 100;
public QuestionBankCache() {
qbanksMap = new LinkedHashMap(MAX_ENTRIES, .75F,true) {
   protected boolean removeEldestEntry(Map.Entry eldest) {    
   return size() > MAX_ENTRIES;    
   
};

}

LinkedHashMap相比HashMap类,HashMap中元素是无序的,而LinkedHashMap中是按照一定顺序排列元素的,其中有个重要的成员变量accessOrder,该属性默认为false,LinkedHashMap仅根据插入顺序对元素排序;如果在其构造函数中指定该属性为true,则LinkedHashMap即可根据访问顺序对元素排序。

    /**
     * The iteration ordering method for this linked hash map: <tt>true</tt>
     * for access-order, <tt>false</tt> for insertion-order.
     *
     * @serial
     */
    private final boolean accessOrder;

1,访问元素的实现

LinkedHashMap的get函数,可以看到实现方法是先根据key值找出元素,并调用了该元素的recordAccess方法

    public V get(Object key) {
        Entry<K,V> e = (Entry<K,V>)getEntry(key);
        if (e == null)
            return null;
        e.recordAccess(this);
        return e.value;
    }


recordAccess方法的实现是在LinkedHashMap Entry类中,该方法在accessOrder属性为true时 将该元素移到容器的header处,以实现按照访问顺序进行排序的目的

    /**
     * LinkedHashMap entry.
     */
    private static class Entry<K,V> extends HashMap.Entry<K,V> {
        // These fields comprise the doubly linked list used for iteration.
        Entry<K,V> before, after;

        Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
            super(hash, key, value, next);
        }

        /**
         * Removes this entry from the linked list.
         */
        private void remove() {
            before.after = after;
            after.before = before;
        }

        /**
         * Inserts this entry before the specified existing entry in the list.
         */
        private void addBefore(Entry<K,V> existingEntry) {
            after  = existingEntry;
            before = existingEntry.before;
            before.after = this;
            after.before = this;
        }

        /**
         * This method is invoked by the superclass whenever the value
         * of a pre-existing entry is read by Map.get or modified by Map.set.
         * If the enclosing Map is access-ordered, it moves the entry
         * to the end of the list; otherwise, it does nothing.
         */
        void recordAccess(HashMap<K,V> m) {
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
            if (lm.accessOrder) {
                lm.modCount++;
                remove();
                addBefore(lm.header);
            }
        }

        void recordRemoval(HashMap<K,V> m) {
            remove();
        }
    }


2,插入元素的实现

插入元素是通过父类的put方法实现,可以看到该方法中会调用recordAccess方法(覆盖方式)和addEntry方法(新增方式),这两个方法在LinkedHashMap类中都进行了重构。

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }
LinkedHashMap中重构(Override)addEntry方法的实现,可以看到调用了removeEldesEntry方法,该方法我们在缓存类中进行了重构(Override),目的是保持缓存中的最大元素个数固定,而LinkedHashMap默认实现是通过动态扩大容量来保证元素空间的

    /**
     * This override alters behavior of superclass put method. It causes newly
     * allocated entry to get inserted at the end of the linked list and
     * removes the eldest entry if appropriate.
     */
    void addEntry(int hash, K key, V value, int bucketIndex) {
        createEntry(hash, key, value, bucketIndex);

        // Remove eldest entry if instructed, else grow capacity if appropriate
        Entry<K,V> eldest = header.after;
        if (removeEldestEntry(eldest)) {
            removeEntryForKey(eldest.key);
        } else {
            if (size >= threshold)
                resize(2 * table.length);
        }
    }

    /**
     * This override differs from addEntry in that it doesn't resize the
     * table or remove the eldest entry.
     */
    void createEntry(int hash, K key, V value, int bucketIndex) {
        HashMap.Entry<K,V> old = table[bucketIndex];
        Entry<K,V> e = new Entry<K,V>(hash, key, value, old);
        table[bucketIndex] = e;
        e.addBefore(header);
        size++;
    }





   

你可能感兴趣的:(利用LinkedHashMap简单实现基于LRU策略的缓存)