添加以下配置信息;

/**
* 基于注解添加缓存
*/
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport { private final RedisConnectionFactory redisConnectionFactory; CacheConfig(RedisConnectionFactory redisConnectionFactory) {
this.redisConnectionFactory = redisConnectionFactory;
} @Bean
@Override
public KeyGenerator keyGenerator() {
return (o, method, objects) -> {
StringBuilder sb = new StringBuilder(32);
sb.append(o.getClass().getSimpleName());
sb.append(".");
sb.append(method.getName());
if (objects.length > 0) {
sb.append("#");
}
String sp = "";
for (Object object : objects) {
sb.append(sp);
if (object == null) {
sb.append("NULL");
} else {
sb.append(object.toString());
}
sp = ".";
}
return sb.toString();
};
} /**
* 配置 RedisCacheManager,使用 cache 注解管理 redis 缓存
*/
@Bean
@Override
public CacheManager cacheManager() {
// 初始化一个RedisCacheWriter
RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); // 设置默认过期时间:30 分钟
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
// .disableCachingNullValues()
// 使用注解时的序列化、反序列化
.serializeKeysWith(MyRedisCacheManager.STRING_PAIR)
.serializeValuesWith(MyRedisCacheManager.FASTJSON_PAIR); return new MyRedisCacheManager(cacheWriter, defaultCacheConfig);
}
}

redis配置信息:

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({LettuceConnectionConfiguration.class})
public class RedisConfig { @Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
// set key serializer
StringRedisSerializer serializer = MyRedisCacheManager.STRING_SERIALIZER;
// 设置key序列化类,否则key前面会多了一些乱码
template.setKeySerializer(serializer);
template.setHashKeySerializer(serializer); // fastjson serializer
GenericFastJsonRedisSerializer fastSerializer = MyRedisCacheManager.FASTJSON_SERIALIZER;
template.setValueSerializer(fastSerializer);
template.setHashValueSerializer(fastSerializer);
// 如果 KeySerializer 或者 ValueSerializer 没有配置,则对应的 KeySerializer、ValueSerializer 才使用这个 Serializer
template.setDefaultSerializer(fastSerializer); // factory
template.setConnectionFactory(redisConnectionFactory);
template.afterPropertiesSet();
return template;
} @Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
} }

重写RedisCacheManager

public class MyRedisCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyRedisCacheManager.class);

    private ApplicationContext applicationContext;

    private Map<String, RedisCacheConfiguration> initialCacheConfiguration = new LinkedHashMap<>();

    /**
* key serializer
*/
public static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer(); /**
* value serializer
* <pre>
* 使用 FastJsonRedisSerializer 会报错:java.lang.ClassCastException
* FastJsonRedisSerializer<Object> fastSerializer = new FastJsonRedisSerializer<>(Object.class);
* </pre>
*/ public static final GenericFastJsonRedisSerializer FASTJSON_SERIALIZER = new GenericFastJsonRedisSerializer(); /**
* key serializer pair
*/
public static final RedisSerializationContext.SerializationPair<String> STRING_PAIR = RedisSerializationContext
.SerializationPair.fromSerializer(STRING_SERIALIZER);
/**
* value serializer pair
*/
public static final RedisSerializationContext.SerializationPair<Object> FASTJSON_PAIR = RedisSerializationContext
.SerializationPair.fromSerializer(FASTJSON_SERIALIZER); public MyRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
super(cacheWriter, defaultCacheConfiguration);
} @Override
public Cache getCache(String name) {
Cache cache = super.getCache(name);
return new RedisCacheWrapper(cache);
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} @Override
public void afterPropertiesSet() {
String[] beanNames = applicationContext.getBeanNamesForType(Object.class);
for (String beanName : beanNames) {
final Class clazz = applicationContext.getType(beanName);
add(clazz);
}
super.afterPropertiesSet();
} @Override
protected Collection<RedisCache> loadCaches() {
List<RedisCache> caches = new LinkedList<>();
for (Map.Entry<String, RedisCacheConfiguration> entry : initialCacheConfiguration.entrySet()) {
caches.add(super.createRedisCache(entry.getKey(), entry.getValue()));
}
return caches;
} private void add(final Class clazz) {
ReflectionUtils.doWithMethods(clazz, method -> {
ReflectionUtils.makeAccessible(method);
CacheExpire cacheExpire = AnnotationUtils.findAnnotation(method, CacheExpire.class);
if (cacheExpire == null) {
return;
}
Cacheable cacheable = AnnotationUtils.findAnnotation(method, Cacheable.class);
if (cacheable != null) {
add(cacheable.cacheNames(), cacheExpire);
return;
}
Caching caching = AnnotationUtils.findAnnotation(method, Caching.class);
if (caching != null) {
Cacheable[] cs = caching.cacheable();
if (cs.length > 0) {
for (Cacheable c : cs) {
if (cacheExpire != null && c != null) {
add(c.cacheNames(), cacheExpire);
}
}
}
} else {
CacheConfig cacheConfig = AnnotationUtils.findAnnotation(clazz, CacheConfig.class);
if (cacheConfig != null) {
add(cacheConfig.cacheNames(), cacheExpire);
}
}
}, method -> null != AnnotationUtils.findAnnotation(method, CacheExpire.class));
} private void add(String[] cacheNames, CacheExpire cacheExpire) {
for (String cacheName : cacheNames) {
if (cacheName == null || "".equals(cacheName.trim())) {
continue;
}
long expire = cacheExpire.expire();
LOGGER.info("cacheName: {}, expire: {}", cacheName, expire);
if (expire >= 0) {
// 缓存配置
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(expire))
.disableCachingNullValues()
// .prefixKeysWith(cacheName)
.serializeKeysWith(STRING_PAIR)
.serializeValuesWith(FASTJSON_PAIR);
initialCacheConfiguration.put(cacheName, config);
} else {
LOGGER.warn("{} use default expiration.", cacheName);
}
}
} protected static class RedisCacheWrapper implements Cache {
private final Cache cache; RedisCacheWrapper(Cache cache) {
this.cache = cache;
} @Override
public String getName() {
// LOGGER.info("name: {}", cache.getName());
try {
return cache.getName();
} catch (Exception e) {
LOGGER.error("getName ---> errmsg: {}", e.getMessage(), e);
return null;
}
} @Override
public Object getNativeCache() {
// LOGGER.info("nativeCache: {}", cache.getNativeCache());
try {
return cache.getNativeCache();
} catch (Exception e) {
LOGGER.error("getNativeCache ---> errmsg: {}", e.getMessage(), e);
return null;
}
} @Override
public ValueWrapper get(Object o) {
// LOGGER.info("get ---> o: {}", o);
try {
return cache.get(o);
} catch (Exception e) {
LOGGER.error("get ---> o: {}, errmsg: {}", o, e.getMessage(), e);
return null;
}
} @Override
public <T> T get(Object o, Class<T> aClass) {
// LOGGER.info("get ---> o: {}, clazz: {}", o, aClass);
try {
return cache.get(o, aClass);
} catch (Exception e) {
LOGGER.error("get ---> o: {}, clazz: {}, errmsg: {}", o, aClass, e.getMessage(), e);
return null;
}
} @Override
public <T> T get(Object o, Callable<T> callable) {
// LOGGER.info("get ---> o: {}", o);
try {
return cache.get(o, callable);
} catch (Exception e) {
LOGGER.error("get ---> o: {}, errmsg: {}", o, e.getMessage(), e);
return null;
}
} @Override
public void put(Object o, Object o1) {
// LOGGER.info("put ---> o: {}, o1: {}", o, o1);
try {
cache.put(o, o1);
} catch (Exception e) {
LOGGER.error("put ---> o: {}, o1: {}, errmsg: {}", o, o1, e.getMessage(), e);
}
} @Override
public ValueWrapper putIfAbsent(Object o, Object o1) {
// LOGGER.info("putIfAbsent ---> o: {}, o1: {}", o, o1);
try {
return cache.putIfAbsent(o, o1);
} catch (Exception e) {
LOGGER.error("putIfAbsent ---> o: {}, o1: {}, errmsg: {}", o, o1, e.getMessage(), e);
return null;
}
} @Override
public void evict(Object o) {
// LOGGER.info("evict ---> o: {}", o);
try {
cache.evict(o);
} catch (Exception e) {
LOGGER.error("evict ---> o: {}, errmsg: {}", o, e.getMessage(), e);
}
} @Override
public void clear() {
// LOGGER.info("clear");
try {
cache.clear();
} catch (Exception e) {
LOGGER.error("clear ---> errmsg: {}", e.getMessage(), e);
}
}
}
}

应用:

@GetMapping("/getShopByShopNO/{shopNo}")
public Mono<ShopDO> getShopByShopNO(@PathVariable("shopNo") final String shopNo){
final ShopDO shopByShopNO = shopDAO.getShopByShopNO(shopNo);
return Mono.just(shopByShopNO);
} /**
* 查询店铺详情
* @param shopNo
* @return
*/
@Cacheable(value = "shop",key = "'shop_'.concat(#root.args[0])",sync = true)
@CacheExpire(30)
ShopDO getShopByShopNO(String shopNo);

参考:https://www.cnblogs.com/wjwen/p/9301119.html

基于springboot2.x集成缓存注解及设置过期时间的更多相关文章

  1. redis 一二事 - 设置过期时间,以文件夹形式展示key显示缓存数据

    在使用redis时,有时回存在大量数据的时候,而且分类相同,ID相同 可以使用hset来设置,这样有一个大类和一个小分类和一个value组成 但是hset不能设置过期时间 过期时间只能在set上设置 ...

  2. 可以设置过期时间的Java缓存Map

    前言 最近项目需求需要一个类似于redis可以设置过期时间的K,V存储方式.项目前期暂时不引进redis,暂时用java内存代替. 解决方案 1. ExpiringMap 功能简介 : 1.可设置Ma ...

  3. java操作Redis缓存设置过期时间

    关于Redis的概念和应用本文就不再详解了,说一下怎么在java应用中设置过期时间. 在应用中我们会需要使用redis设置过期时间,比如单点登录中我们需要随机生成一个token作为key,将用户的信息 ...

  4. 29.Jwt集成(3):token设置过期时间、异常判断

    token设置过期时间 package main import ( "fmt" "github.com/dgrijalva/jwt-go" "io/i ...

  5. redis批量设置过期时间

    Redis 中有删除单个 Key 的指令 DEL,但好像没有批量删除 Key 的指令,不过我们可以借助 Linux 的 xargs 指令来完成这个动作.代码如下: redis-cli keys &qu ...

  6. Redis原子性写入HASH结构数据并设置过期时间

    Redis中提供了原子性命令SETEX或SET来写入STRING类型数据并设置Key的过期时间: > SET key value EX NX ok > SETEX key value ok ...

  7. redis中的key设置过期时间

    EXPIRE key seconds 为给定  key  设置生存时间,当  key  过期时(生存时间为  0  ),它会被自动删除. 在 Redis 中,带有生存时间的  key  被称为『易失的 ...

  8. redis文档翻译_key设置过期时间

    Available since 1.0.0.    使用開始版本号1.01 Time complexity: O(1)  时间复杂度O(1) 出处:http://blog.csdn.net/colum ...

  9. 针对永久不过期的key 批量设置过期时间

    问题需求: redis内存暴增,后来发现有很多设置永久不过期. 解决:查找出来之后针对前缀批量设置过期时间 (过期时间与开发沟通 保证服务不受影响) 来源于网上杨一的代码 正好解决了我遇到的问题 在这 ...

随机推荐

  1. 零基础C#网站开发实战教学(全套)最新更新2019-12-16。。。

    这是林枫山自己编写制作的全套Visual Studio 2013 C# 网站开发案例实战教学教程,欢迎下载学习. 下载目录链接如下(如果链接下载不了,请加QQ:714259796获取教程): 网站界面 ...

  2. c++中共享内存原理及实现

    共享内存 (也叫内存映射文件) 主要是通过映射机制实现的 , Windows 下进程的地址空间在逻辑上是相互隔离的 , 但在物理上却是重叠的 ; 所谓的重叠是指同一块内存区域可能被多个进程同时使用 , ...

  3. 单点登录(SSO)解决方案介绍

    一.单点登录的介绍 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用 ...

  4. LINGO与EXCEL之间的数据传递

    前言 LINGO 作为非线性规划运算的专用软件,得出结果一般都是纯文本的一列数据,要想将数据呈现到论文当中,需要整理到 EXCEL 中,使用复制粘贴容易出错还费时,所以必须要动用函数来提高效率! 案例 ...

  5. 【转帖】全网最通俗易懂的Kafka入门

    全网最通俗易懂的Kafka入门 http://www.itpub.net/2019/12/04/4597/ 前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://g ...

  6. 【转帖】Kafka入门介绍

    Kafka入门介绍 https://www.cnblogs.com/swordfall/p/8251700.html 最近在看hdoop的hdfs 以及看了下kafka的底层存储,发现分布式的技术基本 ...

  7. [Docker] - 安装 SQL Server 2019,使用 SSMS 进行登录

    镜像仓库 https://hub.docker.com/_/microsoft-mssql-server 拉取镜像并进行安装(以 SQL Server 2019 CTP 3.2 为例) docker ...

  8. day03——整型、字符串、for循环

    day03 整型 用于比较和运算 32位:-2 ** 31--2 ** 31-1 64位:-2 ** 63--2 ** 63-1 长整型(long) python2中有长整型.获取的是整数 pytho ...

  9. Java 中的 equals,==与 hashCode 的区别与联系

    一. 关系操作符 ==:若操作数的类型是基本数据类型,则该关系操作符判断的是左右两边操作数的值是否相等若操作数的类型是引用数据类型,则该关系操作符判断的是左右两边操作数的内存地址是否相同.也就是说,若 ...

  10. TCP/IP详解 IP路由选择

    TCP/IP详解 IP路由选择 在本篇文章当中, 将通过例子来说明IP路由选择器过程 如图所示, 主机A与主机B分别是处在两个不同的子网当中, 中间通过一个路由连接. 如果主机A请求与主机B进行通行, ...