redis是单线程的,所以redis的操作是原子性的,这样可以保证不会出现并发问题。
redis基于内存,速度非常快,据测试,redis读的速度是110000次/s,写的速度是81000次/s
本节介绍SpringBoot引入redis,以及使用RedisTemplate来操作redis数据。
采用SpringBoot 2.1.9.RELEASE,对应示例代码在:https://github.com/laolunsi/spring-boot-examples/tree/master/07-spring-boot-redis-demo
一、A Simple Demo-使用SpringBoot连接redismaven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>yml:
server: port: 8867 spring: redis: host: localhost port: 6379 #password: '' database: 6测试类:
@SpringBootTest @RunWith(SpringRunner.class) public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void testRedis() { String key = "hello"; redisTemplate.opsForValue().set("hello", "你好"); String res = (String) redisTemplate.opsForValue().get(key); System.out.println(res); } }执行结果:
看一下redis:
这里存在一个问题:默认的存储方式导致key在redis-manager里面显示出来是乱码的,并且存储结果是二进制了。这样不利用我们查看redis里面的数据。
我们需要自定义redis存储的序列化规则。
二、解决RedisTemplate默认序列化的问题完善一下maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>定义RedisConfig类:
/** * redis配置 * 主要是配置Redis的序列化规则,替换默认的jdkSerializer * key的序列化规则用StringRedisSerializer * value的序列化规则用Jackson2JsonRedisSerializer */ @Configuration public class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); // 使用Jackson2JsonRedisSerialize替换默认序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置key和value的序列化规则 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }删除之前的key,重新执行一下test方法:
下面来演示一下SpringBoot使用RedisTemplate进行redis数据的操作
三、基于SpringBoot的redis操作——key/list/hashRedisTemplate内置redis操作如下:
这里主要展示value/hash/list三种用法:
3.1 RedisTemplate.opsForValue @Test public void testKeyOps() { // 测试redis操作key-value形式 Set<String> keySet = new HashSet<>(); String key1 = "name"; keySet.add(key1); // 存储简单的key-value,并设置过期时间 redisTemplate.opsForValue().set(key1, "eknown", 1, TimeUnit.MINUTES); String key2 = "token:user1"; String key3 = "token:user2"; keySet.add(key2); keySet.add(key3); // redisTemplate.opsForValue().set(key2, "{\"name\":\"eknown\"}, \"role\":\"admin\""); redisTemplate.opsForValue().set(key3, "{\"name\":\"test\"}, \"role\":\"test\""); // 根据key的集合获取多个value List<String> valueList = redisTemplate.opsForValue().multiGet(keySet); for (String value : valueList) { System.out.println(value); } }执行结果: