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();
}
}

基于LRU Cache的简单缓存的更多相关文章

  1. 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)

    一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...

  2. 基于Spring Cache实现二级缓存(Caffeine+Redis)

    一.聊聊什么是硬编码使用缓存? 在学习Spring Cache之前,笔者经常会硬编码的方式使用缓存. 我们来举个实际中的例子,为了提升用户信息的查询效率,我们对用户信息使用了缓存,示例代码如下: @A ...

  3. LRU Cache的简单c++实现

    什么是 LRU LRU Cache是一个Cache的置换算法,含义是“最近最少使用”,把满足“最近最少使用”的数据从Cache中剔除出去,并且保证Cache中第一个数据是最近刚刚访问的,因为这样的数据 ...

  4. 使用Springboot Cache做简单缓存

    使用Springboot Cache做简单缓存 1.简单介绍 ​ 当我们需要展示数据的时候,后台会根据需要从服务器中获取数据,但是频繁的请求数据库会对服务造成压力,于是我们引入了缓存这个概念. ​ 当 ...

  5. Redis(八) LRU Cache

    Redis(八)-- LRU Cache 在计算机中缓存可谓无所不在,无论还是应用还是操作系统中,为了性能都需要做缓存.然缓存必然与缓存算法息息相关,LRU就是其中之一.笔者在最先接触LRU是大学学习 ...

  6. LRU cache缓存简单实现

    LRU cache LRU(最近最少使用)是一种常用的缓存淘汰机制.当缓存大小容量到达最大分配容量的时候,就会将缓存中最近访问最少的对象删除掉,以腾出空间给新来的数据. 实现 (1)单线程简单版本 ( ...

  7. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  8. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

随机推荐

  1. 刷题总结——doing homework again(hdu1789)

    题目: Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot ...

  2. 深入浅出mysql全文随笔

    进入mysql :mysql -uroot -p 1.DDL(Data Definition Languages)语句:数据定义语言 2.DML(Data Manipulation Language) ...

  3. 【BZOJ4555】【TJOI2016】【HEOI2016】求和 (第二类斯特林数+NTT卷积)

    Description 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: $$f(n)=\sum_{i=0}^n\sum_{j=0}^i S(i,j)\tim ...

  4. Nginx配置https双向认证

    1.      前期的准备工作: 安装openssl和nginx的https模块 cd ~/ mkdir ssl cd ssl mkdir demoCA cd demoCA mkdir newcert ...

  5. 電池的標稱電壓 與 power consumption 量測

    電池標稱電壓 定義如下圖, 以25度為例,20度再往上點, 4V 放一下電就往下掉, 3V 放一下電就往下掉, 假設 3.8V 是擁有最多 capacity 可以 discharge 的電壓,放電放了 ...

  6. [virtualbox] virtualbox 安裝 ubuntu,但 virtualbox 卻無法執行 ubuntu 的快速鍵,解法方式

    solution open virtualbox -> file -> preference -> input -> below picture 按下快速鍵,即發生作用. 原先 ...

  7. 快充 IC BQ25896 的 input current monitor

    BQ25896 沒有顯示 input current 的 register, 但可以讀 ILIM pin 的 電壓 做計算求出 input current.

  8. 解决npm 的 shasum check failed for错误

    使用npm安装一些包失败,类似如下报错情况:   C:\Program Files\nodejs>npm update npm npm ERR! Windows_NT 10.0.14393 np ...

  9. (1)angularJs

    一. 1.下载 https://angularjs.org/ 2.网络引用 https://code.angularjs.org/ 3.模块内引用 angularjs <body ng-app& ...

  10. ML| EM

    What's xxx The EM algorithm is used to find the maximum likelihood parameters of a statistical model ...