发表于: 2020-07-03 21:52:40
1 1758
今天完成的事情:Spring 整合 Memcached Redis
一、memcached
业务层中的逻辑
控制台输出
乱码是因为 Window 系统自带的 Telnet客户端 不支持中文
在Linuxs上Jmeter测试
无缓存
有缓存
速度确实变快了 虽然只涨了一点点
二、Redis
Windows 安装redis
Windows 下载压缩包解压后 运行
Spring 整合Redis
redis.properties
#JedisPoolConfig的参数
#最大连接数
redis.pool.maxTotal=30
#最大空闲时间
redis.pool.maxIdle=10
#每次最大连接数
redis.pool.numTestsPerEvictionRun=1024
#释放扫描的扫描间隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#连接的最小空闲时间
redis.pool.minEvictableIdleTimeMillis=1800000
#连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
redis.pool.softMinEvictableIdleTimeMillis=10000
#获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
redis.pool.maxWaitMillis=1500
#在获得链接的时候检查有效性,默认false
redis.pool.testOnBorrow=true
#在空闲时检查有效性,默认false
redis.pool.testWhileIdle=true
#连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
redis.pool.blockWhenExhausted=false
#JedisConnectionFactory的参数
#主机地址,默认:localhost
redis.hostName=127.0.0.1
#主机端口,默认:6379
redis.port=6379
#超时时间,默认:2000
redis.timeout=3000
#密码
#redis.password
#是否使用连接池,默认true
redis.usePool=true
#使用数据库的索引,0-15之间的数字,默认:0
redis.dbIndex=0
#是否使用数据类型的转换,默认:true
#redis.convertPipelineAndTxResults
#哨兵配置
#redis.sentinelConfig
#集群配置
#redis.clusterConfig
redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--引入配置文件-->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:properties/redis.properties</value>
</list>
</property>
</bean>
<!--配置 jedis 连接池-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<!-- 最大空闲时间 -->
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<!-- 每次最大连接数 -->
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
<!-- 释放扫描的扫描间隔 -->
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
<!-- 连接的最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
<!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
<!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
<!-- 在获得链接的时候检查有效性,默认false -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<!-- 在空闲时检查有效性,默认false -->
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true-->
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
</bean>
<!--jedis工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="hostName" value="${redis.hostName}"/>
<property name="port" value="${redis.port}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="database" value="${redis.dbIndex}"/>
<property name="usePool" value="${redis.usePool}"/>
<!--可以通过构造注入或者Set注入两种方式-->
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!--redisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
</beans>
工具类
@Component
public class RedisUtil {
private static final RedisTemplate redisTemplate;
static {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:redis.xml");
redisTemplate = (RedisTemplate) applicationContext.getBean("redisTemplate");
}
//添加缓存
public void set(String key,Object value){
try {
redisTemplate.opsForValue().set(key,value);
System.out.println("redis添加");
} catch (Exception e) {
e.printStackTrace();
System.out.println("添加失败");
}
}
//获取缓存
public Object get(String key){
try{
return redisTemplate.opsForValue().get(key);
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取失败");
}
return null;
}
}
写了个测试类 ,可是被我删掉了
通过Redis 解压后的文件夹中 启动
redis-server
redis-cli 查看 缓存中的数据
Linuxs上安装 redis 并启动
在业务层中写好了逻辑,但是报错了
@Override
public Student selectByPrimaryKey(Long id) {
if(redisUtil.get("key2") != null){
System.out.println("redis获取优秀学生");
}else {
redisUtil.set("key2",studentMapper.selectByPrimaryKey((long) 1));
System.out.println("redis添加学生");
}
return studentMapper.selectByPrimaryKey(id);
}
最后下载了一个可视化 缓存工具
1、连接
2、命名
3、IP地址 本地 127.0.0.1
4、端口号 默认6379
5、密码
明天计划的事情,解决报错,任务六深度思考
评论