SpringBoot整合guava缓存
1.pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency>
2.yaml配置文件
#spring配置
spring:
application:
name: cardmember
datasource:
cache:
type: guava
cache-names: merchantDetail,selConfig #缓存名字
guava:
spec: maximumSize=500,expireAfterWrite=5m
springboot支持的缓存类型
public enum CacheType { GENERIC, JCACHE, EHCACHE, HAZELCAST, INFINISPAN, COUCHBASE, REDIS, CAFFEINE, /** @deprecated */ @Deprecated GUAVA, SIMPLE, NONE; private CacheType() { } }
3.service调用
@Cacheable(value = "merchantDetail") public Map checkMerchantInfo(String merchantID) { Map<String, String> map = merchantMapper.findAppCardMerchantById(merchantID); logger.info("------商户详情从数据库中获取-----:{}", map); return map; }
最基础运用,不需要其他多余配置。