自定义缓存

随便记记

service

public Object getSessionAttribute(HttpServletRequest request,String key){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			return cache.getCache(key);
		}
		return null;
	}
	
	public void removeSessionAttribute(HttpServletRequest request,HttpServletResponse response, String key){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			cache.removeCache(key);
		}
	}
	
	public void addSessionAttribute(HttpServletRequest request,HttpServletResponse response, String key,Object val){
		CookieCache cache = (CookieCache)request.getAttribute("CACHE");
		if(cache != null){
			cache.pushCache(key, val);
		}else{
			cache = new CookieCache();
			cache.pushCache(key, val);
			cache.setBindIp(request.getRemoteAddr());
			request.setAttribute("CACHE", cache);
			
		}
	}

bean

public class CookieCache implements java.io.Serializable{

	private static final long serialVersionUID = 246613021579031321L;

	private int randomId = new java.util.Random(System.currentTimeMillis()).nextInt();
	
	public String bindIp;
	
	private Map<String,Object> cache = new HashMap<String,Object>();
	
	/**
	 * @return the bindIp
	 */
	public String getBindIp() {
		return bindIp;
	}

	/**
	 * @return the randomId
	 */
	public int getRandomId() {
		return randomId;
	}



	/**
	 * @param randomId the randomId to set
	 */
	public void setRandomId(int randomId) {
		this.randomId = randomId;
	}



	/**
	 * @return the cache
	 */
	public Map<String, Object> getCache() {
		return cache;
	}



	/**
	 * @param cache the cache to set
	 */
	public void setCache(Map<String, Object> cache) {
		this.cache = cache;
	}



	/**
	 * @param bindIp the bindIp to set
	 */
	public void setBindIp(String bindIp) {
		this.bindIp = bindIp;
	}



	public void pushCache(String key,Object val){
		cache.put(String.format("%d%s", this.randomId,key), val);
	}
	
	public Object removeCache(String key){
		return cache.remove(String.format("%d%s", this.randomId,key));
	}
	
	public Object getCache(String key){
		return cache.get(String.format("%d%s", this.randomId,key));
	}
	
	@Override
	public String toString() {
		return JSONObject.fromObject(this).toString();
	}
}
然后通过拦截器进行迭代更新,




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值