600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > SpringBoot整合Redis - @Cacheable 和 RedisTemplate

SpringBoot整合Redis - @Cacheable 和 RedisTemplate

时间:2021-04-15 22:38:57

相关推荐

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>>() {@Overridepublic int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {// TODO Auto-generated method stubreturn 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);//取前8int 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。