redis基本操作,基于StringRedisTemplate,存储,取值,设置超时时间,获取超时时间

时间:2022-06-29 22:12:21

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @GetMapping("/test")
    void test() {
        stringRedisTemplate.opsForValue().set("liuqi", "13221050705", 
        15, TimeUnit.MINUTES);

        String str1 = stringRedisTemplate.opsForValue().get("liuqi");
        long time1 = stringRedisTemplate.getExpire("liuqi");
        long time2 = stringRedisTemplate.getExpire("67");
        String str2 = stringRedisTemplate.opsForValue().get("67");


        stringRedisTemplate.opsForValue().set("test", "100", 60 * 10, 
        TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
        String s1 = stringRedisTemplate.opsForValue().get("test");

        long n1 =      
       stringRedisTemplate.boundValueOps("test").increment(-1); //val做-1操作
        String s2 = stringRedisTemplate.boundValueOps("test").get();
        long n2 = 
        stringRedisTemplate.boundValueOps("test").decrement(1);
        String s3 = stringRedisTemplate.boundValueOps("test").get();
        String s4 = 
        stringRedisTemplate.boundValueOps("test").getAndSet("670");


        String s5 = stringRedisTemplate.opsForValue().get("test");//据key获取缓存中的val


        long n3 = 
        stringRedisTemplate.boundValueOps("test").increment(1);//val 
         1

        long n4 = stringRedisTemplate.getExpire("test");//根据key获取过期时间
        boolean f6 = stringRedisTemplate.expire("test", 10, 
        TimeUnit.MINUTES);

        long n5 = stringRedisTemplate.getExpire("test", 
        TimeUnit.SECONDS);//根据key获取过期时间并换算成指定单位

        boolean f = stringRedisTemplate.delete("test");//根据key删除缓boolean f2 = stringRedisTemplate.hasKey("546545");//检查key是否存在,返回boolean值

        long n6 = stringRedisTemplate.opsForSet().add("123", "1", 
        "2", "3");//向指定key中存放set集合

        boolean f3 = stringRedisTemplate.expire("123", 1000, 
        TimeUnit.MILLISECONDS);//设置过期时间

        boolean f4 = 
        stringRedisTemplate.opsForSet().isMember("123", "1");//根据key查看集合中是否存在指定数据

        Set<String> setStr = 
        stringRedisTemplate.opsForSet().members("123");//根据key获取set集合


    }