温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot怎么整合Redis使用@Cacheable和RedisTemplate

发布时间:2022-07-18 14:02:58 来源:亿速云 阅读:342 作者:iii 栏目:开发技术

这篇文章主要介绍“SpringBoot怎么整合Redis使用@Cacheable和RedisTemplate”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot怎么整合Redis使用@Cacheable和RedisTemplate”文章能帮助大家解决问题。

对之前网站做了一些很简单的优化,给用户列表加了一个分页功能。

分页就更好考虑加载速度,如果换一页就要等几秒,那体验感是非常差的。

因此想到了加一个redis缓存。

springboot整合redis有两种方式:

  • 一、使用注解,@EnableCaching @Cacheable . . . 等

  • 二、使用RedisTemplate

两者都能操作缓存,使用RedisTemplate 操作肯定是比使用注解灵活、方便。但是从理论上来讲注解方式速度应该更快,因为使用注解如果在缓存中有就直接从缓存中取,不用进入方法。而RedisTemplate 必须进入方法,而且执行写的逻辑判断。

下面记录一下我给分页做缓存的思路,肯定有很多不好的地方,希望大家可以给我指出。

业务场景是后台管理系统,不用过于注重实时数据刷新,就设置一个小时过期。

我的思路是:

 第一次加载页面,就从数据库把前面四页的数据从数据库查询出来,这样第一次稍微多等一下,后面换页几乎不用等待,这样体验比较好。然后每次换页都换查看有没有在缓存中,没用就加入缓存。

@RequestMapping("/appUser/{currentPage}")
public R<String> getTableData1(@PathVariable int currentPage) {
    //第一次请求 前面几页用到的概率更大  把后面三页存入redis 减少后面分页请求的时间  以后每次加载页面都把那页放入redis
    // 设置一个小时过期
    Page<AppUser> appUserPage = new Page<AppUser>(currentPage, 12);
    if (currentPage == 1 && !redisTemplate.hasKey(1)) {
        for (int i = 1; i < 5; i++) {
            Page<AppUser> redisPage = new Page<AppUser>(i, 12);
            redisTemplate.opsForValue().set(i, appUserServiceInterface.page(redisPage), 1, TimeUnit.HOURS);
        }
    } else if (!redisTemplate.hasKey(currentPage)) {
        redisTemplate.opsForValue().set(currentPage, appUserServiceInterface.page(appUserPage), 1, TimeUnit.HOURS);
        return R.success((Page<AppUser>) redisTemplate.opsForValue().get(currentPage));
    } else if (redisTemplate.hasKey(currentPage)) {
        return R.success((Page<AppUser>) redisTemplate.opsForValue().get(currentPage));
    }
    return R.success(appUserServiceInterface.page(appUserPage));
}

数据统计那块我又试了试注解。

先要在启动加上 @EnableCaching注解 

注解使用就简单,在方法上加上@Cacheable 就行,执行方法前会查询redis缓存是否有对应的key,有就直接取值,没有就执行方法。

value = "appUserData" 是缓存区的名字 , key是键的名字 。

以下的键值就是 appUserData : : userArea

@RequestMapping ("/userArea")
@Cacheable(value = "appUserData",key ="'userArea'")
public R<String> area() {
    List<AppUser> userList = appUserServiceInterface.list();
    List<String> areaList = new ArrayList<>();

    for (AppUser appUser : userList) {
        areaList.add(appUser.getArea());
    }
    //放入map记录每个月份出现的次数
    Map<String, Integer> areaTimes = new HashMap<>();
    for (String s : areaList) {
        if (!areaTimes.containsKey(s)) {
            areaTimes.put(s, 1);
        }else {
            areaTimes.put(s, areaTimes.get(s) + 1);
        }
    }
    //排序
    //自定义比较器
    Comparator<Map.Entry<String, Integer>> valCmp = new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            // TODO Auto-generated method stub
            return o2.getValue() - o1.getValue();  // 降序排序,如果想升序就反过来
        }
    };
    //将map转成List,map的一组key,value对应list一个存储空间
    List<Map.Entry<String, Integer>> mapList = new ArrayList<Map.Entry<String, Integer>>(areaTimes.entrySet()); //传入maps实体
    Collections.sort(mapList, valCmp);

    //取前8
    int len = mapList.size();
    for (int i = 0; i < len-8; i++) {
        mapList.remove(8);
    }

    Map<String, String> resMap = new HashMap<>();
    for (Map.Entry<String, Integer> m : mapList) {
        resMap.put(m.getKey(), m.getValue().toString());
    }
    return R.success(resMap);
}

其他注解:

  • @CachePut

  • @Caching

  • @CacheEvict

关于“SpringBoot怎么整合Redis使用@Cacheable和RedisTemplate”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI