java cache教程_自己动手实现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 __cacheList = new java.util.Hashtable();

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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值