基于LRU Cache的简单缓存

package com.test.testCache;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.LruCache;

public class TestStore
{
    private final static int CACHE_SIZE = 1000;
    private LruCache<String, String> mContent;

    public TestStore(Context context)
    {
        this(context, CACHE_SIZE);
    }

    public TestStore(Context context, int size)
    {
        super();
        mContent = new LruCache<String, String>(size);
        JSONArray arry = getTopicReadData(context);
        if (arry == null) {
            return;
        }
        for (int i = 0; i < arry.length(); i++) {
            String content = null;
            try {
                content = arry.getString(i);
            } catch (JSONException e) {
            }
            if (TextUtils.isEmpty(content)) {
                continue;
            }
            putReadData(content);
        }
    }

    public boolean isExsit(String key) {
        String value = mContent.get(key);
        return !TextUtils.isEmpty(value);
    }

    public void putReadData(String key) {
        mContent.put(key, key);
    }

    public void saveDataToFile(Context context) {
        JSONArray json = new JSONArray();
        Map<String, String> map = mContent.snapshot();
        for (String id : map.values()) {
            json.put(id);
        }
        saveTopicReadData(context, json);
    }

    public JSONArray getTopicReadData(Context context) {
        SharedPreferences preferences = getSharedPreferences(context);
        String content = preferences.getString(STORE_KEY_TOPIC_READDATA, null);
        JSONArray array = null;
        try {
            array = new JSONArray(content);
        } catch (Exception e) {
        }
        return array;
    }

    private void saveTopicReadData(Context context, JSONArray array) {
        Editor edit = getSharedPreferencesEditor(context);
        edit.putString(STORE_KEY_TOPIC_READDATA, array.toString());
        edit.commit();
    }

    // =======================================
    private final static String STORE_NAME = "1111";
    private final static String STORE_KEY_TOPIC_READDATA = "22222";

    private SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(STORE_NAME, 0);
    }

    private Editor getSharedPreferencesEditor(Context context) {
        return getSharedPreferences(context).edit();
    }
}

 

转载于:https://www.cnblogs.com/lchd/p/4218593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值