重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package qinbo hui;
创新互联于2013年创立,是专业互联网技术服务公司,拥有项目网站设计、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元富锦做网站,已为上家服务,为富锦各地企业和个人服务,联系电话:13518219792
/*
设计思想来自-回钦波(qq: )
*/
public class CacheConfModel implements java io Serializable{
private long beginTime;
private boolean isForever = false;
private int durableTime;
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 durableTime;
}
public void setDurableTime(int durableTime) {
this durableTime = durableTime;
}
}
package qinbo hui;
import java util *;
import test CacheConfModel;
/*
设计思想来自-回钦波(qq: )
*/
public class CacheMgr {
private static Map cacheMap = new HashMap();
private static Map cacheConfMap = new HashMap();
private CacheMgr(){
}
private static CacheMgr cm = null;
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){
boolean flag = false;
cacheMap put(key value);
cacheConfMap put(key ccm);
System out println( now addcache== +cacheMap size());
return true;
}
/**
* 删除缓存
* @param key
* @return
*/
public boolean removeCache(Object key){
cacheMap remove(key);
cacheConfMap remove(key);
System out println( now removeCache== +cacheMap size());
return true;
}
/**
* 清除缓存的类
* @author wanglj
* 继承Thread线程类
*/
private static class ClearCache extends Thread{
public void run(){
while(true){
Set tempSet = new HashSet();
Set set = cacheConfMap keySet();
Iterator it = erator();
while(it hasNext()){
Object key = it next();
CacheConfModel ccm = (CacheConfModel)cacheConfMap get(key);
//比较是否需要清除
if(!ccm isForever()){
if((new Date() getTime() ccm getBeginTime())= ccm getDurableTime()* * ){
//可以清除 先记录下来
tempSet add(key);
}
}
}
//真正清除
Iterator tempIt = erator();
while(tempIt hasNext()){
Object key = tempIt next();
cacheMap remove(key);
cacheConfMap remove(key);
}
System out println( now thread================ +cacheMap size());
//休息
try {
Thread sleep( * L);
} catch (InterruptedException e) {
// TODO Auto generated catch block
e printStackTrace();
}
}
}
}
lishixinzhi/Article/program/Java/hx/201311/25737
?阏飧龊诵牡牡胤剑?褪莂ction这儿不去查数据库,而拿到缓存的数据再直接返回到前台嘛。核心代码逻辑就是:longobsoleteTime=1天;
List
list=cache.get(abc,
obsoleteTime);if(list==null){
list=manager.search(..);
这样的缓存策略很多的啊。比如oscache就可以达到要求,而且用起来很简单,只要一个jar,自己抽象一个cache的接口,套上去,就能用了。
java放入session缓存中
方法如下:
session.setAttribute("Name",Value);
Name 随便取,value就是要放的数据
获取的时候session.getAttribute("Name);
就可以了