发表于: 2018-01-15 21:32:41
1 645
今日完成
1.redis与spring的整合测试
(1)linux安装redis
昨天的apt-get版本根本不能用,重新安装了make版本的redis.参考的
http://blog.csdn.net/liuwei8nn1/article/details/56838729
(2)与spring的整合
2.使用redis编写业务逻辑代码
(1)学生列表
//在缓存中查找是否存在studentlist.
List<StudentCustom> studentlist = (List<StudentCustom>) redisTemplate.opsForHash().get("student", "list");
//如果不存在studentlist,去数据库查询数据,并写入缓存中
if (null == studentlist) {
List<StudentCustom> studentlist2 = studentMapperCustom.findStudentList(studentQueryVo);
redisTemplate.opsForHash().put("student", "list", studentlist2);
return studentlist2;
}
return studentlist;
(2)单个学生信息的查询,一样放在缓存中。
//先在缓存中查找该id的student
StudentCustom studentCustom = (StudentCustom) redisTemplate.opsForHash().get("student", id);
//如果该id的学生不在缓存中,去数据库中查询
if (null == studentCustom) {
Student student = studentMapper.selectByPrimaryKey(id);
StudentCustom studentCustom1 = new StudentCustom();
BeanUtils.copyProperties(student, studentCustom1);
redisTemplate.opsForHash().put("student", id, studentCustom1);
return studentCustom1;
}
return studentCustom;
}
(3)数据同步的问题,当删除,修改或者新增信息时,如果缓存中存在也需要修改缓存中的信息。
//判断该id在缓存中是否存在
StudentCustom FindStudent = (StudentCustom) redisTemplate.opsForHash().get("student", id);
if (null == FindStudent) {
studentMapper.updateByPrimaryKeySelective(studentCustom);
}
redisTemplate.opsForHash().delete("student", id);
//更新学生信息后需要删除缓存中的相应的学生信息。
redisTemplate.opsForHash().delete("student", "list");
studentMapper.updateByPrimaryKeySelective(studentCustom);
3.使用jmeter测试redis的性能表现。
遇到问题
1.使用hmap的时候,对象序列化报错“cant converted to java.lang.String”
分析了一下原因,原来配置了下面的spring-data-redis普通的key,value是可以序列化的。
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
其实使用hmap的时候还要配置对应的序列化属性值。
<!--存取hash的时候也要配置-->
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<!--存储Map时value需要的序列化配置-->
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
2.还是出现了无法转换类型的问题。
这里很关键,因为map的key也不是字符串,也需要序列化的。
那么配置的属性就不能实行这个StringRedisSerializer这个类了。要使用JdkSerializationRedisSerializer。
3.测试过程中老是报错。这个错误出现在压力测试之后。正常连接不出现问题。
查了好多资料,说是连接池的问题。也还没弄清楚连接池怎么配置的?
明日计划
1.提交任务6
2.任务7
收获
1.学会了配置redis.
评论