New file |
| | |
| | | package com.dy.sso.config; |
| | | |
| | | |
| | | import com.dy.common.util.NumUtil; |
| | | import com.github.benmanes.caffeine.cache.Caffeine; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.cache.CacheManager; |
| | | import org.springframework.cache.caffeine.CaffeineCacheManager; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Configuration |
| | | public class CaffeineCacheConfiguration { |
| | | private static final int cacheInitialCapacityDefault = 10 ; |
| | | private static final int cacheMaximumSizeDefault = 10000 ; |
| | | private static final int cacheDurationDefault = 720 ; |
| | | |
| | | @Value("${pipIrr.sso.cacheMaximumSize}") |
| | | private String cacheMaximumSize ; |
| | | |
| | | @Value("${pipIrr.sso.cacheDuration}") |
| | | private String cacheDuration ; |
| | | /* |
| | | initialCapacity=[integer]: 初始的缓存空间大小 |
| | | maximumSize=[long]: 缓存的最大条数 |
| | | maximumWeight=[long]: 缓存的最大权重 |
| | | expireAfterAccess=[duration]: 最后一次写入或访问后经过固定时间过期 |
| | | expireAfterWrite=[duration]: 最后一次写入后经过固定时间过期 |
| | | refreshAfterWrite=[duration]: 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存 |
| | | weakKeys: 打开key的弱引用 |
| | | weakValues:打开value的弱引用 |
| | | softValues:打开value的软引用 |
| | | recordStats:开发统计功能 |
| | | 注意: |
| | | expireAfterWrite和expireAfterAccess同时存在时,以expireAfterWrite为准。 |
| | | maximumSize和maximumWeight不可以同时使用 |
| | | weakValues和softValues不可以同时使用weakValues和softValues不可以同时使用 |
| | | */ |
| | | @Bean |
| | | public CacheManager cacheManager() { |
| | | int cacheMaximumSizeInt; |
| | | int cacheDurationInt; |
| | | if(NumUtil.isPlusIntNumber(cacheMaximumSize)){ |
| | | cacheMaximumSizeInt = Integer.parseInt(cacheMaximumSize) ; |
| | | }else{ |
| | | cacheMaximumSizeInt = cacheMaximumSizeDefault ; |
| | | } |
| | | if(NumUtil.isPlusIntNumber(cacheDuration)){ |
| | | cacheDurationInt = Integer.parseInt(cacheDuration) ; |
| | | }else{ |
| | | cacheDurationInt = cacheDurationDefault ; |
| | | } |
| | | CaffeineCacheManager cacheManager = new CaffeineCacheManager(); |
| | | cacheManager.setCaffeine(Caffeine.newBuilder() |
| | | .initialCapacity(cacheInitialCapacityDefault) |
| | | .maximumSize(cacheMaximumSizeInt) |
| | | .expireAfterAccess(cacheDurationInt, TimeUnit.MINUTES)); |
| | | |
| | | return cacheManager; |
| | | } |
| | | } |