Android 利用 LruCache 实现 LruCacheSet

import android.util.LruCache;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class LruCacheSet<E> {
    
    
    // Use map value (Integer) to save the size of key (E).
    private final LruCache<E, Integer> mLruCache;

    public LruCacheSet(int maxSize) {
    
    
        if (maxSize <= 0) {
    
    
            throw new IllegalArgumentException("maxSize <= 0");
        }
        mLruCache = new LruCache<E, Integer>(maxSize) {
    
    
            @Override
            protected void entryRemoved(boolean evicted, E key, Integer oldVal, Integer newVal) {
    
    
                if (newVal == null) {
    
    
                    LruCacheSet.this.elementRemoved(evicted, key);
                }
            }

            @Override
            protected Integer create(E key) {
    
    
                return null;
            }

            @Override
            protected int sizeOf(E key, Integer value) {
    
    
                if (value != null) {
    
    
                    return value;
                }
                return LruCacheSet.this.sizeOf(key);
            }
        };
    }

    public final void resize(int maxSize) {
    
    
        if (maxSize <= 0) {
    
    
            throw new IllegalArgumentException("maxSize <= 0");
        }
        mLruCache.resize(maxSize);
    }

    public final boolean contains(E e) {
    
    
        if (e == null) {
    
    
            throw new NullPointerException("e == null");
        }
        Integer value = mLruCache.get(e);
        return value != null;
    }

    public final boolean add(E e) {
    
    
        if (e == null) {
    
    
            throw new NullPointerException("e == null");
        }
        Integer size = sizeOf(e);
        Integer oldValue = mLruCache.put(e, size);
        return oldValue == null;
    }

    public final void trimToSize(int maxSize) {
    
    
        mLruCache.trimToSize(maxSize);
    }

    public final boolean remove(E e) {
    
    
        if (e == null) {
    
    
            throw new NullPointerException("e == null");
        }
        Integer oldValue = mLruCache.remove(e);
        return oldValue != null;
    }

    /**
     * <p>The method is called without synchronization: other threads may
     * access the cache while this method is executing.
     *
     * @param evicted true if the entry is being removed to make space, false
     *                if the removal was caused by a {@link #remove}.
     */
    protected void elementRemoved(boolean evicted, E e) {
    
    
    }

    protected int sizeOf(E e) {
    
    
        return 1;
    }

    public final void evictAll() {
    
    
        mLruCache.evictAll();
    }

    public final int size() {
    
    
        return mLruCache.size();
    }

    public final int maxSize() {
    
    
        return mLruCache.maxSize();
    }

    public final int hitCount() {
    
    
        return mLruCache.hitCount();
    }

    public final int missCount() {
    
    
        return mLruCache.missCount();
    }

    public final int createCount() {
    
    
        return mLruCache.createCount();
    }

    public final int putCount() {
    
    
        return mLruCache.putCount();
    }

    public final int evictionCount() {
    
    
        return mLruCache.evictionCount();
    }

    public final Set<E> snapshot() {
    
    
        Map<E, Integer> map = mLruCache.snapshot();
        return new LinkedHashSet<>(map.keySet());
    }

    protected final Object getSynchronizedObject() {
    
    
        return mLruCache;
    }

    @Override
    public final String toString() {
    
    
        int maxSize;
        int hitCount;
        int missCount;
        synchronized (mLruCache) {
    
    
            maxSize = maxSize();
            hitCount = hitCount();
            missCount = missCount();
        }
        int accesses = hitCount + missCount;
        int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0;
        return String.format("LruCacheSet[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]",
                maxSize, hitCount, missCount, hitPercent);
    }
}

猜你喜欢

转载自blog.csdn.net/hegan2010/article/details/105691421
今日推荐