一、介绍
二、使用(Springboot应用)
(1)引入Maven相关依赖(springboot对应redis依赖、连接池依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
注:默认的Java客户端依赖是lettuce
(2)编写配置文件application.properties
spring.redis.host=192.168.105.220
spring.redis.port=6379
spring.redis.password=123456
spring.redis.database=0
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-wait=1000ms
(3)运行测试
@SpringBootTest
class SpringbootRedisTestApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name", "tom");
Object name = valueOperations.get("name");
System.out.println(name);
}
}
三、Redis序列化
(1)上述key存入时是name,但redis中显示xxxxxname,是因为Spring Data Redis默认使用jdkSerialization来序列化。
注:缺点是可读性差、占用内存大。
(2)自定义序列化方式解决上述问题 引入Maven相关依赖(因为Json序列化需要用到对应的Jar包)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
创建配置类并更改默认的序列化器
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.json());
redisTemplate.setHashValueSerializer(RedisSerializer.json());
return redisTemplate;
}
}
再次运行测试类
(3)上述序列化器转换对象为JSON字符串时,会额外存储对象的全类名(带来额外的内存开销)
@SpringBootTest
class SpringbootRedisTestApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("name2", new Person());
Object tom = valueOperations.get("name2");
System.out.println(tom);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person{
private String name = "tom";
private int age = 22;
}
四、通用序列化做法
(1)为了节省内存空间,存储值时通常不用JSON序列化器,改为统一使用String序列化器,当需要存储对象时,手动进行序列化及反序列化(普通字符串转JSON格式字符串)。 (2)配置key和value均用String序列化器
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setValueSerializer(RedisSerializer.string());
redisTemplate.setHashValueSerializer(RedisSerializer.string());
return redisTemplate;
}
}
(3)运行
@SpringBootTest
class SpringbootRedisTestApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() throws JsonProcessingException {
ValueOperations valueOperations = redisTemplate.opsForValue();
Person person = new Person();
ObjectMapper objectMapper = new ObjectMapper();
valueOperations.set("name3", objectMapper.writeValueAsString(person));
String str = (String) valueOperations.get("name3");
Person tom = objectMapper.readValue(str, Person.class);
System.out.println(tom);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person{
private String name = "tom";
private int age = 22;
}
|