Redis介绍
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。相比Memcached
它支持存储的类型相对更多(字符、哈希、集合、有序集合、列表、GEO,同时Redis是线程安全的。2010年3月15日起,Redis的开发工作由VMware主持,2013年5月开始,Redis的开发由Pivotal
赞助。
Lettuce
Spring Boot 2.x对Redis的支持,最大的改变莫过于替换掉底层Jedis的依赖,替换成Luttuce。
Lettuce和Jedis都是连接Redis Server的客户端程序,Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
先添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
首先呢看下yml的配置
spring:
redis:
host: 127.0.0.1
port: 6379
password:
lettuce:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8
max-idle: 8 # 连接池中的最大空闲连接 默认 8
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
min-idle: 0 # 连接池中的最小空闲连接 默认 0
database: 0 # 指定存储的数据是哪个库
默认情况下的模板只能支持StringRedisTemplate<String,String>,只能存字符串。这时需要自定义模板,当自定义模板后又想存储String字符串时,可以使用RedisTemplate的方式,他们俩并不冲突。在RedisCacheAutoConfiguration中自定义了一个RedisTemplate。
package com.maoxs.conf;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis缓存配置
*
* @author FuLin
*/
onfiguration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
然后进行测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate<String, Serializable> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void setTest() {
redisTemplate.opsForValue().set("one", "1");
stringRedisTemplate.opsForValue().set("two", "2");
redisTemplate.opsForValue().set("user:1", new User(1L, "fulin", "123456789"));
ExecutorService executorService = Executors.newFixedThreadPool(1000);
IntStream.range(0, 1000).forEach(i -> {
executorService.execute(() -> stringRedisTemplate.opsForValue().increment("num", 1));
});
}
@Test
public void getTest() {
Object one = redisTemplate.opsForValue().get("one");
Assert.assertEquals("1", one);
String two = stringRedisTemplate.opsForValue().get("two");
Assert.assertEquals("2", two);
User user = (User) redisTemplate.opsForValue().get("user:1");
Assert.assertEquals("fulin", user.getUsername());
}
}
Jedis
如果依然想使用Jedis这里首先需要在spring-boot-starter-data-redis中排除Luttuce依赖,再引入Jedis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
将spring.redis.lettuce.pool 替换为 spring.redis.jedis.pool 即可,其他接连信息不变
spring:
redis:
host: localhost
port: 6379
password: 3096
database: 0
timeout: 5000ms # 连接超时时间
jedis:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-idle: 8 # 连接池中的最大空闲连接 默认 8
min-idle: 0 # 连接池中的最小空闲连接 默认 0
然后使用没什么差别
这里放个学习手册 RedisTemplate 学习手册
本博文是基于springboot2.x 如果有什么不对的请在下方留言。