美文网首页
Springboot 集成Redis 并判断缓存是否存在以及设置

Springboot 集成Redis 并判断缓存是否存在以及设置

作者: Buckler | 来源:发表于2020-07-24 17:27 被阅读0次

Springboot 集成Redis 并判断缓存是否存在以及设置过期时间

    @GetMapping("/api/findbyid")
    public Object findById(int id) {
        String cacheKey = String.valueOf(id);
        //判断缓存是否存在 如果存在则从缓存直接读取 不存在则查库操作
        if (redisTemplate.hasKey(cacheKey)) {
            System.out.println("cache");
        } else {
            System.out.println("sql");
            //redisTemplate.opsForValue().set([CacheKey], [Data], [CacheTimeout], [TimeUnitType]);
            redisTemplate.opsForValue().set(cacheKey, iUserInfo.selectByPrimaryKey(id), 1, TimeUnit.SECONDS);
        }
        return redisTemplate.opsForValue().get(cacheKey);
    }

相关文章

网友评论

      本文标题:Springboot 集成Redis 并判断缓存是否存在以及设置

      本文链接:https://www.haomeiwen.com/subject/ybkslktx.html