springboot 整合GuavaCache缓存
Guava Cache是一种本地缓存机制,之所以叫本地缓存,是因为它不会把缓存数据放到外部文件或者其他服务器上,而是存放到了应用内存中。
Guava Cache的优点是:简单、强大、轻量级。
GuavaCache适用场景:
1.某些接口或者键值会被查询多次以上;
2.愿意使用或牺牲一些内存空间来提升访问或者计算速度;
3.缓存内容或者结果值较小,不会超过内存总容量;
GuavaCache中基于注解的声明式缓存操作
@Cacheable 触发缓存逻辑
Spring 在执行 @Cacheable 标注的方法前先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,执行该方法并将方法返回值放进缓存。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件
@CacheEvict
触发缓存逐出逻辑
方法执行成功后会从缓存中移除相应数据。
参数: value缓存名、 key缓存键值、 condition满足缓存条件、 unless否决缓存条件、 allEntries是否移除所有数据 (设置为true时会移除所有缓存)
@CachePut
和 @Cacheable 类似,但会把方法的返回值放入缓存中, 主要用于数据新增和修改方法。
pom.xml配置文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>1.5.9.RELEASE</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
GuavaCacheConfig:
/**
* Copyright (c) 2020, All Rights Reserved.
*
*/ package com.demo.server.config; import java.util.concurrent.TimeUnit; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.google.common.cache.CacheBuilder; @Configuration
@EnableCaching
public class GuavaCacheConfig { @Bean
public CacheManager cacheManager() {
GuavaCacheManager cacheManager = new GuavaCacheManager();
cacheManager.setCacheBuilder(
CacheBuilder.newBuilder().
expireAfterWrite(10, TimeUnit.SECONDS). //存活时间10秒
maximumSize(1000)); //最大线程数
return cacheManager;
} }
CacheController:
/**
* Copyright (c) 2020, All Rights Reserved.
*
*/ package com.demo.server.guava; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping(value = "/guava")
public class CacheController { @Autowired
private CacheService cacheService; /**
* 查询方法
*/
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getByCache() {
Long startTime = System.currentTimeMillis();
long timestamp = this.cacheService.getByCache();
Long endTime = System.currentTimeMillis();
System.out.println("耗时: " + (endTime - startTime));
return timestamp+"";
} /**
* 保存方法
*/
@RequestMapping(value = "/save", method = RequestMethod.POST)
public void save() {
this.cacheService.save();
} /**
* 删除方法
*/
@RequestMapping(value = "delete", method = RequestMethod.DELETE)
public void delete() {
this.cacheService.delete();
}
}
CacheService:
/**
* Copyright (c) 2020, All Rights Reserved.
*
*/ package com.demo.server.guava; import java.sql.Timestamp; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; @Service
public class CacheService { @CachePut(value = "guavacache")
public long save() {
long timestamp = new Timestamp(System.currentTimeMillis()).getTime();
System.out.println("进行缓存:" + timestamp);
return timestamp;
} @CacheEvict(value = "guavacache")
public void delete() {
System.out.println("删除缓存");
} @Cacheable(value = "guavacache")
public long getByCache() {
try {
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Timestamp(System.currentTimeMillis()).getTime();
} }
Application:
package com.demo; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application {
private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.setWebEnvironment(true);
app.run(args);
LOG.info("**************** Startup Success ****************");
}
}
springboot 整合GuavaCache缓存的更多相关文章
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- springboot整合redis缓存
使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...
- springboot整合redis缓存一些知识点
前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...
- SpringBoot整合guava缓存
1.pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- SpringBoot整合redis缓存(一)
准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...
- springboot 整合ehcache缓存
1.CacheManager Spring Boot默认集成CacheManager,如下包所示: 可以看出springboot自动配置了 JcacheCacheConfiguration. EhCa ...
- springBoot整合ecache缓存
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...
- 转载-Springboot整合ehcache缓存
转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...
- Springboot整合Ehcache缓存
Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...
随机推荐
- 【C语言】两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单,a说他不和x比,c说他不和x,z比,编写程序找出三对赛手名单。
问题分析:假设a是A的对手,b是B的对手,c是C的对手,a,b,c分别是x,y,z之一,且a,b,c互不相等,同时还要满足条件a!= 'x'&&c != 'x'&&c ...
- 机器学习作业(一)线性回归——Python(numpy)实现
题目太长啦!文档下载[传送门] 第1题 简述:设计一个5*5的单位矩阵. import numpy as np A = np.eye(5) print(A) 运行结果: 第2题 简述:实现单变量线性回 ...
- 一步步教你如何在ubuntu虚拟机中安装QEMU并模拟arm 开发环境(二)rootfs制作
过了,一天,周又到了,博主终于可以拿出时间来把上一次没有给大家展示完了的内容今天在这里一并展示给大家,希望和大家共同进步,共同学习,同时我也虔诚的希望各位业界的朋友把自己的工作经验拿出来大家一起分享, ...
- 你所不知道的Hello World[C++实现]
要说OIer界内最简单的程序,那恐怕非Hello World莫属了, 那么这篇文章就介绍如何写Hello World(被打). 最简单的一种实现: #include <iostream> ...
- SpringBoot整合WEB开发--(五)自定义错误页
目的与原理: 处理异常时,若我们想根据实际情况返回不同的页面,@ControllerAdvice与@ExceptionHandler,一般用于处理应用级别的异常,一些容器级别的错误就处理不了,例如Fi ...
- Ubuntu切换默认sh为bash或者dash
Ubuntu切换默认sh为bash或者dash 1 bash与dash 从Ubuntu 6.10开始,默认使用dash(theDebian Almquist Shell)而不是bash(the G ...
- 杭电1087 Super Jumping! Jumping! Jumping!(初见DP)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- oracle数据库操作 接口封装
1.如下 using Oracle.ManagedDataAccess.Client; using System; using System.Collections.Generic; using Sy ...
- 解决问题:SVN重命名后,不允许提交
第一步:选中要修改的文件,右键,修改文件名称,具体操作如下图所示 第二步:提交,提交时,不要提交修改的文件,在上一层文件夹上提交
- Windows下编译Google.Protobuf在Qt(C++)中使用与Unity3d(C#)交互
1.首先从Github-Protobuf下载代码,本文下载的版本号是3.1.0. 2.仔细查看各个README,有相关的资源下载和编译说明. 3.在一个方便的地方创建一个Install类型的文件夹,放 ...