发表于: 2020-05-29 23:44:52
1 1679
今天完成的事情:
1.修改了前期的缓存代码
用try/catch的原因是
获取缓存代码
如果这个缓存代码没有结果
rs是int类型的 没法用if 判断空值, 判断数字大小又不符合业务
所以用try/catch,倒是能运行,但很奇怪。
后面想到还有包装类Integer这个东西
就修改成下面这样, 这样就能判断返回int类型缓存值是否为空了
@Override
public int MemCountStudent() {
Integer rs = (Integer) client.get("MemCountStudent");
if (rs!=null){
logger.info("有缓存,缓存为" + rs + "现在开始返回缓存");
return rs;
}else{
//如果缓存为空 返回缓存数据
logger.info("没有缓存,现在从数据库查询数据");
int rss = studentMapper.countStudent();
logger.info("从数据库中查询处的数据为" + rss);
try {
logger.info("现在开始把数据放入缓存");
boolean state = client.set("MemCountStudent", rss);
logger.info("添加新缓存是否成功" + state);
return rss;
}catch (Exception e){
logger.info(e.toString());
return rss;
}}
2. 安装完 Linux的 Redis
参考: https://blog.csdn.net/qq_22599199/article/details/89232767
3. 完成Spring+redis的代码整合
redis.properties 文件
#最大能够保持idel状态的对象数
redis.maxIdle=800
redis.minIdle=100
#当池内没有返回对象时,最大等待时间
redis.maxWaitMillis=3000
#最大活动对象数
redis.maxTotal=128
redis.host=127.0.0.1
redis.port=6379
#客户端超时时间
redis.timeout=0
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
#在空闲时检查有效性, 默认false
redis.testWhileIdle=true
#是否进行有效性检查
redis.testOnReturn=true
#没密码就注释掉
#redis.password=123456
spring+redis文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com"/>
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!--设置数据池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="minIdle" value="${redis.minIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<!--链接redis-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<!-- <property name="password" value="${redis.password}"></property>-->
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- redis 序列化策略 ,手动指定 key 采用String序列化策略 -->
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringSerializer"/>
<!-- 开启事务,可以通过transcational注解控制 -->
<property name="enableTransactionSupport" value="true"/>
</bean>
</beans>
applicationContext.xml 引入spring+redis文件
<!-- 引入redis.xml文件-->
<import resource="classpath:spring-redis.xml" />
实现类中的代码
@Override
public int RedisCountStudent() {
Integer rs =(Integer) redisTemplate.opsForValue().get("RedisCountStudent");
if(rs!= null){
logger.info("有缓存,缓存为" + rs + "现在开始返回缓存");
return rs;
}else{
//如果缓存为空 返回缓存数据
logger.info("没有缓存,现在从数据库查询数据");
int rss = studentMapper.countStudent();
logger.info("从数据库中查询处的数据为" + rss);
try {
logger.info("现在开始把数据放入缓存");
redisTemplate.opsForValue().set("RedisCountStudent",rss);
logger.info("放入缓存成功");
}catch (Exception e){
logger.info(e.toString());
logger.info("放入缓存失败!");
}
return rss;
}
}
List<student> student = (List<student>)redisTemplate.opsForValue().get("RedisStudent");
logger.info("查询出的缓存值为" + student);
if (student != null && student.size()>0) {
logger.info("返回缓存");
return student;
} else {
logger.info("缓存为空,现在从数据库查询数据");
List<student> students = studentMapper.allStudent();
logger.info("从数据库中查询处的数据为" + students);
try {
logger.info("把数据放入缓存");
redisTemplate.opsForValue().set("RedisStudent",students);
logger.info("添加缓存成功");
}catch (Exception e){
logger.info(e.toString());
logger.info("放入缓存失败!");
}
return students;
}
}
4. Jemter测试
服务器开启缓存/nginx
本地jmeter连接服务器ip和端口
1. 不开启缓存测试
50线程 循环1
2.开启缓存
(1)开启Memcached
20 10
访问人数少 带缓存的90%line比不带缓存低
50 1
带缓存的jsp页面很快
(2)开启Redis
20 10
带缓存的快一些
50 1

50 运行50S
换个测试方式 带缓存JSP的更快 不带缓存的JSP开始出现异常了
3. 开启nginx负载均衡 关闭缓存
20 10
50 1

4.开启nginx 开启缓存
开启memcached
20 10
50 1
开启redis缓存
20 50S
50 1
跑的越多 越能显示缓存和负载均衡的好处
不过服务器和数据库太拉跨了 顶不住更多线程和时间的运行了
不然就开始出现异常。
明天计划的事情:
完善任务 进行任务总结
评论