1.pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.application配置redis
spring:
redis:
database: 0 # Redis 数据库索引(默认为 0)
host: 192.168.161.3 # Redis 服务器地址
port: 6379 # Redis 服务器连接端口
password: 123456 # Redis 服务器连接密码(默认为空)
lettuce:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-idle: 8 # 连接池中的最大空闲连接 默认 8
min-idle: 0 # 连接池中的最小空闲连接 默认 0
3.数据对象实现Serializable接口
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PictureVO implements Serializable {
private static final long serialVersionUID = -8985545025228238754L;
private Long id;
private String author;
private String type;
}
4.解决key-value乱码的配置
@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;
}
}
5.测试使用redisTemplate
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisConfigTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/*
@Autowired
private RedisTemplate redisTemplate;
*/
@Resource(name = "redisTemplate")
private ValueOperations<String,Object> valueOperations;
@Resource(name = "redisTemplate")
private HashOperations<String, String, Object> hashOperations;
@Resource(name = "redisTemplate")
private ListOperations<String, Object> listOperations;
@Resource(name = "redisTemplate")
private SetOperations<String, Object> setOperations;
@Resource(name = "redisTemplate")
private ZSetOperations<String, Object> zSetOperations;
@Test
public void testValueObj() throws Exception{
PictureVO pi = new PictureVO(Long.valueOf(10),"byrant","a");
//ValueOperations<String,Object> operations = redisTemplate.opsForValue();
valueOperations.set("picture:1",pi); //暂时没定时,默认永久
PictureVO getBack = (PictureVO)valueOperations.get("picture:1");
System.out.println(getBack);
}
@Test
public void testSetOperation() throws Exception{
PictureVO p = new PictureVO(Long.valueOf(11),"boke","b");
PictureVO p2 = new PictureVO(Long.valueOf(12),"curry","a");
setOperations.add("picture_set",p,p2);
Set<Object> result = setOperations.members("picture_set");
System.out.println(result);
}
@Test
public void HashOperations() throws Exception{
PictureVO p = new PictureVO(Long.valueOf(13),"boke","b");
hashOperations.put("hash:picture","id",p.getId());
hashOperations.put("hash:picture","author",p.getAuthor());
hashOperations.put("hash:picture","type",p.getType());
System.out.println(hashOperations.get("hash:picture","id"));
}
@Test
public void ListOperations() throws Exception{
listOperations.leftPush("list:picture",new PictureVO(Long.valueOf(14),"boke","byrant"));
listOperations.leftPush("list:picture",new PictureVO(Long.valueOf(15),"Jordan","Mikel"));
listOperations.leftPush("list:picture",new PictureVO(Long.valueOf(16),"curry","stephen"));
System.out.println(listOperations.leftPop("list:picture"));
}
}
如图testValueObj