diff --git a/pom.xml b/pom.xml index 526202e..02a212c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.irrigation common-lib - 1.2.2 + 1.2.3 UTF-8 @@ -17,7 +17,7 @@ 1.11 20.0 1.7.24 - 1.2.46 + 1.2.49 4.3.14.RELEASE 4.2.4.RELEASE 2.6.11 diff --git a/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java b/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java index de36ec4..cd80740 100644 --- a/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java +++ b/src/main/java/com/irrigation/icl/cache/config/CacheConfig.java @@ -1,13 +1,11 @@ package com.irrigation.icl.cache.config; -import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** @@ -17,27 +15,22 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class CacheConfig { + @ConditionalOnMissingBean(RedisTemplate.class) @Bean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); - - //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 -// Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); - - GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(); - -// ObjectMapper mapper = new ObjectMapper(); -// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); -// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); -// serializer.setObjectMapper(mapper); - - template.setValueSerializer(serializer); +// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); +// ObjectMapper om = new ObjectMapper(); +// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); +// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); +// jackson2JsonRedisSerializer.setObjectMapper(om); +// + template.setValueSerializer(new JdkSerializationRedisSerializer()); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; } - } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java index 71360cd..3917077 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java @@ -6,21 +6,18 @@ import net.sf.ehcache.CacheException; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; -import net.sf.ehcache.config.*; +import net.sf.ehcache.config.CacheConfiguration; +import net.sf.ehcache.config.Configuration; +import net.sf.ehcache.config.DiskStoreConfiguration; +import net.sf.ehcache.config.PersistenceConfiguration; import net.sf.ehcache.store.MemoryStoreEvictionPolicy; -import net.sf.ehcache.store.compound.ReadWriteCopyStrategy; import org.springframework.cache.Cache; -import org.springframework.cache.ehcache.EhCacheCacheManager; -import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.data.redis.cache.RedisCache; import org.springframework.data.redis.core.RedisOperations; import org.springframework.util.StopWatch; -import java.io.Serializable; -import java.sql.Time; import java.text.SimpleDateFormat; -import java.time.LocalDateTime; import java.util.Date; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; @@ -33,11 +30,10 @@ import java.util.concurrent.FutureTask; */ @Slf4j public class LayeringCache extends RedisCache { + private final ConcurrentHashMap> layeringL1CacheContainer = new ConcurrentHashMap<>(); private LayeringCacheProperties layeringCacheProperties; private LayeringCacheManager layeringCacheManager; private CacheManager layeringL1CacheManager; - private final ConcurrentHashMap> layeringL1CacheContainer = new ConcurrentHashMap<>(); - private boolean layerL1Enable; private String ID; // private String HOST; @@ -63,17 +59,17 @@ public class LayeringCache extends RedisCache { ehCache = getL1Cache(super.getName()); Element value = ehCache.get(key); if (value != null) { - log.info("Hit Cache L1 (ehcache) :{}={}", key, value); + log.debug("Hit Cache L1 (ehcache) :{}={}", key, value); return new SimpleValueWrapper(value.getObjectValue()); } } Cache.ValueWrapper wrapper = super.get(key); if (layerL1Enable && ehCache != null && wrapper != null) { ehCache.put(new Element(key, wrapper.get())); - log.info("Hit Cache L2 (redis) :{}={}", key, wrapper); + log.debug("Hit Cache L2 (redis) :{}={}", key, wrapper); } stopWatch.stop(); - log.info("get use {} ms", stopWatch.getTotalTimeMillis()); + log.debug("get use {} ms", stopWatch.getTotalTimeMillis()); return wrapper; } @@ -90,7 +86,7 @@ public class LayeringCache extends RedisCache { } super.put(key, value); stopWatch.stop(); - log.info("put use {} ms", stopWatch.getTotalTimeMillis()); + log.debug("put use {} ms", stopWatch.getTotalTimeMillis()); } @Override @@ -131,11 +127,11 @@ public class LayeringCache extends RedisCache { switch (command.getOper()) { case Command.OPT_DEL: getL1Cache(command.getName()).remove(command.getKey()); - log.info("Remove Cache L1 {} - {}", command.getName(), command.getKey()); + log.debug("Remove Cache L1 {} - {}", command.getName(), command.getKey()); break; case Command.OPT_REM: getL1Cache(command.getName()).removeAll(); - log.info("Clear Cache L1 {}", command.getName()); + log.debug("Clear Cache L1 {}", command.getName()); break; case Command.OPT_SET: Cache.ValueWrapper wrapper = super.get(command.getKey()); @@ -143,7 +139,7 @@ public class LayeringCache extends RedisCache { Ehcache ehCache = getL1Cache(super.getName()); ehCache.put(new Element(command.getKey(), wrapper.get(), false, layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds(), layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds())); - log.info("Update Cache L1 {} - {}", command.getKey(), wrapper.get()); + log.debug("Update Cache L1 {} - {}", command.getKey(), wrapper.get()); } break; default: