01.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
02.编写Redis配置文件
spring.redis.host=192.168.10.102
spring.redis.port=6379
spring.redis.database= 0
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
03.Redis具体操作
3.1Redis存储字符串
@Autowired
private RedisTemplate<String,String> redisTemplate1;
@GetMapping("demo01")
private String redisdemo(){
redisTemplate1.opsForValue().set("demo01","abc");
return redisTemplate1.opsForValue().get("demo01");
}
执行后redis的存储情况
127.0.0.1:6379> get demo01
"abc"
3.2Redis存储对象
存储对象无任何序列化操作
User类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
controller操作
@Autowired
private RedisTemplate<Object, Object> redisTemplate2;
@GetMapping("demo03")
private User redisdemo03() {
redisTemplate2.opsForValue().set("userdem01",new User("mhzzj", 1314));
User u= (User) redisTemplate2.opsForValue().get("userdem01");
return u;
}
执行结果:
报错:序列化异常,由于User没有实现序列化接口
User实现序列化接口后操作
User类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{
private String name;
private int age;
}
controller操作
@Autowired
private RedisTemplate<Object, Object> redisTemplate2;
@GetMapping("demo03")
private User redisdemo03() {
redisTemplate2.opsForValue().set("userdem01",new User("mhzzj", 1314));
User u= (User) redisTemplate2.opsForValue().get("userdem01");
System.out.println(u);
return u;
}
执行结果
控制太打印
User(name=mhzzj, age=1314)
执行后redis的存储情况
127.0.0.1:6379> get userdem01
(nil)
127.0.0.1:6379> get \xac\xed\x00\x05t\x00\tuserdem01
(nil)
127.0.0.1:6379> get "\xac\xed\x00\x05t\x00\tuserdem01"
"\xac\xed\x00\x05sr\x00\x1fcom.demo.domain.User`\xd36\\L\xe6\xbc@\x02\x00\x02I\x00\x03ageL\x00\x04namet\x00\x12Ljava/lang/String;xp\x00\x00\x05\"t\x00\x05mhzzj"
原因:实现Serializable(序列化接口)后,对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。所以redis现在存储的是一个二进制流,如果不喜欢这种序列化方式,可以在redis自定义序列化的方式
自定义RedisTemplate实现自定义序列化方式
Redis的Java配置类
@Configuration
public class RedisConifg {
@Autowired
private RedisConnectionFactory connectionFactory;
@Bean
public RedisTemplate<String,Object> redisTemplate(){
RedisTemplate<String,Object> template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
User类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
controller操作
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@GetMapping("demo03")
private User redisdemo03() {
redisTemplate.opsForValue().set("userdem01",new User("mhzzj", 1314));
User u= (User) redisTemplate.opsForValue().get("userdem01");
System.out.println(u);
return u;
}
执行结果
127.0.0.1:6379> keys *
1) "userdem01"
127.0.0.1:6379> get "userdem01"
"[\"com.demo.domain.User\",{\"name\":\"mhzzj\",\"age\":1314}]"
|