业务上需要把一些数据放到redis里面,但是系统逻辑代码差不多编写完成了,怎么整?用Spring Cache啊,对既有业务逻辑侵袭极小。

于是尝试调查了一下,遇到一些问题分享一下(本文使用Spring MVC,不使用Spring Boot)

1.配置:

<!--启用缓存-->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<!-- 这里可以配置多个redis -->
<bean class="com.xxx.cache.RedisCache">
<property name="redisTemplate" ref="redisTemplate" />
<property name="name" value="yyy"/>
<!-- name对应的名称要在类或方法的注解中使用 可以指定多个cache管理模式 -->
</bean>
</set>
</property>
</bean>

2.代码中使用

@Cacheable(value="yyy",key="#root.target.getCompanyID()+#xxxId")
public List<XXXInfo> getXXXData(int xxxId) {

3.遇到的坑1:Cache机制不被触发

原因:getXXXData()是个内部方法,在同一个Service1的另外一个方法中被内部调用,不是直接被Controller调用的。
解决:在同一个Service1中,自注入(Self autowiring),这货好像Spring4.3之前还不支持
@Autowired
Service1 self // 调用的地方
self.getXXXData(xxxId);

4.遇到的坑2:Key上面的SpEL表达式使用了基类的方法getCompanyID(),报错找不到方法。EL1004E: Method call: Method getCompanyID()  cannot be found on org.springframework.cache.interceptor.CacheExpressionRootObject type

解决:坑了个爹了,基类的方法是protected类型的,改成public就可以了

Spring Cache Redis结合遇到的坑的更多相关文章

  1. spring cache redis

    一.使用开源包(spring-data-redis) 1.引入jar包 <dependency>      <groupId>org.springframework.data& ...

  2. spring Cache /Redis 缓存 + Spring 的集成示例

    spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...

  3. 【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案

    前言 之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的. ...

  4. 使用Spring Cache + Redis + Jackson Serializer缓存数据库查询结果中序列化问题的解决

    应用场景 我们希望通过缓存来减少对关系型数据库的查询次数,减轻数据库压力.在执行DAO类的select***(), query***()方法时,先从Redis中查询有没有缓存数据,如果有则直接从Red ...

  5. spring整合redis使用RedisTemplate的坑Could not get a resource from the pool

    一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...

  6. spring Cache + Redis 开发数据字典以及自定义标签

    一.数据库表结构 1.  分类表:dict_type 2.  子项表:dict_entry 二.页面维护功能示意图: 1.  分类管理 点击子项管理进入子项管理页面 2.子项管理 三.数据字典添加到缓 ...

  7. Spring Cache扩展:注解失效时间+主动刷新缓存

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  8. Java缓存相关memcached、redis、guava、Spring Cache的使用

    随笔分类 - Java缓存相关 主要记录memcached.redis.guava.Spring Cache的使用 第十二章 redis-cluster搭建(redis-3.2.5) 摘要: redi ...

  9. Spring Cache For Redis

    一.概述 缓存(Caching)可以存储经常会用到的信息,这样每次需要的时候,这些信息都是立即可用的. 常用的缓存数据库: Redis   使用内存存储(in-memory)的非关系数据库,字符串.列 ...

随机推荐

  1. 出现org.springframework.beans.factory.NoSuchBeanDefinitionException 的解决思路

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: privat ...

  2. 201871010125-王玉江《面向对象程序设计(java)》第十五周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ht ...

  3. 201871010133-赵永军《面向对象程序设计(java)》第十六周学习总结

    201871010133-赵永军<面向对象程序设计(java)>第十六周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  4. 文件处理file handling

    #1. 打开文件,得到文件句柄并赋值给一个变量 #2. 通过句柄对文件进行操作 #3. 关闭文件 #1.open函数打开文件,open找的是系统的编码gbkf = open("陈粒" ...

  5. MyBatisPlus快速入门

    MyBatisPlus快速入门 官方网站 https://mp.baomidou.com/guide 慕课网视频 https://www.imooc.com/learn/1130 入门 https:/ ...

  6. ASP.NET开发实战——(五)ASP.NET MVC & 分层

    上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...

  7. ASP.NET开发实战——(四)ASP.NET MVC是如何运行的?它的生命周期是什么?

    前面的文章我们使用ASP.NET MVC创建了个博客应用,那么它是如何工作的呢?我们都知道ASP.NET的程序需要部署到IIS上才能够通过浏览器来访问,那么IIS与ASP.NET MVC程序之间又是如 ...

  8. [LeetCode] 854. K-Similar Strings 相似度为K的字符串

    Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two ...

  9. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  10. java byte[]和base64互相转换

      1.方式一 import java.io.UnsupportedEncodingException; import java.util.Base64; // byte[]转base64 Strin ...