com.github.rexsheng.springboot.faster.system.cache.CaffeineRedisCacheManager Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.cache;
import com.github.rexsheng.springboot.faster.cache.AbstractMultilevelCacheManager;
import com.github.rexsheng.springboot.faster.cache.MultilevelCacheProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.data.redis.cache.RedisCacheManager;
import java.util.ArrayList;
import java.util.List;
public class CaffeineRedisCacheManager extends AbstractMultilevelCacheManager {
private static final Logger logger= LoggerFactory.getLogger(CaffeineRedisCacheManager.class);
private MultilevelCacheProperties cacheProperties;
private CaffeineCacheManager caffeineCacheManager;
private RedisCacheManager redisCacheManager;
public CaffeineRedisCacheManager(MultilevelCacheProperties cacheProperties,
ObjectProvider caffeineCacheManager,
ObjectProvider redisCacheManager) {
this.cacheProperties=cacheProperties;
this.caffeineCacheManager = caffeineCacheManager.getIfAvailable();
this.redisCacheManager = redisCacheManager.getIfAvailable();
}
@Override
protected List getMultilevelCaches(String name) {
List cacheList=new ArrayList<>();
if(cacheProperties.getCaffeine()!=null && Boolean.TRUE.equals(cacheProperties.getCaffeine().getEnabled())){
Cache firstCache = caffeineCacheManager!=null?caffeineCacheManager.getCache(name):null;
if(firstCache!=null){
cacheList.add(firstCache);
logger.debug("add first level cache: {}",firstCache);
}
}
if(cacheProperties.getRedis()!=null && Boolean.TRUE.equals(cacheProperties.getRedis().getEnabled())) {
Cache secondCache = redisCacheManager != null ? redisCacheManager.getCache(name) : null;
if (secondCache != null) {
cacheList.add(secondCache);
logger.debug("add second level cache: {}",secondCache);
}
}
logger.debug("configured multi level caches: {}",cacheList);
return cacheList;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy