mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
优化二级缓存
This commit is contained in:
@@ -18,6 +18,7 @@ import com.google.common.base.Splitter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.redisson.spring.cache.RedissonCache;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.data.redis.cache.RedisCache;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -125,6 +127,20 @@ public class CacheCtlService extends BaseService<CacheCtlEntity, CacheCtlReposit
|
||||
return dto;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}else if (cache instanceof RedissonCache rc){
|
||||
|
||||
|
||||
return rc.getNativeCache().keySet().stream()
|
||||
.map(
|
||||
x -> {
|
||||
CacheCtlDto dto = new CacheCtlDto(request.getId());
|
||||
dto.setId(request.getId());
|
||||
dto.setCacheKey(x.toString());
|
||||
return dto;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
}else {
|
||||
throw new BizException("不支持的缓存类型");
|
||||
|
||||
@@ -135,9 +151,16 @@ public class CacheCtlService extends BaseService<CacheCtlEntity, CacheCtlReposit
|
||||
@SneakyThrows
|
||||
public CacheCtlDto value(CacheCtlDto request) {
|
||||
|
||||
StopWatch stopWatch = new StopWatch("getCacheValue " + request.getCacheName() + " " + request.getCacheKey());
|
||||
|
||||
stopWatch.start("get");
|
||||
Cache cache = cacheManager.getCache(request.getCacheName());
|
||||
Cache.ValueWrapper valueWrapper = cache.get(request.getCacheKey());
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
stopWatch.start("toJson");
|
||||
|
||||
if (valueWrapper == null){
|
||||
|
||||
request.setCacheValue("null");
|
||||
@@ -145,7 +168,9 @@ public class CacheCtlService extends BaseService<CacheCtlEntity, CacheCtlReposit
|
||||
request.setCacheValue(objectMapper.writeValueAsString(valueWrapper.get()));
|
||||
}
|
||||
|
||||
stopWatch.stop();
|
||||
|
||||
log.info("{}", stopWatch.prettyPrint());
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -45,7 +44,6 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional
|
||||
@CacheConfig(cacheManager = "caffeineCacheManager")
|
||||
public class PermissionService extends BaseService<PermissionEntity, PermissionRepository> {
|
||||
|
||||
@Autowired PermissionRepository repository;
|
||||
|
||||
@@ -34,7 +34,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -55,7 +54,6 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional
|
||||
@CacheConfig(cacheManager = "caffeineCacheManager")
|
||||
public class ResourceService extends BaseService<ResourceEntity, ResourceRepository> {
|
||||
|
||||
@Autowired ResourceRepository repository;
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.benmanes.caffeine.cache.CaffeineSpec;
|
||||
|
||||
import org.apache.commons.collections.Factory;
|
||||
import org.apache.commons.collections.map.DefaultedMap;
|
||||
import org.redisson.api.LocalCachedMapOptions;
|
||||
import org.redisson.api.RMap;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.spring.cache.RedissonSpringCacheManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
@@ -26,34 +23,32 @@ public class CacheConfig {
|
||||
@Autowired RedissonClient redissonClient;
|
||||
|
||||
@Bean
|
||||
@Qualifier("redisCacheManager" )
|
||||
public RedissonSpringCacheManager redisCacheManager() {
|
||||
@Qualifier("redissonCacheManager")
|
||||
public RedissonSpringCacheManager redissonCacheManager() {
|
||||
|
||||
Map<String, org.redisson.spring.cache.CacheConfig> cacheConfigs = new HashMap<>();
|
||||
return new RedissonSpringCacheManager(
|
||||
redissonClient,
|
||||
DefaultedMap.decorate(
|
||||
cacheConfigs,
|
||||
new Factory() {
|
||||
@Override
|
||||
public Object create() {
|
||||
return new org.redisson.spring.cache.CacheConfig(
|
||||
TimeUnit.MINUTES.toMillis(120),
|
||||
TimeUnit.MINUTES.toMillis(120));
|
||||
}
|
||||
}));
|
||||
return new RedissonSpringCacheManager(redissonClient, cacheConfigs) {
|
||||
|
||||
@Override
|
||||
protected RMap<Object, Object> getMap(
|
||||
String name, org.redisson.spring.cache.CacheConfig config) {
|
||||
|
||||
return redissonClient.getLocalCachedMap(
|
||||
name,
|
||||
LocalCachedMapOptions.defaults()
|
||||
.storeMode(LocalCachedMapOptions.StoreMode.LOCALCACHE_REDIS)
|
||||
.cacheProvider(LocalCachedMapOptions.CacheProvider.CAFFEINE)
|
||||
.syncStrategy(LocalCachedMapOptions.SyncStrategy.INVALIDATE));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("caffeineCacheManager")
|
||||
public CaffeineCacheManager caffeineCacheManager() {
|
||||
CaffeineCacheManager manager = new CaffeineCacheManager();
|
||||
manager.setCaffeineSpec(CaffeineSpec.parse("expireAfterWrite=60m"));
|
||||
return manager;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// @Bean
|
||||
// @Qualifier("caffeineCacheManager")
|
||||
// public CaffeineCacheManager caffeineCacheManager() {
|
||||
// CaffeineCacheManager manager = new CaffeineCacheManager();
|
||||
// manager.setCaffeineSpec(CaffeineSpec.parse("expireAfterWrite=60m"));
|
||||
// return manager;
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -15,16 +15,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
//@Component
|
||||
//@Primary
|
||||
@Slf4j
|
||||
public class MultiLevelCacheManager implements CacheManager {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user