前文:今天在使用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缓存出现的小失误的更多相关文章

  1. Spring Cache缓存注解

    目录 Spring Cache缓存注解 @Cacheable 键生成器 @CachePut @CacheEvict @Caching @CacheConfig Spring Cache缓存注解 本篇文 ...

  2. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  3. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  4. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  5. Spring cache 缓存

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  6. 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架

    前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...

  7. Spring Cache缓存技术,Cacheable、CachePut、CacheEvict、Caching、CacheConfig注解的使用

    前置知识: 在Spring Cache缓存中有两大组件CacheManager和Cache.在整个缓存中可以有多个CacheManager,他们负责管理他们里边的Cache.一个CacheManage ...

  8. Spring Cache缓存框架

    一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...

  9. 基于Redis的Spring cache 缓存介绍

    目录 Cache API及默认提供的实现 demo 依赖包安装 定义实体类.服务类和相关配置文件 Cache注解 启用Cache注解 @CachePut @CacheEvict @Cacheable ...

随机推荐

  1. CentOS_6.5配置iptables防火墙策略

    ###############配置filter表防火墙############### #清除预设表filter中的所有规则链的规则iptables -F #清除预设表filter中使用者自定链中的规则 ...

  2. [WebService].net中WebService的使用实例

    .net中WebService的使用实例 一.创建一个Webwebservice 1.新建一个项目WebserverDemo 2.在项目处添加新建项,添加一个web服务 3.编辑TestServer. ...

  3. C#添加VisionPro控件问题

    安装完VisionPro8.2版本以后,用VS2013调用时,没有默认在工具箱内,那么就需要手动添加了,选择菜单栏---->工具----->选择工具箱项,如下图所示:           ...

  4. mysql------Windows7 64bit安装教程------下载mysql

    .1.进入官网的下载位置,https://dev.mysql.com/downloads/installer/: 2.选择“Windows”; 3.点击"MySQL Installer&qu ...

  5. 安装yii2 需要token 记录

    在安装yii2过程中会提示需要token,参照github help: https://help.github.com/articles/creating-a-personal-access-toke ...

  6. MyBatis注解-动态SQL 一个 SqlProvider的demo

    Provider动态语言注解 MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建 ...

  7. 学习excel的使用技巧复制一列文本成新列去重

    学习excel的使用技巧复制一列文本成新列去重 其实比较简单的技巧  知道了就会  不知道就比较麻烦 直接复制到一列 找到 数据选项 删除重复项

  8. leetcode每日刷题计划-简单篇day10

    跳题,不熟悉的留到周末再做. 保持冷静,不信画饼. num 100 相同的树 Same Tree 做法可能不是特别简洁,注意一下.最后判断完子树以后,要确定根的数值是一样的 然后在isleaf的判定先 ...

  9. 稀疏矩阵 part 2

    ▶ 各种稀疏矩阵数据结构之间的转化 ● MAT ←→ CSR CSR * MATToCSR(const MAT *in) // MAT 转 CSR { checkNULL(in); CSR * out ...

  10. java 启动报错 Unsupported major.minor version 52.

    这是java 版本问题,主要是版本不统一,你看看你开发工具用的java版本(编译的java版本和开发的java版本)是否统一,如果没有统一就会报这个错误.