1、新巴巴运动网 day10新巴巴运动网总结笔记day10项目第十天(课程安排)1:在Linux系统上搭建Memcached服务器2:在项目中配置Memcached的客户端来连接Memcached服务器3:手动配置Spring切面(增,删,改,查),操作分布式缓存Memcached4:解决共享Session1 在Linux系统上搭建Memcached服务器1.1 在虚拟机上安装Centos61.2 在Centos6上安装Memcached服务器1.2.1 如果有网络:yum y install memcached1.2.2 如果没有网络: 上传libevent-1.4.13-4.el6.i686.
2、rpmmemcached-1.4.4-3.el6.i686.rpmrpm ivh libevent-1.4.13-4.el6.i686.rpm memcached-1.4.4-3.el6.i686.rpm1.2.3 操作memcached服务器#启动service memcached start#关闭service memcached stop开机自启动chkconfig memcached on查看开机自启动状态chkconfig memcached status1.2.4 Window7下dos连接Memcached服务器第一步:开启telnet 控制面板-程序和功能打开或关闭window
3、s第二步:运行cmd进入dos命令界面telnet ip 11211设置键值对到Memcached中去(命令)(Key)(标记)(时间)(值的长度) set uuu 0 0 4wwww取值出来get uuu2 在项目中配置Memcached的客户端来连接Memcached服务器2.1 位置2.2 内容 sockIOPool sockIOPool 192.168.200.143:11211 1 2.3 测试/* * 测试Memcached客户端 * author lx * */public class TestMemcached extends SpringJunitTest Autowired
4、 private MemCachedClient memCachedClient; Test public void testMemcached() throws Exception /存数据到Memcached memCachedClient.set(haha, abc); /从Memcached中取数据 Object o = memCachedClient.get(haha); System.out.println(o); 3 手动配置Spring切面增,删,改,查,操作分布式缓存Memcached3.1 创建memcached.xml位置配置内容 3.2 创建CacheIntercept
5、or切面对象位置内容准备/* * 基于Spring aop 的 切面对象 * 处理切面有三种 * after * before * around * author lx * */public class CacheInterceptor Autowired private MemCachedClient memCachedClient; /缓存默认超时时间 private static final int TIMEOUT = 3600000;生成唯一的Key策略 /生成Key策略 public String getCacheKey(ProceedingJoinPoint jp) throws
6、JsonGenerationException, JsonMappingException, IOException StringBuilder key = new StringBuilder(); /cn.itcast.core.service.order.OrderServiceImpl + getOrderListWithPage + (OrderQuery orderQuery) String packageName = jp.getTarget().getClass().getName(); String methodName = jp.getSignature().getName(
7、); Object args = jp.getArgs(); /序列化 ObjectMapper om = new ObjectMapper(); om.setSerializationInclusion(Inclusion.NON_NULL); /创建字符串流 StringWriter str = new StringWriter(); for(Object arg : args) om.writeValue(str,arg); key.append(packageName) .append(.) .append(methodName) .append(.) .append(str.toSt
8、ring(); return key.toString(); 查询时切面方法 /查询时切面方法 public Object doAround(ProceedingJoinPoint jp) throws Throwable System.out.println(进入了doAround); /保存数据到Memcached中去 /memcached原理 /Map map = new HashMap(); /第一件事:连接memcached服务器 if(memCachedClient.stats().isEmpty() System.out.println(memcached服务器没有开启或无法连接
9、!); return jp.proceed(); /第二件事:存储时的Key值 如何生成 /Key /1:要求不能出现重复 /2:要有规则,在不存储的情况下,可以随时把我们存储起来的数据找出来 String cacheKey = getCacheKey(jp); /查看memcached服务器中有没有缓存的数据 if(null = memCachedClient.get(cacheKey) Object proceed = jp.proceed(); memCachedClient.set(cacheKey, proceed, TIMEOUT); return memCachedClient.
10、get(cacheKey); 变更时切面方法 /更改时切面方法 public void doAfter(JoinPoint jp) String name = jp.getTarget().getClass().getName(); /cn.itcast.core.service.order.OrderServiceImpl /add* update* delete* /get* /获取memcached中的所有Key Map keys = MemCachedUtil.getKeySet(memCachedClient); /Map 遍历所有key SetEntry entrySet = ke
11、ys.entrySet(); for(Entry entry : entrySet) String key = entry.getKey(); if(key.startsWith(name) memCachedClient.delete(key); 测试1) 测试Controller层访问Service层时,是否能进入切面对象2) 第一次查询时,通过打断点,可看到进入Service查询数据库数据,第二次查询时,通过打断点,可看到不再进入Service,而是去缓存服务器查数据3) 数据变更时,在Service方法后清除此数据库表对应的Service层所有查询时缓存下来的数据4 共享Session
12、解决4.1 位置4.2 CacheSessionProvider提供类的内容注入对象/* * 采用Memcached来存储Session中的用户对象 * * author lx * */public class CacheSessionProvider implements SessionProvider /注入MemachedClient Autowired private MemCachedClient memCachedClient; /设置一个Session超时时间 private int sessionTimeout = 30; /单位分钟往Session中设置值 /往Session
13、中设置值 public void setAttribute(HttpServletRequest request,String name ,Serializable value) /创建一个Map Map map = new HashMap(); /装用户对象到map中 map.put(name, value); /将Map发送到memached服务器中去 memCachedClient.set(getSessionId(request), map,sessionTimeout*60);/单位是秒 获取Session中的值 /获取Session中的值 SuppressWarnings(unch
14、ecked) public Serializable getAttribute(HttpServletRequest request,String name) /从Memcached中取 Map map = null; map = (Map) memCachedClient.get(getSessionId(request); if(null != map) return map.get(name); else return null; 退出登陆 /退出登陆 public void logout(HttpServletRequest request,HttpServletResponse re
15、sponse) /让memcached中的数据失效 if(memCachedClient.keyExists(getSessionId(request) memCachedClient.delete(getSessionId(request); 获取SessionId /获取Session Id public String getSessionId(HttpServletRequest request) /JSESSIONID = 2342141234asdfas return request.getRequestedSessionId(); 4.3 把CacheSessionProvider交给Spring实例类位置内容 测试
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2