【cache】增加一个缓存过期接口,增加fastjson用的redis序列化器

pull/3/head
fengshuonan 2020-12-25 13:28:58 +08:00
parent 683e506db9
commit c6bd3aebdf
6 changed files with 77 additions and 3 deletions

View File

@ -53,6 +53,15 @@ public interface CacheOperatorApi<T> {
*/
void remove(String... key);
/**
*
*
* @param key
* @author stylefeng
* @date 2020/7/8 22:09
*/
void expire(String key, Long expiredSeconds);
/**
* key
*

View File

@ -23,4 +23,9 @@ public interface CacheConstants {
*/
String CACHE_DELIMITER = ":";
/**
* hutool
*/
Long NONE_EXPIRED_TIME = 1000L * 3600 * 24 * 999;
}

View File

@ -47,6 +47,12 @@ public abstract class AbstractMemoryCacheOperator<T> 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);

View File

@ -46,6 +46,11 @@ public abstract class AbstractRedisCacheOperator<T> 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();

View File

@ -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<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final Class<T> clazz;
public FastJson2JsonRedisSerializer(Class<T> 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);
}
}

View File

@ -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<String, String> timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999);
TimedCache<String, String> 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<String, Long> timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999);
TimedCache<String, Long> 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<String, String> timedCache = CacheUtil.newTimedCache(1000L * 3600 * 24 * 999);
TimedCache<String, String> timedCache = CacheUtil.newTimedCache(NONE_EXPIRED_TIME);
WhiteListMemoryCache whiteListMemoryCache = new WhiteListMemoryCache(timedCache);
return new WhiteListService(whiteListMemoryCache);
}