LRUCache缓存

 

LRU算法 (Last Recently Used)

LRU缓存把最近最少使用的数据移除,让给最新读取的数据;

利用LRU缓存,能够提高系统的performance;

 

 

public final class LRUCache implements MemoizeCache<Object, Object> {

    private final Map<Object, Object> cache;

    public LRUCache(final int maxCacheSize) {
        cache = Collections.synchronizedMap(new LRUProtectionStorage(maxCacheSize));
    }

    public Object put(final Object key, final Object value) {
        return cache.put(key, value);
    }

    public Object get(final Object key) {
        return cache.get(key);
    }

    /**
     * Replying on the Collections.SynchronizedMap thread-safe iteration implementation the method will remove all entries holding
     * SoftReferences to gc-evicted objects.
     */
    public void cleanUpNullReferences() {
        synchronized (cache) {
            final Iterator<Map.Entry<Object, Object>> iterator = cache.entrySet().iterator();
            while (iterator.hasNext()) {
                final Map.Entry<Object, Object> entry = iterator.next();
                if (((SoftReference) entry.getValue()).get() == null) iterator.remove();
            }
        }
    }
    

}

 

/**
 * Represents a memoize cache with its essential methods
 * @param <K> type of the keys
 * @param <V> type of the values
 *
 * @author Vaclav Pech
 */
public interface MemoizeCache<K, V> {

    V put(K key, V value);

    V get(K key);

    /**
     * Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.
     * The implementation must ensure that concurrent invocations of all methods on the cache may occur from other threads
     * and thus should protect any shared resources.
     */
    void cleanUpNullReferences();
}

 

import java.util.LinkedHashMap;
import java.util.Map;

public final class LRUProtectionStorage extends LinkedHashMap<Object, Object>
		{
	
	private static final long serialVersionUID = 1L;

	private final int maxSize;

	public LRUProtectionStorage(final int maxSize) {
		this.maxSize = maxSize;
	}

	/**
	 * The eldest entry should be removed when we reached the maximum cache size
	 */
	@Override
	protected boolean removeEldestEntry(final Map.Entry<Object, Object> eldest) {
		return size() > maxSize;
	}

	/**
	 * The touch method can be used to renew an element and move it to the from
	 * of the LRU queue.
	 * 
	 * @param key
	 *            The key of the element to renew
	 * @param value
	 *            A value to newly associate with the key
	 */
	public synchronized void touch(final Object key, final Object value) {
		remove(key);
		put(key, value);
	}

	/**
	 * Makes sure the retrieved object is moved to the head of the LRU list
	 */
	@Override
	public synchronized Object get(final Object key) {
		final Object value = remove(key);
		if (value != null)
			put(key, value);
		return value;
	}

	/**
	 * Performs a shallow clone
	 * 
	 * @return The cloned instance
	 */
	@Override
	public Object clone() {
		return super.clone();
	}
}

 

你可能感兴趣的:(cache)