新巴巴运动网day10.docx

上传人:b****1 文档编号:10750675 上传时间:2023-05-27 格式:DOCX 页数:14 大小:192.67KB
下载 相关 举报
新巴巴运动网day10.docx_第1页
第1页 / 共14页
新巴巴运动网day10.docx_第2页
第2页 / 共14页
新巴巴运动网day10.docx_第3页
第3页 / 共14页
新巴巴运动网day10.docx_第4页
第4页 / 共14页
新巴巴运动网day10.docx_第5页
第5页 / 共14页
新巴巴运动网day10.docx_第6页
第6页 / 共14页
新巴巴运动网day10.docx_第7页
第7页 / 共14页
新巴巴运动网day10.docx_第8页
第8页 / 共14页
新巴巴运动网day10.docx_第9页
第9页 / 共14页
新巴巴运动网day10.docx_第10页
第10页 / 共14页
新巴巴运动网day10.docx_第11页
第11页 / 共14页
新巴巴运动网day10.docx_第12页
第12页 / 共14页
新巴巴运动网day10.docx_第13页
第13页 / 共14页
新巴巴运动网day10.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

新巴巴运动网day10.docx

《新巴巴运动网day10.docx》由会员分享,可在线阅读,更多相关《新巴巴运动网day10.docx(14页珍藏版)》请在冰点文库上搜索。

新巴巴运动网day10.docx

新巴巴运动网day10

新巴巴运动网总结笔记day10

项目第十天(课程安排)

1:

在Linux系统上搭建Memcached服务器

2:

在项目中配置Memcached的客户端来连接Memcached服务器

3:

手动配置Spring切面(增,删,改,查),操作分布式缓存Memcached

4:

解决共享Session

1在Linux系统上搭建Memcached服务器

1.1在虚拟机上安装Centos6

1.2在Centos6上安装Memcached服务器

1.2.1如果有网络:

yum–yinstallmemcached

1.2.2如果没有网络:

上传

libevent-1.4.13-4.el6.i686.rpm

memcached-1.4.4-3.el6.i686.rpm

rpm–ivhlibevent-1.4.13-4.el6.i686.rpmmemcached-1.4.4-3.el6.i686.rpm

1.2.3操作memcached服务器

#启动

servicememcachedstart

#关闭

servicememcachedstop

开机自启动

chkconfigmemcachedon

查看开机自启动状态

chkconfigmemcachedstatus

1.2.4Window7下dos连接Memcached服务器

第一步:

开启telnet

控制面板-程序和功能打开或关闭windows

第二步:

运行—cmd进入dos命令界面

telnetip11211

设置键值对到Memcached中去

(命令)(Key)(标记)(时间)(值的长度)

setuuu004

wwww

取值出来

getuuu

2在项目中配置Memcached的客户端来连接Memcached服务器

2.1位置

2.2内容

--配置缓存-->

sockIOPool

--配置分布式缓存的连接池-->

sockIOPool

192.168.200.143:

11211

1

2.3测试

/**

*测试Memcached客户端

*@authorlx

*

*/

publicclassTestMemcachedextendsSpringJunitTest{

 

@Autowired

privateMemCachedClientmemCachedClient;

@Test

publicvoidtestMemcached()throwsException{

//存数据到Memcached

memCachedClient.set("haha","abc");

//从Memcached中取数据

Objecto=memCachedClient.get("haha");

System.out.println(o);

}

}

3手动配置Spring切面增,删,改,查,操作分布式缓存Memcached

3.1创建memcached.xml

位置

配置内容

--配置切面-->

--处理切面的对象-->

config>

--指定处理此切面的对象-->

aspectref="cacheInterceptor">

--指定用于查询的切面规则-->

aroundmethod="doAround"pointcut="execution(*cn.itcast.core.service.*.*Service.get*(..))"/>

--指定用于更改的切面规则-->

aftermethod="doAfter"pointcut="execution(*cn.itcast.core.service.*.*Service.add*(..))"/>

aftermethod="doAfter"pointcut="execution(*cn.itcast.core.service.*.*Service.update*(..))"/>

aftermethod="doAfter"pointcut="execution(*cn.itcast.core.service.*.*Service.delete*(..))"/>

aspect>

config>

3.2创建CacheInterceptor切面对象

位置

内容

准备

/**

*基于Springaop的切面对象

*处理切面有三种

*after

*before

*around

*@authorlx

*

*/

publicclassCacheInterceptor{

@Autowired

privateMemCachedClientmemCachedClient;

//缓存默认超时时间

privatestaticfinalintTIMEOUT=3600000;

生成唯一的Key策略

//生成Key策略

publicStringgetCacheKey(ProceedingJoinPointjp)throwsJsonGenerationException,JsonMappingException,IOException{

StringBuilderkey=newStringBuilder();

//cn.itcast.core.service.order.OrderServiceImpl+getOrderListWithPage+(OrderQueryorderQuery)

StringpackageName=jp.getTarget().getClass().getName();

StringmethodName=jp.getSignature().getName();

Object[]args=jp.getArgs();

//序列化

ObjectMapperom=newObjectMapper();

om.setSerializationInclusion(Inclusion.NON_NULL);

//创建字符串流

StringWriterstr=newStringWriter();

for(Objectarg:

args){

om.writeValue(str,arg);

}

key.append(packageName)

.append(".")

.append(methodName)

.append(".")

.append(str.toString());

returnkey.toString();

}

查询时切面方法

//查询时切面方法

publicObjectdoAround(ProceedingJoinPointjp)throwsThrowable{

System.out.println("进入了doAround");

//保存数据到Memcached中去

//memcached原理

//Mapmap=newHashMap();

//第一件事:

连接memcached服务器

if(memCachedClient.stats().isEmpty()){

System.out.println("memcached服务器没有开启或无法连接!

");

returnjp.proceed();

}

//第二件事:

存储时的Key值如何生成

//Key

//1:

要求不能出现重复

//2:

要有规则,在不存储的情况下,可以随时把我们存储起来的数据找出来

StringcacheKey=getCacheKey(jp);

//查看memcached服务器中有没有缓存的数据

if(null==memCachedClient.get(cacheKey)){

Objectproceed=jp.proceed();

memCachedClient.set(cacheKey,proceed,TIMEOUT);

}

returnmemCachedClient.get(cacheKey);

}

变更时切面方法

//更改时切面方法

publicvoiddoAfter(JoinPointjp){

Stringname=jp.getTarget().getClass().getName();

//cn.itcast.core.service.order.OrderServiceImpl

//add*update*delete*

//get*

//获取memcached中的所有Key

Mapkeys=MemCachedUtil.getKeySet(memCachedClient);

//Map遍历所有key

Set>entrySet=keys.entrySet();

for(Entryentry:

entrySet){

Stringkey=entry.getKey();

if(key.startsWith(name)){

memCachedClient.delete(key);

}

}

}

测试

1)测试Controller层访问Service层时,是否能进入切面对象

2)第一次查询时,通过打断点,可看到进入Service查询数据库数据,第二次查询时,通过打断点,可看到不再进入Service,而是去缓存服务器查数据

3)数据变更时,在Service方法后清除此数据库表对应的Service层所有查询时缓存下来的数据

4共享Session解决

4.1位置

4.2CacheSessionProvider提供类的内容

注入对象

/**

*采用Memcached来存储Session中的用户对象

*

*@authorlx

*

*/

publicclassCacheSessionProviderimplementsSessionProvider{

//注入MemachedClient

@Autowired

privateMemCachedClientmemCachedClient;

//设置一个Session超时时间

privateintsessionTimeout=30;//单位分钟

往Session中设置值

//往Session中设置值

publicvoidsetAttribute(HttpServletRequestrequest,Stringname,Serializablevalue){

//创建一个Map

Mapmap=newHashMap();

//装用户对象到map中

map.put(name,value);

//将Map发送到memached服务器中去

memCachedClient.set(getSessionId(request),map,sessionTimeout*60);//单位是秒

}

获取Session中的值

//获取Session中的值

@SuppressWarnings("unchecked")

publicSerializablegetAttribute(HttpServletRequestrequest,Stringname){

//从Memcached中取

Mapmap=null;

map=(Map)memCachedClient.get(getSessionId(request));

if(null!

=map){

returnmap.get(name);

}else{

returnnull;

}

}

退出登陆

//退出登陆

publicvoidlogout(HttpServletRequestrequest,HttpServletResponseresponse){

//让memcached中的数据失效

if(memCachedClient.keyExists(getSessionId(request))){

memCachedClient.delete(getSessionId(request));

}

}

获取SessionId

//获取SessionId

publicStringgetSessionId(HttpServletRequestrequest){

//JSESSIONID=2342141234asdfas

returnrequest.getRequestedSessionId();

}

4.3把CacheSessionProvider交给Spring实例类

位置

内容

--Session提供类-->

测试

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2