Commit 465b6add8fa4258f1c20e72208254ad4fe18158d
1 parent
0858ede0
#fix:修复对PageHelper中拦截对象无法正常序列化的BUG
Showing
3 changed files
with
24 additions
and
35 deletions
pom.xml
| @@ -6,7 +6,7 @@ | @@ -6,7 +6,7 @@ | ||
| 6 | 6 | ||
| 7 | <groupId>com.irrigation</groupId> | 7 | <groupId>com.irrigation</groupId> |
| 8 | <artifactId>common-lib</artifactId> | 8 | <artifactId>common-lib</artifactId> |
| 9 | - <version>1.2.2</version> | 9 | + <version>1.2.3</version> |
| 10 | 10 | ||
| 11 | <properties> | 11 | <properties> |
| 12 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | 12 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| @@ -17,7 +17,7 @@ | @@ -17,7 +17,7 @@ | ||
| 17 | <commons-codec.version>1.11</commons-codec.version> | 17 | <commons-codec.version>1.11</commons-codec.version> |
| 18 | <guava.vsersion>20.0</guava.vsersion> | 18 | <guava.vsersion>20.0</guava.vsersion> |
| 19 | <slf4j.version>1.7.24</slf4j.version> | 19 | <slf4j.version>1.7.24</slf4j.version> |
| 20 | - <fastjson.version>1.2.46</fastjson.version> | 20 | + <fastjson.version>1.2.49</fastjson.version> |
| 21 | <spring-web.version>4.3.14.RELEASE</spring-web.version> | 21 | <spring-web.version>4.3.14.RELEASE</spring-web.version> |
| 22 | <spring-security-core.version>4.2.4.RELEASE</spring-security-core.version> | 22 | <spring-security-core.version>4.2.4.RELEASE</spring-security-core.version> |
| 23 | <ehcache.version>2.6.11</ehcache.version> | 23 | <ehcache.version>2.6.11</ehcache.version> |
src/main/java/com/irrigation/icl/cache/config/CacheConfig.java
| 1 | package com.irrigation.icl.cache.config; | 1 | package com.irrigation.icl.cache.config; |
| 2 | 2 | ||
| 3 | -import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; | ||
| 4 | -import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
| 5 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| 6 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | 3 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 7 | import org.springframework.context.annotation.Bean; | 4 | import org.springframework.context.annotation.Bean; |
| 8 | import org.springframework.context.annotation.Configuration; | 5 | import org.springframework.context.annotation.Configuration; |
| 9 | import org.springframework.data.redis.connection.RedisConnectionFactory; | 6 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 10 | import org.springframework.data.redis.core.RedisTemplate; | 7 | import org.springframework.data.redis.core.RedisTemplate; |
| 8 | +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; | ||
| 11 | import org.springframework.data.redis.serializer.StringRedisSerializer; | 9 | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 12 | 10 | ||
| 13 | /** | 11 | /** |
| @@ -17,27 +15,22 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; | @@ -17,27 +15,22 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
| 17 | @Configuration | 15 | @Configuration |
| 18 | public class CacheConfig { | 16 | public class CacheConfig { |
| 19 | 17 | ||
| 18 | + @ConditionalOnMissingBean(RedisTemplate.class) | ||
| 20 | @Bean | 19 | @Bean |
| 21 | public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { | 20 | public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { |
| 22 | RedisTemplate<Object, Object> template = new RedisTemplate<>(); | 21 | RedisTemplate<Object, Object> template = new RedisTemplate<>(); |
| 23 | template.setConnectionFactory(connectionFactory); | 22 | template.setConnectionFactory(connectionFactory); |
| 24 | - | ||
| 25 | - //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 | ||
| 26 | -// Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); | ||
| 27 | - | ||
| 28 | - GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer(); | ||
| 29 | - | ||
| 30 | -// ObjectMapper mapper = new ObjectMapper(); | ||
| 31 | -// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
| 32 | -// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
| 33 | -// serializer.setObjectMapper(mapper); | ||
| 34 | - | ||
| 35 | - template.setValueSerializer(serializer); | 23 | +// Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); |
| 24 | +// ObjectMapper om = new ObjectMapper(); | ||
| 25 | +// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | ||
| 26 | +// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | ||
| 27 | +// jackson2JsonRedisSerializer.setObjectMapper(om); | ||
| 28 | +// | ||
| 29 | + template.setValueSerializer(new JdkSerializationRedisSerializer()); | ||
| 36 | //使用StringRedisSerializer来序列化和反序列化redis的key值 | 30 | //使用StringRedisSerializer来序列化和反序列化redis的key值 |
| 37 | template.setKeySerializer(new StringRedisSerializer()); | 31 | template.setKeySerializer(new StringRedisSerializer()); |
| 38 | template.afterPropertiesSet(); | 32 | template.afterPropertiesSet(); |
| 39 | return template; | 33 | return template; |
| 40 | } | 34 | } |
| 41 | 35 | ||
| 42 | - | ||
| 43 | } | 36 | } |
src/main/java/com/irrigation/icl/cache/core/LayeringCache.java
| @@ -6,21 +6,18 @@ import net.sf.ehcache.CacheException; | @@ -6,21 +6,18 @@ import net.sf.ehcache.CacheException; | ||
| 6 | import net.sf.ehcache.CacheManager; | 6 | import net.sf.ehcache.CacheManager; |
| 7 | import net.sf.ehcache.Ehcache; | 7 | import net.sf.ehcache.Ehcache; |
| 8 | import net.sf.ehcache.Element; | 8 | import net.sf.ehcache.Element; |
| 9 | -import net.sf.ehcache.config.*; | 9 | +import net.sf.ehcache.config.CacheConfiguration; |
| 10 | +import net.sf.ehcache.config.Configuration; | ||
| 11 | +import net.sf.ehcache.config.DiskStoreConfiguration; | ||
| 12 | +import net.sf.ehcache.config.PersistenceConfiguration; | ||
| 10 | import net.sf.ehcache.store.MemoryStoreEvictionPolicy; | 13 | import net.sf.ehcache.store.MemoryStoreEvictionPolicy; |
| 11 | -import net.sf.ehcache.store.compound.ReadWriteCopyStrategy; | ||
| 12 | import org.springframework.cache.Cache; | 14 | import org.springframework.cache.Cache; |
| 13 | -import org.springframework.cache.ehcache.EhCacheCacheManager; | ||
| 14 | -import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; | ||
| 15 | import org.springframework.cache.support.SimpleValueWrapper; | 15 | import org.springframework.cache.support.SimpleValueWrapper; |
| 16 | import org.springframework.data.redis.cache.RedisCache; | 16 | import org.springframework.data.redis.cache.RedisCache; |
| 17 | import org.springframework.data.redis.core.RedisOperations; | 17 | import org.springframework.data.redis.core.RedisOperations; |
| 18 | import org.springframework.util.StopWatch; | 18 | import org.springframework.util.StopWatch; |
| 19 | 19 | ||
| 20 | -import java.io.Serializable; | ||
| 21 | -import java.sql.Time; | ||
| 22 | import java.text.SimpleDateFormat; | 20 | import java.text.SimpleDateFormat; |
| 23 | -import java.time.LocalDateTime; | ||
| 24 | import java.util.Date; | 21 | import java.util.Date; |
| 25 | import java.util.concurrent.Callable; | 22 | import java.util.concurrent.Callable; |
| 26 | import java.util.concurrent.ConcurrentHashMap; | 23 | import java.util.concurrent.ConcurrentHashMap; |
| @@ -33,11 +30,10 @@ import java.util.concurrent.FutureTask; | @@ -33,11 +30,10 @@ import java.util.concurrent.FutureTask; | ||
| 33 | */ | 30 | */ |
| 34 | @Slf4j | 31 | @Slf4j |
| 35 | public class LayeringCache extends RedisCache { | 32 | public class LayeringCache extends RedisCache { |
| 33 | + private final ConcurrentHashMap<String, Future<Ehcache>> layeringL1CacheContainer = new ConcurrentHashMap<>(); | ||
| 36 | private LayeringCacheProperties layeringCacheProperties; | 34 | private LayeringCacheProperties layeringCacheProperties; |
| 37 | private LayeringCacheManager layeringCacheManager; | 35 | private LayeringCacheManager layeringCacheManager; |
| 38 | private CacheManager layeringL1CacheManager; | 36 | private CacheManager layeringL1CacheManager; |
| 39 | - private final ConcurrentHashMap<String, Future<Ehcache>> layeringL1CacheContainer = new ConcurrentHashMap<>(); | ||
| 40 | - | ||
| 41 | private boolean layerL1Enable; | 37 | private boolean layerL1Enable; |
| 42 | private String ID; | 38 | private String ID; |
| 43 | // private String HOST; | 39 | // private String HOST; |
| @@ -63,17 +59,17 @@ public class LayeringCache extends RedisCache { | @@ -63,17 +59,17 @@ public class LayeringCache extends RedisCache { | ||
| 63 | ehCache = getL1Cache(super.getName()); | 59 | ehCache = getL1Cache(super.getName()); |
| 64 | Element value = ehCache.get(key); | 60 | Element value = ehCache.get(key); |
| 65 | if (value != null) { | 61 | if (value != null) { |
| 66 | - log.info("Hit Cache L1 (ehcache) :{}={}", key, value); | 62 | + log.debug("Hit Cache L1 (ehcache) :{}={}", key, value); |
| 67 | return new SimpleValueWrapper(value.getObjectValue()); | 63 | return new SimpleValueWrapper(value.getObjectValue()); |
| 68 | } | 64 | } |
| 69 | } | 65 | } |
| 70 | Cache.ValueWrapper wrapper = super.get(key); | 66 | Cache.ValueWrapper wrapper = super.get(key); |
| 71 | if (layerL1Enable && ehCache != null && wrapper != null) { | 67 | if (layerL1Enable && ehCache != null && wrapper != null) { |
| 72 | ehCache.put(new Element(key, wrapper.get())); | 68 | ehCache.put(new Element(key, wrapper.get())); |
| 73 | - log.info("Hit Cache L2 (redis) :{}={}", key, wrapper); | 69 | + log.debug("Hit Cache L2 (redis) :{}={}", key, wrapper); |
| 74 | } | 70 | } |
| 75 | stopWatch.stop(); | 71 | stopWatch.stop(); |
| 76 | - log.info("get use {} ms", stopWatch.getTotalTimeMillis()); | 72 | + log.debug("get use {} ms", stopWatch.getTotalTimeMillis()); |
| 77 | return wrapper; | 73 | return wrapper; |
| 78 | } | 74 | } |
| 79 | 75 | ||
| @@ -90,7 +86,7 @@ public class LayeringCache extends RedisCache { | @@ -90,7 +86,7 @@ public class LayeringCache extends RedisCache { | ||
| 90 | } | 86 | } |
| 91 | super.put(key, value); | 87 | super.put(key, value); |
| 92 | stopWatch.stop(); | 88 | stopWatch.stop(); |
| 93 | - log.info("put use {} ms", stopWatch.getTotalTimeMillis()); | 89 | + log.debug("put use {} ms", stopWatch.getTotalTimeMillis()); |
| 94 | } | 90 | } |
| 95 | 91 | ||
| 96 | @Override | 92 | @Override |
| @@ -131,11 +127,11 @@ public class LayeringCache extends RedisCache { | @@ -131,11 +127,11 @@ public class LayeringCache extends RedisCache { | ||
| 131 | switch (command.getOper()) { | 127 | switch (command.getOper()) { |
| 132 | case Command.OPT_DEL: | 128 | case Command.OPT_DEL: |
| 133 | getL1Cache(command.getName()).remove(command.getKey()); | 129 | getL1Cache(command.getName()).remove(command.getKey()); |
| 134 | - log.info("Remove Cache L1 {} - {}", command.getName(), command.getKey()); | 130 | + log.debug("Remove Cache L1 {} - {}", command.getName(), command.getKey()); |
| 135 | break; | 131 | break; |
| 136 | case Command.OPT_REM: | 132 | case Command.OPT_REM: |
| 137 | getL1Cache(command.getName()).removeAll(); | 133 | getL1Cache(command.getName()).removeAll(); |
| 138 | - log.info("Clear Cache L1 {}", command.getName()); | 134 | + log.debug("Clear Cache L1 {}", command.getName()); |
| 139 | break; | 135 | break; |
| 140 | case Command.OPT_SET: | 136 | case Command.OPT_SET: |
| 141 | Cache.ValueWrapper wrapper = super.get(command.getKey()); | 137 | Cache.ValueWrapper wrapper = super.get(command.getKey()); |
| @@ -143,7 +139,7 @@ public class LayeringCache extends RedisCache { | @@ -143,7 +139,7 @@ public class LayeringCache extends RedisCache { | ||
| 143 | Ehcache ehCache = getL1Cache(super.getName()); | 139 | Ehcache ehCache = getL1Cache(super.getName()); |
| 144 | ehCache.put(new Element(command.getKey(), wrapper.get(), false, layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds(), | 140 | ehCache.put(new Element(command.getKey(), wrapper.get(), false, layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds(), |
| 145 | layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds())); | 141 | layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds())); |
| 146 | - log.info("Update Cache L1 {} - {}", command.getKey(), wrapper.get()); | 142 | + log.debug("Update Cache L1 {} - {}", command.getKey(), wrapper.get()); |
| 147 | } | 143 | } |
| 148 | break; | 144 | break; |
| 149 | default: | 145 | default: |