前文:今天在使用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. SQL FOR JSON PATH 返回 json

    --直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...

  2. spark2.1源码分析2:从SparkPi分析一个job的执行

    从SparkPi的一个行动操作入手,选择Run–Debug SparkPi进入调试: F8:Step Over F7:Step Into 右键Run to Cursor Ctrl+B 查看定义 导航– ...

  3. DOM事件类

    1.DOM中的事件级别 DOM0: element.onclick = function(){} DOM1: 没有与事件相关的设计 DOM2: element.addEventListener('cl ...

  4. orcal -对表的操作

    设计表 varchar2(n) number(n,m)整数:n-m,小数m DATE 日期 CLOB 大文本 BLOB二进制 创建表=============== create table membe ...

  5. python 编码规范 (http://www.runoob.com/w3cnote/google-python-styleguide.html)

    命名约定 所谓"内部(Internal)"表示仅模块内可用, 或者, 在类内是保护或私有的. 用单下划线(_)开头表示模块变量或函数是protected的(使用import * f ...

  6. python大法好——Python SMTP发送邮件

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  7. mysql 函数获取子节点

    DELIMITER $$ USE `topsale`$$ DROP FUNCTION IF EXISTS `getShopIdByUserId`$$ CREATE DEFINER=`root`@`%` ...

  8. cobbler登录web界面时出现“Internal Server Error”

    当进行cobbler配置后,并进行web登录时,出现错误: 先查看其日志位置 #cat /etc/httpd/conf.d/ssl.conf 在其中位置发现其错误的日志位置为/etc/httpd/lo ...

  9. Mac+sublime text+skim

    首先:安装Mac Tex .Sublime.skim MacTex:   https://tug.org/mactex/mactex-download.html Sublime3 :  http:// ...

  10. F查询和Q查询,事务及其他

    F查询和Q查询 F查询 在上面所有的例子中,我们构造的过滤器都只是将字段值与某个我们自己设定的常量做比较.如果我们要对两个字段的值做比较,那该怎么做呢? Django 提供 F() 来做这样的比较.F ...