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. vim 翻页命令记录

    vim命令: ctrl-f:往前翻一页(forward) ctrl-b:往后翻一页(backward) ​ ctrl-d:往下翻半页(down) ctrl-u:往上翻半页(up) ​​​

  2. hdu 1695 容斥原理或莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. ubuntu 安装自启动管理

    #sudo apt-get update #sudo apt-get install sysv-rc-conf 运行:#sudo sysv-rc-conf 也可以直接加入启动程序,例如把 /etc/i ...

  4. [原创][FPGA]时钟分频之奇分频(5分频)

    0. 简介 有时在基本模块的设计中常常会使用到时钟分频,时钟的偶分频相对与奇分频比较简单,但是奇分频的理念想透彻后也是十分简单的,这里就把奇分频做一个记录. 1. 奇分频 其实现很简单,主要为使用两个 ...

  5. loj #110. 乘法逆元

    #110. 乘法逆元 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 这是一道模板题. 给定 ...

  6. SPOJ 8222 Substrings

    题面 Description 给长度为 n 的字符串 S , 对任意的 L , 求长度为 L 的子串最多出现的次数. Input String S consists of at most 250000 ...

  7. -[__NSCFString longValue]: unrecognized selector sent to instance

    You can use NSString methods intValue longLongValue. 这个使用longvalue会crash报上面的错误

  8. 【java】google的zxing架包生成二维码和读取二维码【可带文字和logo】

    承接RC4生成不重复字符串的需求之后,因为优惠码要方便用户使用的缘故,所以思来想去,觉得还是直接生成二维码给用户直接扫比较实用,也不用用户专门记录冗长的优惠码编号. ================= ...

  9. layer的alert、prompt等操作如何响应键盘的回车和ESC操作

    layer.prompt({title: '请输入数据', formType: 1, //隐藏用户输入内容 // 这个是确定按钮的事件 "success":function(){ ...

  10. mac下报错 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    如题mac下遇到错误: 解决办法:安装mac的命令行工具CommandLineTools xcode-select --install