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

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

spring boot guava cache 缓存学习

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

引入依赖

   <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>19.0</version></dependency>

自定义key

 @Override@Cacheable(value = "user", key = "'user'.concat(#id.toString())")public User findUserById(Long id) {log.info("findUserById query from db, id: {}", id);return userMap.get(id);}@Override@CachePut(value = "user", key = "'user'.concat(#user.id.toString())")public void update(User user) {log.info("update db, user: {}", user.toString());userMap.put(user.getId(), user);}@Override@CacheEvict(value = "user", key = "'user'.concat(#id.toString())")public void remove(Long id) {log.info("remove from db, id: {}", id);userMap.remove(id);}

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

package com.km.config;import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
import java.util.concurrent.TimeUnit;/*** <p>guava缓存配置</p>* Created by zhezhiyong@163.com on 2017/9/22.*/
@Configuration
@EnableCaching
public class GuavaConfig {/*** 配置全局缓存参数,3600秒过期,最大个数1000*/@Beanpublic CacheManager cacheManager() {GuavaCacheManager cacheManager = new GuavaCacheManager();cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));return cacheManager;}}

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

package com.km.config;import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.ArrayList;
import java.util.concurrent.TimeUnit;/*** <p>guava缓存配置</p>* Created by zhezhiyong@163.com on 2017/9/22.*/
@Configuration
@EnableCaching
public class GuavaConfig {private static final int DEFAULT_MAXSIZE = 1000;private static final int DEFAULT_TTL = 3600;/*** 定义cache名称、超时时长秒、最大个数* 每个cache缺省3600秒过期,最大个数1000*/public enum Caches {user(60, 2),info(5),role;Caches() {}Caches(int ttl) {this.ttl = ttl;}Caches(int ttl, int maxSize) {this.ttl = ttl;this.maxSize = maxSize;}private int maxSize = DEFAULT_MAXSIZE;    //最大數量private int ttl = DEFAULT_TTL;        //过期时间(秒)public int getMaxSize() {return maxSize;}public void setMaxSize(int maxSize) {this.maxSize = maxSize;}public int getTtl() {return ttl;}public void setTtl(int ttl) {this.ttl = ttl;}}/*** 个性化配置缓存*/@Beanpublic CacheManager cacheManager() {SimpleCacheManager manager = new SimpleCacheManager();//把各个cache注册到cacheManager中,GuavaCache实现了org.springframework.cache.Cache接口ArrayList<GuavaCache> caches = new ArrayList<>();for (Caches c : Caches.values()) {caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build()));}manager.setCaches(caches);return manager;}
}

配置yml

server:port: 8080
spring:cache:type: guava

配置启动

package com.km;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class SpringBootGuavaCacheApplication {public static void main(String[] args) {SpringApplication.run(SpringBootGuavaCacheApplication.class, args);}
}

转载于:https://www.cnblogs.com/zhao1949/p/8087049.html

spring boot guava cache 缓存学习相关推荐

  1. Spring Boot基础学习笔记18:Spring Boot整合Redis缓存实现

    文章目录 零.学习目标 一.Spring Boot支持的缓存组件 二.基于注解的Redis缓存实现 (一)安装与启动Redis (二)创建Spring Boot项目 - RedisCacheDemo0 ...

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

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

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

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

  4. java+cache使用方法_java相关:Spring boot redis cache的key的使用方法

    java相关:Spring boot redis cache的key的使用方法 发布于 2020-8-16| 复制链接 摘记: 在数据库查询中我们往往会使用增加缓存来提高程序的性能,@Cacheabl ...

  5. Spring Boot 官方文档学习(一)入门及使用

    Spring Boot 官方文档学习(一)入门及使用 个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问 ...

  6. Spring Boot 集成 Redis 缓存

    Spring Boot 集成 Redis 缓存 在此章,我们将 SpringBoot 集成 Redis 缓存,Redis是一个开源的,基于内存的数据结构存储,可以用作数据库.缓存和消息代理,在本章仅讲 ...

  7. Spring Boot 结合 Redis 缓存

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

  8. Spring Boot集成Redis缓存之模拟高并发场景处理

    前言 同样我们以上一篇文章为例子,搭建好环境之后,我欧美可以模拟高并发场景下,我们的缓存效率怎么样,到底能不能解决我们实际项目中的缓存问题.也就是如何解决缓存穿透? Spring Boot集成Redi ...

  9. Spring boot - 整合 Redis缓存(上)

    一.配置Pom文件 在使用spring boot 2.0整合redis时遇到了好多问题,网上很多例子都是1.x版本的.故2.0没有折腾好所以将2.0降到了1.5.降级后由于thymeleaf版本也会从 ...

最新文章

  1. Django modules模块
  2. 华为BGP的基本配置命令
  3. pytorch nn.Conv2d
  4. [USACO4.2]Drainage Ditches
  5. html图片按页码显示,html - 在打印html文档时显示页码 - 堆栈内存溢出
  6. android thread实例
  7. wifisetting.java_Wifi 笔记 | 启动流程
  8. Tensorflow实现MLP
  9. 怎么利用迭代器写入mysql_range()是什么?为什么不生产迭代器?
  10. Effective C# Item4:使用Conditional特性代替#if条件编译
  11. 银行java程序员面试题_Java程序员面试题集精选
  12. 沟通CTBS远程接入软件研究
  13. android studio无法连接小米手机问题解决
  14. Intouch2020与施耐德PLC通讯
  15. 【C数据类型】基本数据类型
  16. mac终端命令(苹果终端命令)
  17. (机械师T90外接显卡GTX-1080)Win10笔记本通过M.2接口外接独立显卡+解决错误代码43
  18. 功能强大的微信商城系统,欢迎体验
  19. 如果........
  20. css3 中dispaly:none 动画处理

热门文章

  1. Java开发中遇到具有挑战的事_170道Java工程师面试题,你敢挑战吗?
  2. C语言 Win静态库
  3. hadoop hdfs 集群配置
  4. opencv-api minAreaRect
  5. csv 逗号数量不一样_MySQL Workbeach导入CSV时的大坑,一直都是UTF-8问题,绕不过去了~。~...
  6. python r语言 数据分析_PythonR语言-将Python和R整合进一个数据分析流程
  7. vSAN其实很简单-Quickstart是一件很炫的东西
  8. vSphere 7 With K8s系列06:创建命名空间
  9. vSphere 6.7的新增功能?我应该升级吗?
  10. Kubernetes学习总结(17)—— Kubernetes 快速入门需要掌握的知识点总结