随便记记
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();
}
}
然后通过拦截器进行迭代更新,