注解+AOP实现redis遍历缓存
1.注解
package com.yun.smart.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheAnnotation { /**
* 缓存key值
* @return
*/
String key(); /**
* 缓存时长
* @return
*/
long timeToLive(); /**
* 对象类型
* @return
*/
Class<?> clazz(); /**
* 缓存时长单位,默认分
* @return
*/
TimeUnit timeUnit() default TimeUnit.MINUTES; }
2.AOP
package com.yun.smart.aspect; import java.util.concurrent.TimeUnit; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.yun.smart.annotation.CacheAnnotation;
import com.yun.smart.redis.RedisService; @Component
@Aspect
public class CacheAspect { private static Logger LOGGER = LoggerFactory.getLogger(CacheAspect.class); @Autowired
private RedisService redisService; @Around("within(com.yun.smart.cache.service.*) && @annotation(cacheAnnotation)")
public Object doAround(ProceedingJoinPoint pJoinPoint, CacheAnnotation cacheAnnotation) throws Throwable {
String key = cacheAnnotation.key();
TimeUnit timeUnit = cacheAnnotation.timeUnit();
long timeToLive = cacheAnnotation.timeToLive();
Class<?> clazz = cacheAnnotation.clazz(); //获取参数
Object[] args = pJoinPoint.getArgs();
if (args[0] == null) return null; key = key.concat(args[0].toString());
Object obj = redisService.get(key, clazz); if (obj == null) {
Object result = pJoinPoint.proceed();
if (result == null) {
LOGGER.warn("[{}]无命中。", key);
return null;
} redisService.put(key, result, timeToLive, timeUnit);
LOGGER.debug("从数据库命中:{}", result);
return result;
} else {
LOGGER.debug("从缓存命中:{}", obj);
return obj;
} } }
3.使用
/**
* 根据openId查询用户信息
* @param openId
* @return
*/
@CacheAnnotation(key=CacheConstant.USER, clazz=UserInfo.class, timeToLive=24, timeUnit=TimeUnit.HOURS)
public UserInfo getUserInfoById(Long userInfoId) {
return userInfoService.selectById(userInfoId);
}
注解+AOP实现redis遍历缓存的更多相关文章
- ssm+redis 如何更简洁的利用自定义注解+AOP实现redis缓存
基于 ssm + maven + redis 使用自定义注解 利用aop基于AspectJ方式 实现redis缓存 如何能更简洁的利用aop实现redis缓存,话不多说,上demo 需求: 数据查询时 ...
- SpringBoot AOP控制Redis自动缓存和更新
导入redis的jar包 <!-- redis --> <dependency> <groupId>org.springframework.boot</gro ...
- 基于aop的redis自动缓存实现
目的: 对于查询接口所得到的数据,只需要配置注解,就自动存入redis!此后一定时间内,都从redis中获取数据,从而减轻数据库压力. 示例: package com.itliucheng.biz; ...
- 使用AOP 实现Redis缓存注解,支持SPEL
公司项目对Redis使用比较多,因为之前没有做AOP,所以缓存逻辑和业务逻辑交织在一起,维护比较艰难所以最近实现了针对于Redis的@Cacheable,把缓存的对象依照类别分别存放到redis的Ha ...
- spring aop搭建redis缓存
SpringAOP与Redis搭建缓存 近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做 ...
- SpringBoot集成Redis实现缓存处理(Spring AOP实现)
第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的A ...
- SpringCloud微服务实战——搭建企业级开发框架(三十九):使用Redis分布式锁(Redisson)+自定义注解+AOP实现微服务重复请求控制
通常我们可以在前端通过防抖和节流来解决短时间内请求重复提交的问题,如果因网络问题.Nginx重试机制.微服务Feign重试机制或者用户故意绕过前端防抖和节流设置,直接频繁发起请求,都会导致系统防重 ...
- SpringAOP与Redis搭建缓存
近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做查询部分: 数据查询时每次都需要从数据库 ...
- Redis+Spring缓存实例
转自:小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).lis ...
随机推荐
- 云原生ASP.NET Core程序的可监测性和可观察性
分布式应用程序很复杂,给开发人员调试和修复生产问题带来了一系列挑战.尽管微服务架构可帮助维持一支规模较小,可以自主工作并专注于独立业务团队,但由于其分布式性质,它带来了新的挑战.例如,在业务交易过程中 ...
- Kubernetes认证入门指南
Kubernetes用来执行安全访问和权限的步骤有3个--认证(Authentication).授权(Authorization)和准入(Admission).在本文中,我们先开始了解认证(Authe ...
- Nginx 配置实例-配置虚拟主机
Nginx 配置实例-配置虚拟主机 配置基于域名的虚拟主机 1. 配置域名为 aaa.domain.com 的虚拟主机 1.1 nginx 中虚拟主机的配置 1.2 相关目录及文件的创建 1.3 验证 ...
- AlexeyAB DarkNet YOLOv3框架解析与应用实践(四)
AlexeyAB DarkNet YOLOv3框架解析与应用实践(四) Nightmare 从前,在一所大学的大楼里,西蒙尼亚.维达第和齐瑟曼有一个很好的主意,几乎和你现在坐的大楼完全不同.他们想,嘿 ...
- GStreamer跨平台多媒体框架
GStreamer跨平台多媒体框架 Gstreamer基本概念 GStreamer是用于构造媒体处理组件图的库.它支持的应用程序范围从简单的Ogg / Vorbis回放,音频/视频流到复杂的音频(混合 ...
- NVIDIA安培架构
NVIDIA安培架构 NVIDIA Ampere Architecture In-Depth 在2020年英伟达GTC主题演讲中,英伟达创始人兼首席执行官黄仁勋介绍了基于新英伟达安培GPU架构的新英伟 ...
- IDEA骚技巧
1. var 声明 2. null 判空 3. notnull 判非空 4. nn 判非空 5. for 遍历 6. fori 带索引的遍历 7. not 取反 8. if 条件判断 9. cast ...
- Pandas高级教程之:Dataframe的合并
目录 简介 使用concat 使用append 使用merge 使用join 覆盖数据 简介 Pandas提供了很多合并Series和Dataframe的强大的功能,通过这些功能可以方便的进行数据分析 ...
- Redis-持久化策略
redis是一个内存数据库,一旦服务器宕机,内存中的数据将全部丢失.所以,对 Redis 来说,实现数据的持久化,避免从后端数据库中进行恢复,是至关重要的. 目前,Redis 的持久化主要有两大机制, ...
- 【VBA】判断文件是否存在
效果: 源码: Sub 判断文件是否存在() Dim strcfg As String strcfg = "D:\a.cfg" If Dir(strcfg, vbDirectory ...