基于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();
}
}
基于LRU Cache的简单缓存的更多相关文章
- 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)
一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...
- 基于Spring Cache实现二级缓存(Caffeine+Redis)
一.聊聊什么是硬编码使用缓存? 在学习Spring Cache之前,笔者经常会硬编码的方式使用缓存. 我们来举个实际中的例子,为了提升用户信息的查询效率,我们对用户信息使用了缓存,示例代码如下: @A ...
- LRU Cache的简单c++实现
什么是 LRU LRU Cache是一个Cache的置换算法,含义是“最近最少使用”,把满足“最近最少使用”的数据从Cache中剔除出去,并且保证Cache中第一个数据是最近刚刚访问的,因为这样的数据 ...
- 使用Springboot Cache做简单缓存
使用Springboot Cache做简单缓存 1.简单介绍 当我们需要展示数据的时候,后台会根据需要从服务器中获取数据,但是频繁的请求数据库会对服务造成压力,于是我们引入了缓存这个概念. 当 ...
- Redis(八) LRU Cache
Redis(八)-- LRU Cache 在计算机中缓存可谓无所不在,无论还是应用还是操作系统中,为了性能都需要做缓存.然缓存必然与缓存算法息息相关,LRU就是其中之一.笔者在最先接触LRU是大学学习 ...
- LRU cache缓存简单实现
LRU cache LRU(最近最少使用)是一种常用的缓存淘汰机制.当缓存大小容量到达最大分配容量的时候,就会将缓存中最近访问最少的对象删除掉,以腾出空间给新来的数据. 实现 (1)单线程简单版本 ( ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
随机推荐
- Java面试之JVM原理总结
1.什么是JVM? 答:JVM是Java Virual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,他是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟计算机功能来实现 ...
- Vmware Linux虚拟机磁盘扩容方法
我的LINUX版本是ubuntu12.04 32bit.今天在下载android源代码的时候发现自己最初给这个虚拟机分配的磁盘空间不足了(只有20G).所以就需要给磁盘扩容.网上大致搜索了一下,主要有 ...
- VMware 11 安装Mac 10.9
VMware Workstation 11, 10, 9 and 8 Unlocker to Run Mac OS X Guests in Windows 8.1 and 7 VM10装Mac OS ...
- git提交node-modules报文件名过长无法提交问题
当报如下错误时候 fatal: unable to stat 'node_modules/gulp-connect/node_modules/gulp-util/node_modules/datefo ...
- hdu 4941 2014 Multi-University Training Contest 7 1007
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- 标准C程序设计七---57
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 什麼是 struct,union,enumeration 的 tag ?
struct tag { member-list }; union tag { member-list }; enum tag { member-list }; union test1 { int a ...
- 这是一份很有诚意的2017 Google I/O大会的汇总 & 解析
前言 在刚过去的凌晨(北京时间 5月18日 1.00-3.00),一年一度的2017年Google I/O大会在美国谷歌山景城海岸线圆形剧场如期举行 Google I/O 大会:Innovation ...
- 小程序-TabBar点击切换
这种页面的布局会经常用到,所以在此做个笔记,之后遇到可以节省很多时间 WXML: <view class='listTitle_tab'> <view class='scr ...
- python学习笔记1-numpy/enumerate
1. np.size和np.prod import numpy as np x = np.zeros((3, 5, 2), dtype=np.complex128) # ndarray.size is ...