自己动手实现java中cache
实现思路:
创建一个静态Hashtable用于保存key和value,对于cache过期后的方法回调,在cache过期后,再访问cache的时候进行,避免了使用定时器轮询过期时间,进行cache清除的效率损耗。
使用synchronized关键字进行多线程同步。
包括二个类和一个接口:
cache类:里面都是静态方法,提供基于key,value的方法进行cache的添加,修改,访问,进行cache过期后调用callback方法。
cacheitem类:用于管理每个条目的cache内容和超时时间回调方法
ICacheMethod接口:cache到期回调方法需要实现的接口
cache类:里面都是静态方法
import java.util.Date;
public class Cache {
private static java.util.Hashtable<String, Object> __cacheList = new java.util.Hashtable<String, Object>();
public Cache() {
}
// 添加cache,不过期
public synchronized static void add(String key, Object value) {
Cache.add(key, value, -1);
}
// 添加cache有过期时间
public synchronized static void add(String key, Object value, long timeOut) {
Cache.add(key, value, timeOut, null);
}
// 添加cache有过期时间并且具有回调方法
public synchronized static void add(String key, Object value, long timeOut, ICacheMethod callback) {
if (timeOut > 0) {
timeOut += new Date().getTime();
}
CacheItem item = new CacheItem(key, value, timeOut, callback);
Cache.__cacheList.put(key, item);
}
// 获取cache
public synchronized static Object get(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj == null) {
return null;
}
CacheItem item = (CacheItem) obj;
boolean expired = Cache.cacheExpired(key);
if (expired == true) // 已过期
{
if (item.getCallback() == null) {
Cache.remove(key);
return null;
} else {
ICacheMethod callback = item.getCallback();
callback.execute(key);
expired = Cache.cacheExpired(key);
if (expired == true) {
Cache.remove(key);
return null;
}
}
}
return item.getValue();
}
// 移除cache
public synchronized static void remove(String key) {
Object obj = Cache.__cacheList.get(key);
if (obj != null) {
obj = null;
}
Cache.__cacheList.remove(key);
}
// 清理所有cache对象
public synchronized static void clear() {
for (String s : Cache.__cacheList.keySet()) {
Cache.__cacheList.put(s, null);
}
Cache.__cacheList.clear();
}
// 判断是否过期
private static boolean cacheExpired(String key) {
CacheItem item = (CacheItem) Cache.__cacheList.get(key);
if (item == null) {
return false;
}
long milisNow = new Date().getTime();
long milisExpire = item.getTimeOut();
if (milisExpire <= 0) { // 不过期
return false;
} else if (milisNow >= milisExpire) {
return true;
} else {
return false;
}
}
}
public class CacheItem {
private String key;
private Object value;
private long timeOut;
private ICacheMethod callback = null;
public CacheItem() {
}
public ICacheMethod getCallback() {
return callback;
}
public void setCallback(ICacheMethod callback) {
this.callback = callback;
}
public CacheItem(String key, Object value) {
this.key = key;
this.value = value;
this.timeOut = 0;
}
public CacheItem(String key, Object value, long timeOut) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
}
public CacheItem(String key, Object value, long timeOut, ICacheMethod callback) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.callback = callback;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
}
public interface ICacheMethod {
public void execute(String key);
}
自己动手实现java中cache的更多相关文章
- Java中各类Cache机制实现解决方案[来自CSDN]
摘要:在Java中,不同的类都有自己单独的Cache机制,实现的方法也可能有所不同,文章列举了Java中常见的各类Cache机制的实现方法,同时进行了综合的比较. 在Java中,不同的类都有自己单独的 ...
- Java中内存中的Heap、Stack与程序运行的关系
堆和栈的内存管理 栈的内存管理是顺序分配的,而且定长,不存在内存回收问题:而堆 则是随机分配内存,不定长度,存在内存分配和回收的问题:堆内存和栈内存的区别可以用如下的比喻来看出:使用堆内存就象是自己动 ...
- java 中hashcode和equals 总结
一.概述 在Java中hashCode的实现总是伴随着equals,他们是紧密配合的,你要是自己设计了其中一个,就要设计另外一个.当然在多数情况下,这两个方法是不用我们考虑的,直 ...
- 谈谈 Java 中的那些“琐”事
一.公平锁&非公平锁 是什么 公平锁:线程按照申请锁的顺序来获取锁:在并发环境中,每个线程都会被加到等待队列中,按照 FIFO 的顺序获取锁. 非公平锁:线程不按照申请锁的顺序来获取锁:一上来 ...
- Java中的关键字 transient
先解释下Java中的对象序列化 在讨论transient之前,有必要先搞清楚Java中序列化的含义: Java中对象的序列化指的是将对象转换成以字节序列的形式来表示,这些字节序列包含了对象的数据和信息 ...
- java中关键字volatile的作用
用在多线程,同步变量. 线程为了提高效率,将某成员变量(如A)拷贝了一份(如B),线程中对A的访问其实访问的是B.只在某些动作时才进行A和B的同步.因此存在A和B不一致的情况.volatile就是用来 ...
- 深入理解Java中的String
一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...
- 浅析Java中的final关键字(转载)
自http://www.cnblogs.com/dolphin0520/p/3736238.html转载 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括 ...
- java中注解的使用与实例 (二)
java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单.注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后 某个时刻方便地使用这些数据(通过 解 ...
随机推荐
- postmortem report of period M1
一.设想和目标 1.我们的软件主要要解决学长设计的学霸系统中视频及文档的浏览功能问题. 2.时间相对充裕.不过对于我们这些零基础的人来说还是比较困难. 3.我们团队中不同意见通常会进行进一步讨论,说出 ...
- NABC的特点分析
题目: 请把采用卡片分类的方法讨论你们的团队开发项目特点,再按照 NABC 的框架分析每个特点. 每一个组员针对其中的一个特点将NABC的分析结果发表博 ...
- unity 协同
void Update () { if(Input .GetKeyDown (KeyCode .W )) { StartCoroutine ("Test"); } } IEnume ...
- 用 Android-X86 和 VirtualBox 玩安卓游戏
目前的系统是 Ubuntu 14.04,近日玩了玩 flash 版的<皇家禁卫军:前线>塔防游戏,还是想试试原生安卓游戏的表现.发现大概有两个选择: 各类安卓模拟器:官方SDK模拟器,bl ...
- 如何用pdfbox-app-1.8.10.jar批处理将pdf文档转换成text文档
1.首先下载pdfbox-app-1.8.10.jar(下载地址:http://pdfbox.apache.org/download.html) 2.将pdfbox-app-1.8.10.jar加载到 ...
- hibernate学习笔记--可选的配置属性
3.4. 可选的配置属性 有大量属性能用来控制Hibernate在运行期的行为. 它们都是可选的, 并拥有适当的默认值. 警告: 其中一些属性是"系统级(system-level)的&qu ...
- 【python】编码规范(转载)
转自:http://www.cnblogs.com/itech/archive/2012/01/06/2314454.html 1 编码 >>所有的 Python 脚本文件都应在文件头标上 ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Notes on the Dirichlet Distribution and Dirichlet Process
Notes on the Dirichlet Distribution and Dirichlet Process In [3]: %matplotlib inline Note: I wrote ...
- WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results
GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...