160527、项目上线后session(远程session)
2016-05-31 14:17
253 查看
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import com.danga.MemCached.MemCachedClient;
/**
* 远程Session存放在Memcached缓存服务器里的Session
*/
public class CacheSessionProvider implements SessionProvider{
@Autowired
private MemCachedClient memCachedClient;
private int expiry = 30;//分钟
private static final String JSESSIONID = "JSESSIONID";
//放值
@Override
public void setAttribute(HttpServletRequest request,HttpServletResponse response, String name,
Serializable value) {
/* HttpSession session = request.getSession();//true Cookie JSESSIONID
session.setAttribute(name, value); */
//本地有一份
//另一份Memcached
Map<String,Serializable> session = new HashMap<String,Serializable>();
session.put(name, value);
//保存远程了
memCachedClient.set(getSessionId(request,response), session, expiry*60);
}
//取值
@SuppressWarnings("unchecked")
@Override
public Serializable getAttribute(HttpServletRequest request,HttpServletResponse response, String name) {
Map<String,Serializable> session = (Map<String, Serializable>) memCachedClient.get(getSessionId(request,response));
if(null != session){
return session.get(name);
}
return null;
}
@Override
public void logout(HttpServletRequest request,HttpServletResponse response) {
// TODO Auto-generated method stub
if(memCachedClient.keyExists(getSessionId(request, response))){
memCachedClient.delete(getSessionId(request, response));
}
//清理Cookie
}
@Override
public String getSessionId(HttpServletRequest request,HttpServletResponse response) {
//所有的Cookie
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0){
for(Cookie c : cookies){
if(JSESSIONID.equals(c.getName())){
return c.getValue();
}
}
}
//生成一个
String sessionId = UUID.randomUUID().toString().replaceAll("-", "");
Cookie cookie = new Cookie(JSESSIONID,sessionId);
cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
return sessionId;
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
}
注意:平时开发的时候是本地session,上线后放在缓存里面,所以将httpsession缓存了cachesession。spring的配置文件中加上<bean id="sessionProvider" class="cn.itcast.common.web.session.CacheSessionProvider">
<property name="expiry" value="60"/>
</bean>
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import com.danga.MemCached.MemCachedClient;
/**
* 远程Session存放在Memcached缓存服务器里的Session
*/
public class CacheSessionProvider implements SessionProvider{
@Autowired
private MemCachedClient memCachedClient;
private int expiry = 30;//分钟
private static final String JSESSIONID = "JSESSIONID";
//放值
@Override
public void setAttribute(HttpServletRequest request,HttpServletResponse response, String name,
Serializable value) {
/* HttpSession session = request.getSession();//true Cookie JSESSIONID
session.setAttribute(name, value); */
//本地有一份
//另一份Memcached
Map<String,Serializable> session = new HashMap<String,Serializable>();
session.put(name, value);
//保存远程了
memCachedClient.set(getSessionId(request,response), session, expiry*60);
}
//取值
@SuppressWarnings("unchecked")
@Override
public Serializable getAttribute(HttpServletRequest request,HttpServletResponse response, String name) {
Map<String,Serializable> session = (Map<String, Serializable>) memCachedClient.get(getSessionId(request,response));
if(null != session){
return session.get(name);
}
return null;
}
@Override
public void logout(HttpServletRequest request,HttpServletResponse response) {
// TODO Auto-generated method stub
if(memCachedClient.keyExists(getSessionId(request, response))){
memCachedClient.delete(getSessionId(request, response));
}
//清理Cookie
}
@Override
public String getSessionId(HttpServletRequest request,HttpServletResponse response) {
//所有的Cookie
Cookie[] cookies = request.getCookies();
if(null != cookies && cookies.length > 0){
for(Cookie c : cookies){
if(JSESSIONID.equals(c.getName())){
return c.getValue();
}
}
}
//生成一个
String sessionId = UUID.randomUUID().toString().replaceAll("-", "");
Cookie cookie = new Cookie(JSESSIONID,sessionId);
cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
return sessionId;
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
}
注意:平时开发的时候是本地session,上线后放在缓存里面,所以将httpsession缓存了cachesession。spring的配置文件中加上<bean id="sessionProvider" class="cn.itcast.common.web.session.CacheSessionProvider">
<property name="expiry" value="60"/>
</bean>
相关文章推荐
- Leetcode 58 Length of Last Word
- MySQL数据备份之mysqldump
- easyui tree 多级节点的的某个节点的所有子节点id查询
- VMware vCenter Server Appliance 部署向导文档
- 线程通信
- spark部署、计算模型、内部执行原理、工作机制详解
- 浅谈一下weex,如有错误,请阿里大神指正
- 《Linux及安全》实践3.3
- 开源播放框架ijkplayer(iOS版)使用教程
- Makefile入门
- 边做边学Python Flask Web开发(1)-- 安装Python
- C# 图形的绘制矩形椭圆
- mysql 常用SQL语句
- RHRL7.0安装RAC11.2.0.4----修改主机名
- 实践三
- Angular1.0
- MediaRecorder 录制视频不支持暂停续录的问题
- 第14周-项目2-两个成员的类模板(2)
- Vi经典插件ctags(转)
- Photoshop制作大气华丽的游戏首页