diff --git a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java index e9cfee9ae..cf256202b 100644 --- a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java +++ b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/CacheOperatorApi.java @@ -53,6 +53,15 @@ public interface CacheOperatorApi { */ void remove(String... key); + /** + * 删除缓存 + * + * @param key 键,多个 + * @author stylefeng + * @date 2020/7/8 22:09 + */ + void expire(String key, Long expiredSeconds); + /** * 判断某个key值是否存在于缓存 * diff --git a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java index a5e629222..5b65293bd 100644 --- a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java +++ b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java @@ -23,4 +23,9 @@ public interface CacheConstants { */ String CACHE_DELIMITER = ":"; + /** + * 给hutool缓存用的无限过期时间 + */ + Long NONE_EXPIRED_TIME = 1000L * 3600 * 24 * 999; + } diff --git a/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractMemoryCacheOperator.java b/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractMemoryCacheOperator.java index 83d9a2a0f..2b1251718 100644 --- a/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractMemoryCacheOperator.java +++ b/kernel-d-cache/cache-sdk-memory/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractMemoryCacheOperator.java @@ -47,6 +47,12 @@ public abstract class AbstractMemoryCacheOperator implements CacheOperatorApi } } + @Override + public void expire(String key, Long expiredSeconds) { + T value = this.get(getCommonKeyPrefix() + key); + timedCache.put(getCommonKeyPrefix() + key, value, expiredSeconds * 1000); + } + @Override public boolean contains(String key) { return timedCache.containsKey(key); diff --git a/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractRedisCacheOperator.java b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractRedisCacheOperator.java index fd5788f97..b4712db95 100644 --- a/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractRedisCacheOperator.java +++ b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/AbstractRedisCacheOperator.java @@ -46,6 +46,11 @@ public abstract class AbstractRedisCacheOperator implements CacheOperatorApi< redisTemplate.delete(withPrefixKeys); } + @Override + public void expire(String key, Long expiredSeconds) { + redisTemplate.boundValueOps(getCommonKeyPrefix() + key).expire(expiredSeconds, TimeUnit.SECONDS); + } + @Override public boolean contains(String key) { T value = redisTemplate.boundValueOps(getCommonKeyPrefix() + key).get(); diff --git a/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/serializer/FastJson2JsonRedisSerializer.java b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/serializer/FastJson2JsonRedisSerializer.java new file mode 100644 index 000000000..e0c433088 --- /dev/null +++ b/kernel-d-cache/cache-sdk-redis/src/main/java/cn/stylefeng/roses/kernel/cache/serializer/FastJson2JsonRedisSerializer.java @@ -0,0 +1,47 @@ +package cn.stylefeng.roses.kernel.cache.serializer; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.Feature; +import com.alibaba.fastjson.serializer.SerializerFeature; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * fastjson redis序列化器 + * + * @author fengshuonan + * @date 2017/11/7 上午9:20 + */ +public class FastJson2JsonRedisSerializer implements RedisSerializer { + + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + + private final Class clazz; + + public FastJson2JsonRedisSerializer(Class clazz) { + super(); + this.clazz = clazz; + } + + @Override + public byte[] serialize(T object) throws SerializationException { + if (object == null) { + return new byte[0]; + } + return JSON.toJSONString(object, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException { + if (bytes == null || bytes.length <= 0) { + return null; + } + String str = new String(bytes, DEFAULT_CHARSET); + + return JSON.parseObject(str, clazz, Feature.SupportAutoType); + } + +} \ No newline at end of file diff --git a/kernel-d-validator/validator-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/validator/starter/ValidatorAutoConfiguration.java b/kernel-d-validator/validator-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/validator/starter/ValidatorAutoConfiguration.java index 2ed1254a9..9c8790862 100644 --- a/kernel-d-validator/validator-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/validator/starter/ValidatorAutoConfiguration.java +++ b/kernel-d-validator/validator-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/validator/starter/ValidatorAutoConfiguration.java @@ -15,6 +15,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import static cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants.NONE_EXPIRED_TIME; + /** * 校验器自动配置 * @@ -33,7 +35,7 @@ public class ValidatorAutoConfiguration { @Bean @ConditionalOnMissingBean(BlackListApi.class) public BlackListApi blackListApi() { - TimedCache timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999); + TimedCache timedCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME); BlackListMemoryCache blackListMemoryCache = new BlackListMemoryCache(timedCache); return new BlackListService(blackListMemoryCache); } @@ -47,7 +49,7 @@ public class ValidatorAutoConfiguration { @Bean @ConditionalOnMissingBean(CountValidatorApi.class) public CountValidatorApi countValidatorApi() { - TimedCache timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999); + TimedCache timedCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME); DefaultCountValidateCache defaultCountValidateCache = new DefaultCountValidateCache(timedCache); return new DefaultCountValidator(defaultCountValidateCache); } @@ -61,7 +63,7 @@ public class ValidatorAutoConfiguration { @Bean @ConditionalOnMissingBean(WhiteListApi.class) public WhiteListApi whiteListApi() { - TimedCache timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999); + TimedCache timedCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME); WhiteListMemoryCache whiteListMemoryCache = new WhiteListMemoryCache(timedCache); return new WhiteListService(whiteListMemoryCache); }