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
*/
@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
@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;
}
} /**
* 个性化配置缓存
*/
@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: 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);
}
}

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. HDUOJ-----1074 Integer Inquiry

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. leetcode44:wildcard

    44. Wildcard Matching 问题描述 给定字符串s和模式p,判断字符串s是否完全符合模式p 其中字符串s只包含小写字母,模式串p包含小写字母.*.?,其中星号表示任意长度的任意字符串, ...

  3. ipsec在企业网中的应用(IKE野蛮模式)(转)

    from:http://lulu1101.blog.51cto.com/4455468/817954 ipsec在企业网中的应用(IKE野蛮模式) 案例: 本实验采用华为三台F100防火墙,和一台s3 ...

  4. 遇到影响服务器性能的cpuspeed 服务

    最近碰到一个很蛋痛的问题,,我在公司的代码上实现了一个功能,然后基于这个测试,结果比对数据发现每天少三千多万条,, 然后我各种优化,各种零碎部功能阉割,,还是丢数据! 之后,监控运行网卡----wat ...

  5. fedora国内源常见配置

    yum install yum-fastestmirror3.rpmfusion源 rpm -ivh http://download1.rpmfusion.org/free/fedora/rpmfus ...

  6. Entity Framework之深入分析

    EF虽然是一个晚生畸形的ORM框架,但功能强大又具有灵活性的,给了开发人员一定的发挥空间.因为微软出发点总是好的,让开发变得简单,但实际上不是所有的事情都这么理想.这里顺便推荐马丁大叔的书<企业 ...

  7. Error 25007.初始化合成时发生错误。安装程序无法使用 LoadLibraryShim() 加载合成。

    安装“Microsoft .NET Framework 2.exe”报错如下: c:\windows\microsoft.net\framework\...类似这种错误都因 .NET Framewor ...

  8. Linux安装ElasticSearch-2.2.0-分词器插件(IK)

    1.在gitpub上搜索elasticsearch-analysis,能够看到所有elasticsearch的分词器: 2.安装IK分词器:https://github.com/medcl/elast ...

  9. MongoDB 学习笔记(9)--- Limit与Skip方法

    MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的 ...

  10. Java之基于S2SH与手机数据交互(一)

    在前两篇博客,介绍了在eclipse上搭建SSH,可是好多小伙伴反映.看了偶写滴博客.跟着搭建还是错误百出,唉! 事实上不经历错误怎么能不见红线啊!于是我在上篇博客补充了他们的错误,还在被错误困扰的童 ...