spring boot guava cache 缓存学习
来源:互联网 发布:淘宝宝贝名称规则 编辑:程序博客网 时间:2024/05/29 14:32
http://blog.csdn.net/hy245120020/article/details/78065676
*******************************************************
spring boot guava cache 缓存学习
- 自定义key
- 自定义全局key过期时间,缓存个数
- 针对单个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@EnableCachingpublic class GuavaConfig { /** * 配置全局缓存参数,3600秒过期,最大个数1000 */ @Bean public 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@EnableCachingpublic 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; } } /** * 个性化配置缓存 */ @Bean public 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: 8080spring: cache: type: guava
配置启动
package com.km;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublic class SpringBootGuavaCacheApplication { public static void main(String[] args) { SpringApplication.run(SpringBootGuavaCacheApplication.class, args); }}
阅读全文
0 0
- spring boot guava cache 缓存学习
- spring boot guava cache 缓存学习
- spring boot redis cache 缓存学习
- spring boot caffeine cache 缓存学习
- Spring Boot 数据缓存 Cache
- Guava Cache缓存
- Spring Boot 菜鸟教程 17 Cache-缓存
- Spring Boot 轻量级缓存 Ace-Cache
- Spring boot的数据缓存Cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- spring guava cache
- 深入浅出mysql-sql基础
- 数据结构实验之排序四:寻找大富翁
- 防止SQL注入:永远不要信任外界输入的数据,特别是来自于客户端的,包括选择框、表单隐藏域和 cookie
- Rhyme/Hibernate getCurrentSession()与openSession()的区别
- 《PCL点云库学习&VS2010(X64)》Part 48 基于霍夫变换的点云平面检测法
- spring boot guava cache 缓存学习
- 微信小程序商品筛选,侧方弹出动画选择页面
- js实现星星评分功能的实现,并获取评分多少的功能(附源码)
- 学习矢量量化-LVQ
- Scala中for循环逆序遍历数组
- Hcdp认证培训是什么?
- 生成对抗网络(GAN)的理论与应用完整入门介绍
- 考研,加油,祝福你们
- phantomjs爬虫在docker下不能正常运行的问题