Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持为每一个 caffeine 设置过期时间 #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Data;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
* @author lengleng
Expand All @@ -15,20 +17,35 @@
public class CaffeineConfigProp {

/**
* 访问后过期时间
* 默认访问后过期时间
*/
private Duration expireAfterAccess;

/**
* 写入后过期时间
* 每个cacheName的过访问后过期时间,优先级比expireAfterAccess高
*/
private Map<String, Duration> expireAfterAccesses = new HashMap<>();

/**
* 默认写入后过期时间
*/
private Duration expireAfterWrite;

/**
* 写入后刷新时间
* 每个cacheName的写入后过期时间,优先级比expireAfterWrite高
*/
private Map<String, Duration> expireAfterWrites = new HashMap<>();

/**
* 默认写入后刷新时间
*/
private Duration refreshAfterWrite;

/**
* 每个cacheName的入后刷新时间,优先级比refreshAfterWrite高
*/
private Map<String, Duration> refreshAfterWrites = new HashMap<>();

/**
* 初始化大小
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.benmanes.caffeine.cache.Caffeine;
import com.pig4cloud.plugin.cache.enums.CacheOperation;
import com.pig4cloud.plugin.cache.properties.CacheConfigProperties;
import com.pig4cloud.plugin.cache.properties.CaffeineConfigProp;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -70,26 +71,30 @@ public <K, V> com.github.benmanes.caffeine.cache.Cache<K, V> getCaffeineCache(St
}

public RedisCaffeineCache createCache(String name) {
return new RedisCaffeineCache(name, stringKeyRedisTemplate, caffeineCache(), cacheConfigProperties);
return new RedisCaffeineCache(name, stringKeyRedisTemplate, caffeineCache(name), cacheConfigProperties);
}

public com.github.benmanes.caffeine.cache.Cache<Object, Object> caffeineCache() {
return caffeineCacheBuilder().build();
public com.github.benmanes.caffeine.cache.Cache<Object, Object> caffeineCache(String name) {
return caffeineCacheBuilder(name).build();
}

public Caffeine<Object, Object> caffeineCacheBuilder() {
public Caffeine<Object, Object> caffeineCacheBuilder(String name) {
Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
doIfPresent(cacheConfigProperties.getCaffeine().getExpireAfterAccess(), cacheBuilder::expireAfterAccess);
doIfPresent(cacheConfigProperties.getCaffeine().getExpireAfterWrite(), cacheBuilder::expireAfterWrite);
doIfPresent(cacheConfigProperties.getCaffeine().getRefreshAfterWrite(), cacheBuilder::refreshAfterWrite);
if (cacheConfigProperties.getCaffeine().getInitialCapacity() > 0) {
cacheBuilder.initialCapacity(cacheConfigProperties.getCaffeine().getInitialCapacity());
CaffeineConfigProp caffeineProp = cacheConfigProperties.getCaffeine();
doIfPresent(caffeineProp.getExpireAfterAccesses().getOrDefault(name, caffeineProp.getExpireAfterAccess()),
cacheBuilder::expireAfterAccess);
doIfPresent(caffeineProp.getExpireAfterWrites().getOrDefault(name, caffeineProp.getExpireAfterWrite()),
cacheBuilder::expireAfterWrite);
doIfPresent(caffeineProp.getRefreshAfterWrites().getOrDefault(name, caffeineProp.getRefreshAfterWrite()),
cacheBuilder::refreshAfterWrite);
if (caffeineProp.getInitialCapacity() > 0) {
cacheBuilder.initialCapacity(caffeineProp.getInitialCapacity());
}
if (cacheConfigProperties.getCaffeine().getMaximumSize() > 0) {
cacheBuilder.maximumSize(cacheConfigProperties.getCaffeine().getMaximumSize());
if (caffeineProp.getMaximumSize() > 0) {
cacheBuilder.maximumSize(caffeineProp.getMaximumSize());
}
if (cacheConfigProperties.getCaffeine().getKeyStrength() != null) {
switch (cacheConfigProperties.getCaffeine().getKeyStrength()) {
if (caffeineProp.getKeyStrength() != null) {
switch (caffeineProp.getKeyStrength()) {
case WEAK:
cacheBuilder.weakKeys();
break;
Expand All @@ -98,8 +103,8 @@ public Caffeine<Object, Object> caffeineCacheBuilder() {
default:
}
}
if (cacheConfigProperties.getCaffeine().getValueStrength() != null) {
switch (cacheConfigProperties.getCaffeine().getValueStrength()) {
if (caffeineProp.getValueStrength() != null) {
switch (caffeineProp.getValueStrength()) {
case WEAK:
cacheBuilder.weakValues();
break;
Expand Down