SpringBoot整合Redis在可视化工具乱码问题,以及常用的api

 pom依赖:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/>
 </parent>

<dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
</dependencies>  

yml配置:

1 spring: 
2   redis:
3     host: 127.0.0.1

配置类:

 1 /**
 2  * Redis key乱码配置.
 3  *
 4  * @author 宫新程
 5  * @since 2019/3/3 16:17
 6  */
 7 @Configuration
 8 public class RedisConfigBean {
 9 
10   /**
11    * redis 防止key value 前缀乱码.
12    *
13    * @param factory redis连接 factory
14    * @return redisTemplate
15    */
16   @Bean(name = "redisTemplate")
17   public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
18     RedisTemplate<String, Object> template = new RedisTemplate<>();
19     template.setConnectionFactory(factory);
20     template.setKeySerializer(new StringRedisSerializer());
21     template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
22     template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
23     template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
24     template.afterPropertiesSet();
25     return template;
26   }
27 }

 

常用api:

stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
stringRedisTemplate.opsForValue().get("test") //根据key获取缓存中的val
stringRedisTemplate.boundValueOps("test").increment(-1); //val做-1操作
stringRedisTemplate.boundValueOps("test").increment(1); //val +1
stringRedisTemplate.getExpire("test") //根据key获取过期时间
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS) //根据key获取过期时间并换算成指定单位
stringRedisTemplate.delete("test"); //根据key删除缓存
stringRedisTemplate.hasKey("546545"); //检查key是否存在,返回boolean值
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS); //设置过期时间
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");  //向指定key中存放set集合
stringRedisTemplate.opsForSet().isMember("red_123", "1")  //根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().members("red_123");  //根据key获取set集合

 

转载于:https://www.cnblogs.com/gxc6/p/10466211.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值