http://blog.csdn.net/hy245120020/article/details/78065676

************************************************************

spring boot guava cache 缓存学习

  1. 自定义key
  2. 自定义全局key过期时间,缓存个数
  3. 针对单个key自定义过期时间,缓存个数

引入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.google.guava</groupId>
  7. <artifactId>guava</artifactId>
  8. <version>19.0</version>
  9. </dependency>

自定义key

  1. @Override
  2. @Cacheable(value = "user", key = "'user'.concat(#id.toString())")
  3. public User findUserById(Long id) {
  4. log.info("findUserById query from db, id: {}", id);
  5. return userMap.get(id);
  6. }
  7. @Override
  8. @CachePut(value = "user", key = "'user'.concat(#user.id.toString())")
  9. public void update(User user) {
  10. log.info("update db, user: {}", user.toString());
  11. userMap.put(user.getId(), user);
  12. }
  13.  
  14. @Override
  15. @CacheEvict(value = "user", key = "'user'.concat(#id.toString())")
  16. public void remove(Long id) {
  17. log.info("remove from db, id: {}", id);
  18. userMap.remove(id);
  19. }

自定义全局key过期时间,缓存个数

  1. package com.km.config;
  2.  
  3. import com.google.common.cache.CacheBuilder;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.annotation.EnableCaching;
  6. import org.springframework.cache.guava.GuavaCache;
  7. import org.springframework.cache.support.SimpleCacheManager;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. /**
  15. * <p>guava缓存配置</p>
  16. * Created by zhezhiyong@163.com on 2017/9/22.
  17. */
  18. @Configuration
  19. @EnableCaching
  20. public class GuavaConfig {
  21.  
  22. /**
  23. * 配置全局缓存参数,3600秒过期,最大个数1000
  24. */
  25. @Bean
  26. public CacheManager cacheManager() {
  27. GuavaCacheManager cacheManager = new GuavaCacheManager();
  28. cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));
  29. return cacheManager;
  30. }
  31.  
  32. }

针对单个key自定义过期时间,缓存个数

  1. package com.km.config;
  2.  
  3. import com.google.common.cache.CacheBuilder;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.cache.annotation.EnableCaching;
  6. import org.springframework.cache.guava.GuavaCache;
  7. import org.springframework.cache.support.SimpleCacheManager;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. /**
  15. * <p>guava缓存配置</p>
  16. * Created by zhezhiyong@163.com on 2017/9/22.
  17. */
  18. @Configuration
  19. @EnableCaching
  20. public class GuavaConfig {
  21.  
  22. private static final int DEFAULT_MAXSIZE = 1000;
  23. private static final int DEFAULT_TTL = 3600;
  24.  
  25. /**
  26. * 定义cache名称、超时时长秒、最大个数
  27. * 每个cache缺省3600秒过期,最大个数1000
  28. */
  29. public enum Caches {
  30. user(60, 2),
  31. info(5),
  32. role;
  33.  
  34. Caches() {
  35. }
  36.  
  37. Caches(int ttl) {
  38. this.ttl = ttl;
  39. }
  40.  
  41. Caches(int ttl, int maxSize) {
  42. this.ttl = ttl;
  43. this.maxSize = maxSize;
  44. }
  45.  
  46. private int maxSize = DEFAULT_MAXSIZE; //最大數量
  47. private int ttl = DEFAULT_TTL; //过期时间(秒)
  48.  
  49. public int getMaxSize() {
  50. return maxSize;
  51. }
  52.  
  53. public void setMaxSize(int maxSize) {
  54. this.maxSize = maxSize;
  55. }
  56.  
  57. public int getTtl() {
  58. return ttl;
  59. }
  60.  
  61. public void setTtl(int ttl) {
  62. this.ttl = ttl;
  63. }
  64. }
  65.  
  66. /**
  67. * 个性化配置缓存
  68. */
  69. @Bean
  70. public CacheManager cacheManager() {
  71. SimpleCacheManager manager = new SimpleCacheManager();
  72. //把各个cache注册到cacheManager中,GuavaCache实现了org.springframework.cache.Cache接口
  73. ArrayList<GuavaCache> caches = new ArrayList<>();
  74. for (Caches c : Caches.values()) {
  75. caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build()));
  76. }
  77. manager.setCaches(caches);
  78. return manager;
  79. }
  80. }

配置yml

  1. server:
  2. port: 8080
  3. spring:
  4. cache:
  5. type: guava

配置启动

  1. package com.km;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cache.annotation.EnableCaching;
  6.  
  7. @SpringBootApplication
  8. @EnableCaching
  9. public class SpringBootGuavaCacheApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(SpringBootGuavaCacheApplication.class, args);
  13. }
  14. }

spring boot guava cache 缓存学习的更多相关文章

  1. Spring Boot 入门之缓存和 NoSQL 篇(四)

    原文地址:Spring Boot 入门之缓存和 NoSQL 篇(四) 博客地址:http://www.extlight.com 一.前言 当系统的访问量增大时,相应的数据库的性能就逐渐下降.但是,大多 ...

  2. Spring Boot中使用缓存

    Spring Boot中使用缓存 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一. 原始的使 ...

  3. Spring Boot中的缓存支持(一)注解配置与EhCache使用

    Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...

  4. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  5. Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

    Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...

  6. google guava cache缓存基本使用讲解

    代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...

  7. Spring Boot 结合 Redis 缓存

    Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...

  8. Spring Boot 使用Redis缓存

    本文示例源码,请看这里 Spring Cache的官方文档,请看这里 缓存存储 Spring 提供了很多缓存管理器,例如: SimpleCacheManager EhCacheCacheManager ...

  9. 20191127 Spring Boot官方文档学习(4.12)

    4.12.缓存(Caching) Spring框架提供了对应用程序透明添加缓存的支持.从本质上讲,抽象将缓存应用于方法,从而根据缓存中可用的信息减少执行次数.缓存逻辑是透明应用的,不会对调用者造成任何 ...

随机推荐

  1. 详解iOS多图下载的缓存机制

    1. 需求点是什么? 这里所说的多图下载,就是要在tableview的每一个cell里显示一张图片,而且这些图片都需要从网上下载. 2. 容易遇到的问题 如果不知道或不使用异步操作和缓存机制,那么写出 ...

  2. 【LeetCode】227. Basic Calculator II

    Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...

  3. webpack window 处理图片和其他静态文件

    安装url-loader npm install url-loader --save-dev 配置config文件 {        test: /\.(png|jpg)$/,        load ...

  4. 【Oracle】Oracle约束的总结

    你对ORACLE约束的了解如何?比较模糊还是相当透彻?如果你对下面几个问题了如指掌的话,恭喜你,你已经对约束掌握得比较好了,不用看这篇文章了.ORACLE的约束有啥功能作用? 有哪些类型约束(不同版本 ...

  5. centos6.4安装GitLab

    参考文章: http://www.pickysysadmin.ca/2013/03/25/how-to-install-gitlab-5-0-on-centos-6/ yum安装redis的方法: h ...

  6. @property与@synthesize的差别

    上一篇文章我有讲到self.与_的差别,往往和这个问题相伴随的是我困惑的问题是"@property与@synthesize的差别" @property的使用方法 @interfac ...

  7. winform中键盘和鼠标事件的捕捉和重写

    在编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时,希望无论当前哪个控件获得焦点,当用户按上.下.左.右键 ...

  8. VS Code 中文注释显示乱码

    将设置中的"files.autoGuessEncoding"项的值改为true即可. 1.文件 2.首选项 3.设置 4.搜索 "files.autoGuessEncod ...

  9. java和js实现电话号码部分隐藏

    有时候我们不需要将电话号码全部展现在页面上,那么我们可能要对电话号码进行相应的处理,js代码如下: alert("13456789012".replace(/(\d{3})(\d{ ...

  10. Zephir入门 —— 语法篇

    概述 Zephir的语法跟PHP很相似,所以这里不会把官网的文档照搬过来翻译一遍,而是会把一些Zephir相较于PHP比较特别的语法挑出来讲一下.如果想要要完整学习Zephir的语法,没有比官网的文档 ...