From 7922024d8b174d8c57b9edfd0a031e722f366208 Mon Sep 17 00:00:00 2001 From: boni Date: Wed, 31 Oct 2018 18:21:42 +0800 Subject: [PATCH] #chore:升级版本为2.0.0 --- pom.xml | 8 ++++---- src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java | 20 ++++++++++++++++---- src/main/java/com/irrigation/icl/cache/core/LayeringCache.java | 55 ++++++++++--------------------------------------------- src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java | 13 +++++-------- src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java | 41 ++++++++++++++++++----------------------- src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java | 6 +++--- src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java | 11 ++++++----- 7 files changed, 62 insertions(+), 92 deletions(-) diff --git a/pom.xml b/pom.xml index d8bd51a..d61b9ec 100644 --- a/pom.xml +++ b/pom.xml @@ -6,20 +6,20 @@ com.irrigation common-lib - 1.2.4 + 2.0.0 UTF-8 1.8 - 1.5.10.RELEASE + 2.0.5.RELEASE 2.7.0 1.16.20 1.11 20.0 1.7.24 1.2.49 - 4.3.14.RELEASE - 4.2.4.RELEASE + 5.0.9.RELEASE + 5.0.9.RELEASE 2.6.11 diff --git a/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java b/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java index e8dcbb1..e49c26f 100644 --- a/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java +++ b/src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java @@ -9,6 +9,8 @@ import org.springframework.cache.CacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -17,6 +19,8 @@ import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; +import java.time.Duration; + /** * @Author: boni * @Date: 2018/8/24-下午2:37 @@ -29,11 +33,19 @@ public class LayeringCacheAutoFactory { @Bean public LayeringCacheManager cacheManager(RedisTemplate redisTemplate, LayeringCacheProperties layeringCacheProperties) { - LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); - cacheManager.setUsePrefix(true); + + RedisCacheWriter redisCacheWriter = RedisCacheWriter + .nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L; - cacheManager.setExpires(layeringCacheProperties.getExpires()); - cacheManager.setDefaultExpiration(expire); + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration + .defaultCacheConfig() + .entryTtl(Duration.ofSeconds(expire)); + LayeringCacheManager cacheManager = + new LayeringCacheManager( + redisTemplate, + redisCacheWriter, + redisCacheConfiguration, + layeringCacheProperties); return cacheManager; } 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 3917077..2b9dad0 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCache.java @@ -14,7 +14,8 @@ import net.sf.ehcache.store.MemoryStoreEvictionPolicy; import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import org.springframework.data.redis.cache.RedisCache; -import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.util.StopWatch; import java.text.SimpleDateFormat; @@ -36,17 +37,14 @@ public class LayeringCache extends RedisCache { private CacheManager layeringL1CacheManager; private boolean layerL1Enable; private String ID; -// private String HOST; -// private String CACHE_STORE; -// private String CACHE_STORE_SYNC; - - public LayeringCache(LayeringCacheManager layeringCacheManager, String name, byte[] prefix, - RedisOperations redisOperations, long expiration) { - super(name, prefix, redisOperations, expiration); + public LayeringCache(LayeringCacheManager layeringCacheManager, String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) { + super(name, cacheWriter, cacheConfig); this.layeringCacheManager = layeringCacheManager; + this.layeringCacheProperties = layeringCacheManager.getLayeringCacheProperties(); this.layerL1Enable = layeringCacheProperties.getCacheL1().isEnable(); + initEhcache(); } @@ -54,6 +52,7 @@ public class LayeringCache extends RedisCache { public Cache.ValueWrapper get(Object key) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); + Ehcache ehCache = null; if (layerL1Enable) { ehCache = getL1Cache(super.getName()); @@ -157,7 +156,7 @@ public class LayeringCache extends RedisCache { Callable callable = () -> { Ehcache cache = layeringL1CacheManager.getCache(name); if (cache == null) { - CacheConfiguration cacheConfiguration = getCacheConfiguration(); + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); cacheConfiguration.setName(name); Ehcache ehcache = new net.sf.ehcache.Cache(cacheConfiguration); layeringL1CacheManager.addCache(ehcache); @@ -188,7 +187,7 @@ public class LayeringCache extends RedisCache { return simpleDateFormat.format(new Date()); } - private CacheConfiguration getCacheConfiguration() { + private CacheConfiguration getEncacheCacheConfiguration() { // Cache CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setEternal(false); @@ -205,12 +204,6 @@ public class LayeringCache extends RedisCache { cacheConfiguration.copyOnRead(true); cacheConfiguration.copyOnWrite(true); -// CopyStrategyConfiguration copyStrategyConfiguration = new CopyStrategyConfiguration(); -// copyStrategyConfiguration.setClass(MyCopyStrategy.class.getName()); -// copyStrategyConfiguration.setCopyStrategyInstance(new MyCopyStrategy()); - -// cacheConfiguration.addCopyStrategy(copyStrategyConfiguration); - cacheConfiguration.setTimeToIdleSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds()); cacheConfiguration.setTimeToLiveSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds()); @@ -220,13 +213,6 @@ public class LayeringCache extends RedisCache { private void initEhcache() { if (layerL1Enable) { this.ID = layeringCacheProperties.getCacheL1().getKey() + "." + getDate(); -// this.HOST = Utils.getLocalHostIP(); -// this.CACHE_STORE = layeringCacheProperties.getCacheL1().getKey() -// + layeringCacheProperties.getCacheL1().getSeparator() -// + "cache" -// + layeringCacheProperties.getCacheL1().getSeparator() -// + "store"; -// this.CACHE_STORE_SYNC = this.CACHE_STORE + layeringCacheProperties.getCacheL1().getSeparator() + "sync"; Configuration configuration = new Configuration(); configuration.setName(this.ID); @@ -238,7 +224,7 @@ public class LayeringCache extends RedisCache { dsc.setPath(layeringCacheProperties.getCacheL1().getLocalStoreLocation() + this.ID); configuration.diskStore(dsc); - CacheConfiguration cacheConfiguration = getCacheConfiguration(); + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); configuration.setDefaultCacheConfiguration(cacheConfiguration); configuration.setDynamicConfig(false); @@ -248,25 +234,4 @@ public class LayeringCache extends RedisCache { layeringL1CacheManager = new CacheManager(configuration); } } -// -// class MyCopyStrategy implements ReadWriteCopyStrategy { -// @Override -// public Element copyForWrite(Element value) { -// if (value != null) { -// Object temp = (Serializable) value.getObjectValue(); -// return new Element(value.getObjectKey(), temp); -// } -// return value; -// } -// -// @Override -// public Element copyForRead(Element storedValue) { -// if (storedValue != null) { -// Object temp = (Serializable) storedValue.getObjectValue(); -// return new Element(storedValue.getObjectKey(), temp); -// } -// return storedValue; -// -// } -// } } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java index a8cfbe6..8dbc834 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java @@ -1,6 +1,6 @@ package com.irrigation.icl.cache.core; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -12,13 +12,10 @@ import org.springframework.core.type.AnnotatedTypeMetadata; public class LayeringCacheLoadCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( - context.getEnvironment(), "springext.cache."); - - Boolean enable = resolver.getProperty("enable",Boolean.class); - if(enable == null){ - return false; - } + Boolean enable = Binder + .get(context.getEnvironment()) + .bind("springext.cache.enable", Boolean.class) + .orElse(false); return enable.booleanValue(); } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java index dd7fedd..328afba 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java @@ -1,47 +1,42 @@ package com.irrigation.icl.cache.core; import com.irrigation.icl.cache.Command; +import lombok.Getter; import org.springframework.data.redis.cache.RedisCache; +import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; -import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.core.RedisTemplate; /** * @Author: boni * @Date: 2018/8/24-下午2:29 */ +@Getter public class LayeringCacheManager extends RedisCacheManager { + final RedisCacheWriter cacheWriter; + final RedisCacheConfiguration redisCacheConfiguration; private LayeringCacheProperties layeringCacheProperties; - - public LayeringCacheProperties getLayeringCacheProperties() { - return layeringCacheProperties; - } - - public void setLayeringCacheProperties(LayeringCacheProperties layeringCacheProperties) { + private RedisTemplate redisTemplate; + + public LayeringCacheManager(RedisTemplate redisTemplate, RedisCacheWriter cacheWriter, RedisCacheConfiguration redisCacheConfiguration, + LayeringCacheProperties layeringCacheProperties) { + super(cacheWriter, redisCacheConfiguration); + this.cacheWriter = cacheWriter; + this.redisCacheConfiguration = redisCacheConfiguration; + this.redisTemplate = redisTemplate; this.layeringCacheProperties = layeringCacheProperties; } - public LayeringCacheManager(RedisOperations redisOperations, LayeringCacheProperties layeringCacheProperties) { - super(redisOperations); - this.layeringCacheProperties = layeringCacheProperties; - } - - @SuppressWarnings("unchecked") @Override - protected RedisCache createCache(String cacheName) { - long expiration = computeExpiration(cacheName); - return new LayeringCache( - this, - cacheName, - (this.isUsePrefix() ? this.getCachePrefix().prefix(cacheName) : null), - this.getRedisOperations(), - expiration); + protected RedisCache getMissingCache(String name) { + return new LayeringCache(this, name, this.cacheWriter, this.redisCacheConfiguration); } - //notify other redis clent to update L1 public void publishMessage(Command command) { - this.getRedisOperations().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); + this.getRedisTemplate().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); } diff --git a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java index 80db5a1..c0bebd0 100644 --- a/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java +++ b/src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java @@ -73,10 +73,10 @@ public class LayeringCacheProperties { /** * 本地缓存过期时间,单位秒,默认10分钟 */ - private int localTimeToLiveSeconds = 1 * 60; - private int localTimeToIdleSeconds = 0; + private int localTimeToLiveSeconds = 10 * 60; + private int localTimeToIdleSeconds = 30; /** - * 本地缓存清理周期,单位秒,默认3分钟 + * 本地缓存清理周期,单位秒,默认90秒 */ private int localDiskExpiryThreadIntervalSeconds = 90; } diff --git a/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java b/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java index 8bd3600..373401a 100644 --- a/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java +++ b/src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java @@ -1,18 +1,19 @@ package com.irrigation.icl.utils; -import org.springframework.security.authentication.encoding.BasePasswordEncoder; -import org.springframework.security.authentication.encoding.Md5PasswordEncoder; + +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; public class PasswordEncoderUtil { - private final static BasePasswordEncoder encoder = new Md5PasswordEncoder(); + private final static PasswordEncoder encoder = new BCryptPasswordEncoder(4); public static String encodePassword(String password) { - return encoder.encodePassword(password, null); + return encoder.encode(password); } public static boolean isPasswordValid(String rawPassword, String encodedPassword) { - return encoder.isPasswordValid(encodedPassword, rawPassword, null); + return encoder.matches(rawPassword, encodedPassword); } } -- libgit2 0.21.4