java jdis 可以缓存map 吗
发布网友
发布时间:2022-05-06 10:50
我来回答
共1个回答
热心网友
时间:2022-06-29 19:48
缓存是Java中主要的内容,主要目的是缓解项目访问数据库的压力以及提升访问数据的效率,以下是通过Map实现java缓存的功能,并没有用cache相关框架。
一、缓存管理类 CacheMgr.java
[java] view plain copy
package cache.com;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 缓存管理类
* @author Administrator
*
*/
public class CacheMgr {
private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();
private static CacheMgr cm = null;
//构造方法
private CacheMgr(){
}
public static CacheMgr getInstance(){
if(cm==null){
cm = new CacheMgr();
Thread t = new ClearCache();
t.start();
}
return cm;
}
/**
* 增加缓存
* @param key
* @param value
* @param ccm 缓存对象
* @return
*/
public boolean addCache(Object key,Object value,CacheConfModel ccm){
System.out.println("开始增加缓存-------------");
boolean flag = false;
try {
cacheMap.put(key, value);
cacheConfMap.put(key, ccm);
System.out.println("增加缓存结束-------------");
System.out.println("now addcache=="+cacheMap.size());
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 获取缓存实体
*/
public Object getValue(String key){
Object ob=cacheMap.get(key);
if(ob!=null){
return ob;
}else{
return null;
}
}
/**
* 获取缓存数据的数量
* @return
*/
public int getSize(){
return cacheMap.size();
}
/**
* 删除缓存
* @param key
* @return
*/
public boolean removeCache(Object key){
boolean flag=false;
try {
cacheMap.remove(key);
cacheConfMap.remove(key);
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 清除缓存的类
* 继承Thread线程类
*/
private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet();
Set set = cacheConfMap.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
CacheConfModel ccm = (CacheConfModel)cacheConfMap.get(key);
//比较是否需要清除
if(!ccm.isForever()){
if((new Date().getTime()-ccm.getBeginTime())>= ccm.getDurableTime()*60*1000){
//可以清除,先记录下来
tempSet.add(key);
}
}
}
//真正清除
Iterator tempIt = tempSet.iterator();
while(tempIt.hasNext()){
Object key = tempIt.next();
cacheMap.remove(key);
cacheConfMap.remove(key);
}
System.out.println("now thread================>"+cacheMap.size());
//休息
try {
Thread.sleep(60*1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
二、缓存属性类 CacheConfModel.java
[java] view plain copy
package cache.com;
/**
* 缓存属性类
* @author Administrator
*
*/
public class CacheConfModel implements java.io.Serializable{
private long beginTime;//缓存开始时间
private boolean isForever = false;//是否持久
private int rableTime;//持续时间
public long getBeginTime() {
return beginTime;
}
public void setBeginTime(long beginTime) {
this.beginTime = beginTime;
}
public boolean isForever() {
return isForever;
}
public void setForever(boolean isForever) {
this.isForever = isForever;
}
public int getDurableTime() {
return rableTime;
}
public void setDurableTime(int rableTime) {
this.rableTime = rableTime;
}
}