增加是否缓存数据库的开关

This commit is contained in:
zhaojun
2023-03-05 15:24:27 +08:00
parent 300e58e92c
commit dcadffa265
2 changed files with 10 additions and 2 deletions

View File

@@ -1,8 +1,11 @@
package im.zhaojun.zfile.core.config;
import cn.hutool.core.util.BooleanUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.cache.transaction.TransactionAwareCacheManagerProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -15,13 +18,16 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class SpringCacheConfig {
@Value("${zfile.dbCache.enable:true}")
private Boolean dbCacheEnable;
/**
* 使用 TransactionAwareCacheManagerProxy 装饰 ConcurrentMapCacheManager使其支持事务 (将 put、evict、clear 操作延迟到事务成功提交再执行.
*/
@Bean
public CacheManager cacheManager() {
return new TransactionAwareCacheManagerProxy(new ConcurrentMapCacheManager());
return BooleanUtil.isFalse(dbCacheEnable) ? new NoOpCacheManager() : new TransactionAwareCacheManagerProxy(new ConcurrentMapCacheManager());
}
}

View File

@@ -7,6 +7,8 @@ zfile.db.path=${user.home}/.zfile-v4/db/zfile
zfile.preview.text.maxFileSizeKb=512
zfile.dbCache.enable=true
server.port=8080
# -------------- database config start --------------