<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
<version>2.6.0version>
dependency>
#Redis服务器地址
spring.redis.host=192.168.10.102
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0),可以不写
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
# redis账号密码,若没有账号密码可不进行设置
@Autowired
private RedisTemplate<String,String> redisTemplate1;
@GetMapping("demo01")
private String redisdemo(){
redisTemplate1.opsForValue().set("demo01","abc");
return redisTemplate1.opsForValue().get("demo01");
}
127.0.0.1:6379> get demo01
"abc"
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
@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;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{
private String name;
private int age;
}
@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");//获取刚才redis存储的对象
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自定义序列化的方式
@Configuration
public class RedisConifg {
@Autowired
private RedisConnectionFactory connectionFactory;
@Bean
public RedisTemplate<String,Object> redisTemplate(){
RedisTemplate<String,Object> template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
// 序列化后会产生java类型说明,如果不需要用“Jackson2JsonRedisSerializer”和“ObjectMapper ”配合效果更好
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;
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
}
@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}]"