Android从Cache缓存和恢复的简单方法

时间:2016-09-05 09:39:40

标签: android caching

我在Github上找到了这个课程,并且我试图使用它,我的问题是我无法从chach中检索或恢复数据,例如:

MemCache类:

import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.HashMap;

public class MemCache<K, V>
{
    private final HashMap<K, CacheValue<K, V>> mCache = new HashMap<K, CacheValue<K, V>>();
    private final ReferenceQueue<V> mRefQueue = new ReferenceQueue<V>();

    public V get(K key)
    {
        prune();
        CacheValue<K, V> ref = mCache.get(key);
        if (ref != null)
        {
            V value = ref.get();
            if (value != null)
                return value;
            else
                mCache.remove(key);
        }
        return null;
    }

    public void put(K key, V value)
    {
        prune();
        mCache.put(key, new CacheValue<K, V>(key, value, mRefQueue));
    }

    public V remove(K key)
    {
        prune();
        CacheValue<K, V> ref = mCache.remove(key);
        if (ref != null)
        {
            V value = ref.get();
            if (value != null)
                return value;
        }
        return null;
    }

    public int size()
    {
        return mCache.size();
    }

    public void clear()
    {
        while (mRefQueue.poll() != null)
            /* Do nothing... */;

        mCache.clear();
    }

    @SuppressWarnings("unchecked")
    private void prune()
    {
        CacheValue<K, V> ref;
        while ((ref = (CacheValue<K, V>)mRefQueue.poll()) != null)
        {
            K key = ref.key.get();
            if (key != null)
                mCache.remove(key);
        }
    }

    /**
     * @deprecated Do not use.
     */
    public ReferenceQueue<V> getReferenceQueue()
    {
        return mRefQueue;
    }

    private static class CacheValue<Key, Value> extends SoftReference<Value>
    {
        /**
         * Reference to the key that installed this value so we can prune
         * entries when the value expires.
         * <p>
         * Must be weakly referenced for the case where the entry is removed
         * from the cache, but still strongly referenced because of the
         * reference queue.
         */
        private final WeakReference<Key> key;

        public CacheValue(Key key, Value value, ReferenceQueue<? super Value> queue)
        {
            super(value, queue);
            this.key = new WeakReference<Key>(key);
        }
    }
}

将数据放入缓存:

public static  MemCache<String,List<SimCards>> cache       = new MemCache<>();

private List<SimCards> sims = new ArrayList<>();

...

SimCards tmp = new SimCards();
tmp.setID("1");
tmp.setNumber("0000");
tmp.setPrice("123");
tmp.setSaleSuccess("1");
sims.add(tmp);
Application.cache.put("all_simcards", sims);
sims.clear();

从缓存中恢复和我的问题:

if (Application.cache.get("all_simcards") != null) {
    sims.addAll(Application.cache.get("all_simcards"));
}

问题是Application.cache.get("all_simcards")返回null,因为sims.clear();清除了我的缓存!!!

1 个答案:

答案 0 :(得分:0)

似乎缓存存储了对象的引用。它没有复制它。因此,如果您致电sims.clear();,则.clear()会影响缓存的值。只是不要致电sims.clear();。或者创建一个新的List,添加要缓存的对象,并存储List而不是原始对象。