Guava Cache本地缓存在 Spring Boot应用中的实践

news/2024/7/5 2:46:25

ZenBook S13 97%屏占比


概述

在如今高并发的互联网应用中,缓存的地位举足轻重,对提升程序性能帮助不小。而 3.x开始的 Spring也引入了对 Cache的支持,那对于如今发展得如火如荼的 Spring Boot来说自然也是支持缓存特性的。当然 Spring Boot默认使用的是 SimpleCacheConfiguration,即使用 ConcurrentMapCacheManager 来实现的缓存。但本文将讲述如何将 Guava Cache缓存应用到 Spring Boot应用中。

Guava Cache是一个全内存的本地缓存实现,而且提供了线程安全机制,所以特别适合于代码中已经预料到某些值会被多次调用的场景

下文就上手来摸一摸它,结合对数据库的操作,我们让 Guava Cache作为本地缓存来看一下效果!

注: 本文首发于 My Personal Blog:CodeSheep·程序羊,欢迎光临 小站


准备工作

  • 准备好数据库和数据表并插入相应实验数据(MySQL)

比如我这里准备了一张用户表,包含几条记录:

准备好MySQL数据库和数据表

我们将通过模拟数据库的存取操作来看看 Guava Cache缓存加入后的效果。


搭建工程:Springboot + MyBatis + MySQL + Guava Cache

pom.xml 中添加如下依赖:

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--for mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!--for Mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- Spring boot Cache--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!--for guava cache--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>27.0.1-jre</version></dependency></dependencies>

建立 Guava Cache配置类

引入 Guava Cache的配置文件 GuavaCacheConfig

@Configuration
@EnableCaching
public class GuavaCacheConfig {@Beanpublic CacheManager cacheManager() {GuavaCacheManager cacheManager = new GuavaCacheManager();cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).maximumSize(1000));return cacheManager;}
}

Guava Cache配置十分简洁,比如上面的代码配置缓存存活时间为 10 秒,缓存最大数目为 1000 个


配置 application.properties

server.port=82# Mysql 数据源配置
spring.datasource.url=jdbc:mysql://121.116.23.145:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=xxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver# mybatis配置
mybatis.type-aliases-package=cn.codesheep.springbt_guava_cache.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

编写数据库操作和 Guava Cache缓存的业务代码

  • 编写 entity
public class User {private Long userId;private String userName;private Integer userAge;public Long getUserId() {return userId;}public void setUserId(Long userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Integer getUserAge() {return userAge;}public void setUserAge(Integer userAge) {this.userAge = userAge;}
}
  • 编写 mapper
public interface UserMapper {List<User> getUsers();int addUser(User user);List<User> getUsersByName( String userName );
}
  • 编写 service
@Service
public class UserService {@Autowiredprivate UserMapper userMapper;public List<User> getUsers() {return userMapper.getUsers();}public int addUser( User user ) {return userMapper.addUser(user);}@Cacheable(value = "user", key = "#userName")public List<User> getUsersByName( String userName ) {List<User> users = userMapper.getUsersByName( userName );System.out.println( "从数据库读取,而非读取缓存!" );return users;}
}

看得很明白了,我们在 getUsersByName接口上添加了注解:@Cacheable。这是 缓存的使用注解之一,除此之外常用的还有 @CachePut@CacheEvit,分别简单介绍一下:

  1. @Cacheable:配置在 getUsersByName方法上表示其返回值将被加入缓存。同时在查询时,会先从缓存中获取,若不存在才再发起对数据库的访问
  2. @CachePut:配置于方法上时,能够根据参数定义条件来进行缓存,其与 @Cacheable不同的是使用 @CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中,所以主要用于数据新增和修改操作上
  3. @CacheEvict:配置于方法上时,表示从缓存中移除相应数据。
  • 编写 controller
@RestController
public class UserController {@Autowiredprivate UserService userService;@AutowiredCacheManager cacheManager;@RequestMapping( value = "/getusersbyname", method = RequestMethod.POST)public List<User> geUsersByName( @RequestBody User user ) {System.out.println( "-------------------------------------------" );System.out.println("call /getusersbyname");System.out.println(cacheManager.toString());List<User> users = userService.getUsersByName( user.getUserName() );return users;}}

改造 Spring Boot应用主类

主要是在启动类上通过 @EnableCaching注解来显式地开启缓存功能

@SpringBootApplication
@MapperScan("cn.codesheep.springbt_guava_cache")
@EnableCaching
public class SpringbtGuavaCacheApplication {public static void main(String[] args) {SpringApplication.run(SpringbtGuavaCacheApplication.class, args);}
}

最终完工的整个工程的结构如下:

完整工程结构


实际实验

通过多次向接口 localhost:82/getusersbyname POST数据来观察效果:

向接口提交数据

可以看到缓存的启用和失效时的效果如下所示(上文 Guava Cache的配置文件中设置了缓存 user的实效时间为 10s):

缓存的启用和失效时的取数据效果

怎么样,缓存的作用还是很明显的吧!


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!

  • My Personal Blog:CodeSheep 程序羊
  • 程序羊的 2018年终总(gen)结(feng)



http://lihuaxi.xjx100.cn/news/236402.html

相关文章

php include include_once 区别,「PHP」include()、include_once()、require()、require_once()的用法及区别...

1、include&#xff1a;使用include引用外部文件时&#xff0c;只有代码执行到include代码段时&#xff0c;调用的外部文件才会被引用并读取&#xff0c;当引用的文件发生错误时&#xff0c;系统只会给出个警告错误&#xff0c;而整个php文件会继续执行。使用require语句来调用…

面试题收集最新

Java高级程序员面试题------https://www.cnblogs.com/mengdou/p/7233398.html Java高级工程师面试题总结及参考答案-----https://www.cnblogs.com/java1024/p/8594784.html Java高级程序员&#xff08;5年左右&#xff09;面试的题目集----https://blog.csdn.net/fangqun663775…

Clojure程序设计

《Clojure程序设计》基本信息作者&#xff1a; (美)Stuart Halloway Aaron Bedra [作译者介绍]出版社&#xff1a;人民邮电出版社ISBN&#xff1a;9787115308474上架时间&#xff1a;2013-3-1出版日期&#xff1a;2013 年3月开本&#xff1a;16开页码&#xff1a;230版次&#…

ubuntu系统php环境变量设置,Ubuntu系统环境变量详解

使用Ubuntu 进行开发绕不开的就是环境变量的配置&#xff0c;由于Linux系统严格的权限管理&#xff0c;造成Ubuntu系统有多个环境变量配置文件&#xff0c;如果不了解其调用顺序&#xff0c;很有可能遇到配置了环境变量&#xff0c;而没有其作用的问题。本文将介绍Ubuntu Linux…

Linux下如何查看版本信息

2019独角兽企业重金招聘Python工程师标准>>> Linux下如何查看版本信息&#xff0c; 包括位数、版本信息以及CPU内核信息、CPU具体型号等等&#xff0c;整个CPU信息一目了然。  1、# uname &#xff0d;a &#xff08;Linux查看版本当前操作系统内核信息&#xff09…

迭代法移动盘子java,算法学习--日记(牛顿迭代法)

牛顿迭代法概念理解牛顿迭代法可以使用函数极限角度无限迭代趋近于某一点理解。迭代算法解决问题&#xff0c;需要做好3个方面的工作&#xff1a;确定迭代变量 在可以用迭代算法解决的问题中&#xff0c;至少存在一个可直接或间接地不断由旧值递推出新值的变量&#xff0c;这个…

医院六级电子病历建设思路及要点

产生背景 在医院电子病历信息化发展的过程中&#xff0c;先后经历了纸质病历、电子病历、结构化电子病历以及具有全医疗过程管理能力的电子病历四个阶段。临床业务需求质量的逐步提升&#xff0c;标准规范的逐步细化&#xff0c;互联网战略的落地实施&#xff0c;无疑对目前电子…

Ubuntu16.04安装qt

5.11官方下载网站&#xff1a; http://download.qt.io/official_releases/qt/5.11/5.11.1/ 可以直接下载linux系统下的.run安装包&#xff1a; 安装方式&#xff1a;https://www.jb51.net/LINUXjishu/501994.html 切换到.run所在的目录&#xff0c;然后 第一步&#xff1a; chm…