edis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。

前面说了,为了解决乱码问题,我们设置了序列化的工具,但是示例中仅仅是操作字符串,如果是设置对象,会是怎么样呢?
为了使用方便,我们引入lombok包
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency>
编写一个Bean
package com.example.springboot.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Blog {
private int id;
private String name;
private boolean isDel;
private Date birthday;
}RedisTemplate的配置类如下
package com.example.springboot.config;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
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);
//重点在这四行代码
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}测试类如下
package com.example.springboot;
import com.example.springboot.model.Blog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Date;
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void strTest(){
redisTemplate.opsForValue().set("name:10001", "Java小强");
System.out.println(redisTemplate.opsForValue().get("name:10001"));
}
@Test
public void objTest(){
Blog blog = Blog.builder().id(1).name("Java小强").isDel(false).birthday(new Date()).build();
redisTemplate.opsForValue().set("blog:10001", blog);
System.out.println((Blog)redisTemplate.opsForValue().get("blog:10001"));
}
}因为我们是知道存储的数值就是Blog这个类,所以我们强制转就可以,然后我们通过RDM看下数据

可以看到,除了我们的原始数据,数据携带了值类型的一些标记。这种用法使得我们使用时变得方便,比如上面我们可以直接强制类型转换得到想要的对象。
但是对于存储来说,这都是冗余数据,在海量数据时,这会是一个严重的问题。
我们可以把对象手动转为JSON,然后把JSON手动转为对象,这样我们操作的键值都是字符串即可,这里就使用到Spring提供的另外一个工具类StringRedisTemplate。
首先我们注释掉原来的RedisConfig配置类。
然后重新编写存储和读取代码
package com.example.springboot;
import com.example.springboot.model.Blog;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Date;
@SpringBootTest
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper objectMapper = new ObjectMapper();
@Test
public void strTest(){
stringRedisTemplate.opsForValue().set("name:10002", "Java小强");
System.out.println(stringRedisTemplate.opsForValue().get("name:10002"));
}
@Test
public void objTest() throws JsonProcessingException {
Blog blog = Blog.builder().id(1).name("Java小强").isDel(false).birthday(new Date()).build();
stringRedisTemplate.opsForValue().set("blog:10002", objectMapper.writeValueAsString(blog));
String blogJson = stringRedisTemplate.opsForValue().get("blog:10002");
System.out.println(blogJson);
blog = objectMapper.readValue(blogJson, Blog.class);
System.out.println(blog.getBirthday().toLocaleString());
}
}然后通过DRM看下数据,就变得非常简洁了

这里需要说下的是,我们编程,只能根据实际情况采取一个相对适中的方案,没有绝对完美的方案。
要么是存储上,要么CPU抗,但无论如何在一个项目中,都要指定一个统一的规范。
EDN