使用Spring Cache缓存出现的小失误
前文:今天在使用Spring Boot项目使用Cache中出现的小失误,那先将自己创建项目的过程摆出来
1.首先创建一个Spring Boot的项目(我这里使用的开发工具是Intellij IDEA),并且导入相关依赖

2.创建Bean对象
public class User implements Serializable {
private Long id; //id
private String username; //账号
private String password; //密码
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.Service
@Service
@CacheConfig(cacheNames = "c1")
public class UserService {
@Cacheable
public User getUserById(Long id){
System.out.println(id);
User user = new User();
user.setId(id);
return user;
}
}
4.测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class RediscacheApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
User userById = userService.getUserById(99L);
System.out.println("userById >>> " + userById);
User userById2 = userService.getUserById(99L);
System.out.println("userById2 >>> " + userById2);
}
}
5.配置文件中注册redis相关信息
#这里是我这里本地装有redis的linux地址
spring.redis.host=192.168.209.131
spring.redis.port=6379
spring.redis.database=0
spring.cache.cache-names=c1
6.开启redis

然后启动!

从控制台这里可以看到方法执行了,并且调用了service中的方法,出现了99
为了检测redis中是否拥有了缓存,再次执行!

。。。?
按照道理来说,这次执行会直接调用缓存里的数据,这里不应该执行service方法里面的内容(也就是不应该出现99的)
那我们看看redis里面的参数

从这里我们可以看出redis并没有任何缓存,我推断这里是没有启动缓存所引起的,跑回主方法看看
@SpringBootApplication
public class RediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(RediscacheApplication.class, args);
}
}
好的,问题出在这里,少了一个@EnableCaching注解来启动缓存,加上注解:
@EnableCaching //这里画个重点
@SpringBootApplication
public class RediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(RediscacheApplication.class, args);
}
}
那么,启动!!!

好的,这次99只出现一次,说明调用了一次service里的方法,并将数据保存到了缓存中。在第二次调用id=99的user对象时,直接从缓存中获取。测试成功!
那这次的问题就解决了,积累点点滴滴,一步一脚印,加油
使用Spring Cache缓存出现的小失误的更多相关文章
- Spring Cache缓存注解
目录 Spring Cache缓存注解 @Cacheable 键生成器 @CachePut @CacheEvict @Caching @CacheConfig Spring Cache缓存注解 本篇文 ...
- 注释驱动的 Spring cache 缓存介绍
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- [转]注释驱动的 Spring cache 缓存介绍
原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- Spring cache 缓存
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架
前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...
- Spring Cache缓存技术,Cacheable、CachePut、CacheEvict、Caching、CacheConfig注解的使用
前置知识: 在Spring Cache缓存中有两大组件CacheManager和Cache.在整个缓存中可以有多个CacheManager,他们负责管理他们里边的Cache.一个CacheManage ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- 基于Redis的Spring cache 缓存介绍
目录 Cache API及默认提供的实现 demo 依赖包安装 定义实体类.服务类和相关配置文件 Cache注解 启用Cache注解 @CachePut @CacheEvict @Cacheable ...
随机推荐
- CentOS7安装HDP集群
之前安装大数据组件都是一个一个手动安装的,最多弄一个脚本自动安装.手动安装麻烦不说,还没有可以监控集群的可视化界面,而且组件的稳定性也是个问题. 所以我们应该试一试HDP和CDH这种企业级的hadoo ...
- day2.jmeter简单压测,下载文件,Charles手机抓包准备
一.压测 压测衡量一个系统的好坏:1.tps每秒钟处理的事物数,2.qps响应时间 添加聚合报告,更改线程组,运行接口请求 **添加压力机 1.首先确保都在同一网段 2.其他电脑要先启动jmeter- ...
- Source Insight 4.0安装后首次打开报错Unable to open or create
错误提示大概如下: Unable to open or create ....我的文档/source insght4.0/xxx.sidb. 这个错误提示就是找不到这个文件,原因是应为有中文路径,那么 ...
- Postman A请求的返回值作为B请求的入参( 拢共分三步)
- TreeMap中文排序,TreeMap倒序输出排列
1.TreeMap集合倒序排列 import java.util.Comparator; /** * 比较算法的类,比较器 * @author Administrator * */ public cl ...
- CentOS安装MySQL的完整步骤
1.官方安装文档 http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 2.下载 Mysql yum包 http://dev.mysql.co ...
- centos7 lnmp环境部署
搭建版本 版本组合 php5.6+apache/2.4.6(centos7)+mysql5.7.24 因为新系统不能确认哪些指令已经搭建 所以安装前需要确认下是否拥有 检测是否已经安装过Vim rp ...
- 四、Python-元组
列表非常适合用于存储在程序运行期间可能变化的数据集.列表是可以修改的,这对处理网 站的用户列表或游戏中的角色列表至关重要. Python将不能修改的值称为不可变的,而不可变的列表被称为元组 一.元组( ...
- Sublime Text3使用Package Control 报错There Are No Packages Available For Installation
转 http://blog.csdn.net/feilong_csdn/article/details/67638660 在使用sublime时,有时候我们希望将代码复制出来后仍然是高亮显示,这样我们 ...
- 魔力Python--if __name__ == '__main__' 的理解
if __name__ == '__main__' 的理解 __name__ 是当前模块名,当模块被直接运行时模块名为 __main__ . 这句话的意思就是,当模块被直接运行时,以下代码块将被运行, ...