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. spring boot-html和templates

    静态页面 spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下 /static /public /resources / ...

  2. FOJ Problem 2260 Card Game

                                                                                            Problem 2260 ...

  3. 部分转 php kafka

    Step 1: 下载Kafka (官网地址:http://kafka.apache.org) Kafka入门经典教程 http://www.aboutyun.com/thread-12882-1-1. ...

  4. upper_bound()和lower_bound()

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  5. Scanner用法

    先来看一个简单的例子: import java.util.*; public class ScannerTest { public static void main(String[] args){   ...

  6. WCF的学习之旅

    一.WCF的简单介绍  Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是MS为SOA (S ...

  7. FMDB使用的数据库的三种形式

    FMDB使用的数据库的三种形式   FMDB是iOS平台下一款优秀的第三方SQLite数据库框架.它以Objective-C的方式封装了SQLite的C语言API.使用起来,它更加面向对象,避免冗余的 ...

  8. luogu P2066 机器分配

    题目背景 无 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15 ...

  9. [Bzoj4942][Noi2017]整数(线段树)

    4942: [Noi2017]整数 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 363  Solved: 237[Submit][Status][D ...

  10. Atcoder Grand Contest 024

    A 略 B 略 C 略 D(构造分形) 题意: 给出一个由n个点的组成的树,你可以加一些点形成一个更大的树.对于新树中的两个点i和j,如果以i为根的树与以j为根的树是同构的那么i和j颜色可以相同.问最 ...