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. P1266 速度限制 (最短路,图论)

    题目链接 Solution 在最短路转移的时候在队列或者堆中记录状态为 \(f[u][v]\) 代表上一个节点为 \(u\) ,速度为 \(v\) . 然后按部就班转移即可... Code #incl ...

  2. Hadoop HDFS 中的一些常用命令

    转载自:hadoop HDFS常用文件操作命令 命令基本格式: hadoop fs -cmd < args > 1.ls hadoop fs -ls / 列出hdfs文件系统根目录下的目录 ...

  3. java中static变量和方法的总结

    转自:http://blog.csdn.net/haobo920/article/details/5921621 java中static变量和方法的总结 java中一切皆是对象 一个类中对象的定义一般 ...

  4. 莫比乌斯函数之和(51nod 1244)

    莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如 ...

  5. 什么是GOP(转)

    所谓GOP,意思是画面组,MPEG格中的帧序列,分为I.P.B三种,如排成IBBPBBPBBPBBPBBP...样式,这种连续的帧图片组合即为GOP(画面群,GROUP OF PICTURE),是MP ...

  6. How to build and run ARM Linux on QEMU from scratch

    This blog shows how to run ARM Linux on QEMU! This can be used as a base for later projects using th ...

  7. Day 16 之二 省市县三级联动

    摘录自:雨神,供参考! province_dic = { "河北": { "石家庄": ["鹿泉", "藁城", &qu ...

  8. 牛客网 Wannafly挑战赛9 A.找一找-数据处理

    好几天没好好学习了(咸鱼晒干了) 把稍微没那么咸鱼的几天前的一场牛客网的比赛稍微看了一下,菜的要死,这一场大数的比较多,都死了. A.找一找 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...

  9. 如何快速定位TempDB产生问题

    步骤1.TempDB压力诊断 等待类型诊断 TempDB的争用压力在等待篇中已经简单介绍,等待的表现为 pagelatch_类等待,等待的资源是 “2: X :X ” tempDB所在磁盘的响应时间 ...

  10. android ListView详解(转)

    在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示.抽空把对ListView的使用做了整理,并写了个小例子,如下图. 列表的显示需要三 ...