发表于: 2017-10-10 21:01:42
1 880
今天完成的事情:
1.完成XMemcached的Spring整合配置
配置文件(写在Spring的配置文件中):
<!--XMemcached配置-->
<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg value="39.108.78.2" />
<constructor-arg value="11211" />
</bean>
</list>
</constructor-arg>
<property name="connectionPoolSize" value="50" />
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" />
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<!-- Use factory bean to build memcached client -->
<bean
id="memcachedClient"
factory-bean="memcachedClientBuilder"
factory-method="build"
destroy-method="shutdown">
</bean>
在Service中使用:
@Autowired
private XMemcachedClientBuilder memcachedClientBuilder;
@Autowired
private MemcachedClient memcachedClient;
创建类,防止反复创建mecacheClient引起错误
private MemcachedClient createClient() throws Exception{
if(memcachedClient==null){//如果spring没有创建成功,再build一次
return memcachedClient = memcachedClientBuilder.build();
}
return null;
}
测试:
@Test
public void TestXMemcached() {
try {
createClient();
memcachedClient.add("test",60,"success");
System.out.println(memcachedClient.get("test"));
} catch (Exception e) {
e.printStackTrace();
}
}
结果:
在项目中实际使用:
由于memcached不支持对象的直接储存,如果要储存,要实现序列化
在model中实现Serializable接口
序列化工具类
public class SerializeUtil {
public static byte[] serizlize(Object object){
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(baos != null){
baos.close();
}
if (oos != null) {
oos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
/*
* 反序列化
* */
public static Object deserialize(byte[] bytes){
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try{
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
}catch(Exception e){
e.printStackTrace();
}finally {
try {
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
}
在任务五的不拦截页面中使用缓存:
public List<Excellence> selectExcellenceByName() {
try {
createClient();
//从缓存中取得被序列化的List并判断是否为空
byte[] excellenceWaitDeserialize = memcachedClient.get("excellence");
if (excellenceWaitDeserialize != null) {
//若不为空,解序列化并使用这个List
List<Excellence> excellences = (List<Excellence>) SerializeUtil.deserialize(excellenceWaitDeserialize);
return excellences;
} else {
//若为空,则序列化List,将其加入到缓存中,并将缓存前的List返回
List<Excellence> excellenceWaitSerialize = excellenceDao.selectExcellenceByName();
memcachedClient.add("excellence", 3600, SerializeUtil.serizlize(excellenceWaitSerialize));
return excellenceWaitSerialize;
}
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
效果:
若是从数据库中直接取出,由于实现逻辑的原因,每个优秀学员的位置都会变化
使用了缓存之后,因为缓存是固定的,所以位置不再变化
使用测试类测试缓存是否存在:
@Test
public void insertUser() throws Exception {
createClient();
assertNotNull(memcachedClient.get("excellence"));
}
结果:
明天计划的事情:
1.完成页面的压测
2.完成负载均衡,压测
3.还有时间的话,学习一下redis
遇到的问题:
1.memcached中不能直接存放List,不然会报“对象未序列化”的错误
解决方法:从网上找了一个序列化工具
半年前学的序列化知识忘光了,得去补补
2.如何解决缓存和数据库不一致的问题
因为我这边没有写进数据的接口,所以在这里只写逻辑
a、增:db-->cache:数据库插入成功,添加到缓存
b、删:cache-->db:先删缓存,在删数据库
c、改:db-->cache:先改数据库,在改缓存
d、查:cache-->db-->cache:先查缓存,没有则查库添加到缓存
注:若有事务存在,a中只插入,不添加到缓存中
收获:
1.学会了如何使用Xmemcached
2.学会了使用序列化工具
进度:
任务6开始时间:2017.10.08
预计demo时间:2017.10.16
延期风险:无
禅道
http://task.ptteng.com/zentao/project-task-350.html
评论