一、关于Spring Cache

缓存在现在的应用中越来越重要,

Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术,并支持使用JCache(JSR-107)注解简化我们开发。

通过SpringCache,可以快速嵌入自己的Cache实现,主要是@Cacheable、@CachePut、@CacheEvict、@CacheConfig、@Caching等注解来实现。

  • @Cacheable:作用于方法上,用于对于方法返回结果进行缓存,如果已经存在该缓存,则直接从缓存中获取,缓存的key可以从入参中指定,缓存的value为方法返回值。
  • @CachePut:作用于方法上,无论是否存在该缓存,每次都会重新添加缓存,缓存的key可以从入参中指定,缓存的value为方法返回值,常用作于更新。
  • @CacheEvict:作用于方法上,用于清除缓存。
  • @CacheConfig:作用在类上,统一配置本类的缓存注解的属性。
  • @Caching:作用于方法上,用于一次性设置多个缓存。
  • @EnableCaching:作用于类上,用于开启注解功能。

二、演示示例

欲使用Spring Cache,需要先引入Spring Cache的依赖。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cache依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

然后在启动类上,我们需要使用@EnableCaching来声明开启缓存。

@EnableCaching //开启缓存
@SpringBootApplication
public class SpringbootApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
} }

这样就可以使用注解来操作缓存了,创建CacheService类,其中dataMap的Map存储数据,省去了数据库的操作。

@Slf4j
@Service
public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
{
for (int i = 1; i < 100 ; i++) {
User u = new User("code" + i, "name" + i);
put(i, u);
}
}
}; // 获取数据
@Cacheable(value = "cache", key = "'user:' + #id")
public User get(int id){
log.info("通过id{}查询获取", id);
return dataMap.get(id);
} // 更新数据
@CachePut(value = "cache", key = "'user:' + #id")
public User set(int id, User u){
log.info("更新id{}数据", id);
dataMap.put(id, u);
return u;
} //删除数据
@CacheEvict(value = "cache", key = "'user:' + #id")
public User del(int id){
log.info("删除id{}数据", id);
dataMap.remove(id);
return u;
} }

get方法模拟查询,@Cacheable用于添加缓存,set方法用于修改,@CachePut更新缓存,del方法用于删除数据, @CacheEvict删除缓存。需要注意的是,注解的value表示缓存分类,并不是指缓存的对象值。

然后在创建CacheApi,用于调用CacheService进行测试。

@RestController
@RequestMapping("cache")
public class CacheApi { @Autowired
private CacheService cacheService; @GetMapping("get")
public User get(@RequestParam int id){
return cacheService.get(id);
} @PostMapping("set")
public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
User u = new User(code, name);
return cacheService.set(id, u);
} @DeleteMapping("del")
public void del(@RequestParam int id){
cacheService.del(id);
} }

然后我们打开swagger-ui界面(http://localhost:10900/swagger-ui.html)进行测试,多次调用查询,可以看到, CacheService的get方法,对于同一id仅仅执行一遍。然后再调用更新,再次get时,即可发现数据已经更新,而调用del,则可以清除缓存,再次查询又会调用方法。

源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

SpringBoot系列:Spring Boot集成Spring Cache的更多相关文章

  1. SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  2. SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

    前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...

  3. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  4. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

  5. Spring Boot 集成spring security4

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  6. Spring Boot 集成 Spring Security 实现权限认证模块

    作者:王帅@CodeSheep   写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...

  7. Spring boot 集成Spring Security

    依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  8. Spring Boot 集成 Spring Security

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. Spring Boot 集成 Spring Security 入门案例教程

    前言 本文作为入门级的DEMO,完全按照官网实例演示: 项目目录结构 Maven 依赖 <parent> <groupId>org.springframework.boot&l ...

随机推荐

  1. C++11部分特性

    初识C++的时候,觉得会个STL就差不多了,后来发现了C++11这个东西,以及C++14,C++17QAQ,看了一下,好高深不学,emmmm真香= = 这里就只讲一下对ACM写代码有很高帮助的部分特性 ...

  2. 松软带你学开发-SQLSELECTDISTINCT语句

    SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法 ...

  3. java动态代理之CGLIB实现

    动态代理(CGlib 与连接池的案例) Cglib代理: 针对类来实现代理,对指定目标 产生一个子类 通过方法拦截技术拦截所有父类方法的调用. 我们要使用cglib代理必须引入 cglib的jar包 ...

  4. Linux 笔记 - 第十三章 Linux 系统日常管理之(一)系统状态监控

    博客地址:http://www.moonxy.com 一.前言 如果你是一名 Linux 运维人员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.系统运行状态主要包括:系统负载.内存状态 ...

  5. 在Typora中使用Markdown

    在Typora中使用Markdown 记:准备正式开始在博客园写博客,故学习Markdown语法,为写博客做好准备.以前也在CSDN写过一些,但广告太多,个人更喜欢博客园的简洁. 1. 标题 (#)标 ...

  6. Tomcat9 安装与配置

    一.下载 到http://tomcat.apache.org/下载绿色解压包 二.启动 1.解压后打开tomcat/bin目录下的startup.bat即可启动 打开后发现出现乱码 解决方法: 打开t ...

  7. 机器学习常用性能度量中的Accuracy、Precision、Recall、ROC、F score等都是些什么东西?

    一篇文章就搞懂啦,这个必须收藏! 我们以图片分类来举例,当然换成文本.语音等也是一样的. Positive 正样本.比如你要识别一组图片是不是猫,那么你预测某张图片是猫,这张图片就被预测成了正样本. ...

  8. 字符串的格式化、运算符和math函数(python中)

    一.字符串的格式化 1.字符串格式化输出 print('%s的年龄是%d' % ('小哥哥',20)) # 将每个值放在⼀个圆括号内,逗号隔开 '{0}的年龄是{1}'.format('⼩小哥哥',2 ...

  9. [Python]字典的简单用法

    Python中的字典与现实中字典类似,从字典中可以找到“鱼”字:鱼类是体被骨鳞.以鳃呼吸.通过尾部和躯干部的摆动以及鳍的协调作用游泳和凭上下颌摄食的变温水生脊椎动物.类比到Python的字典中,把“鱼 ...

  10. springboot 2.1.3.RELEASE版本解析.properties文件配置

    1.有时为了管理一些特定的配置文件,会考虑单独放在一个配置文件中,如redis.properties: #Matser的ip地址 redis.host=192.168.5.234 #端口号 redis ...