注解+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 ...
随机推荐
- 如何在思科交换机上配置Telnet远程登录
本地用户认证登录 如果配置本地用户认证,则需要开启本地数据库认证,这时就不需要配置相应虚拟终端认证密码了,但要至少配置一个本地用户并设置用户密码用来进行登录认证,具体配置如下: C2960#conf ...
- react-redux 源码浅析
react-redux 版本号 7.2.3 react-redux 依赖的库: "dependencies": { "@babel/runtime": &quo ...
- [Django高级之中间件、csrf跨站请求伪造]
[Django高级之中间件.csrf跨站请求伪造] Django中间件 什么是中间件? Middleware is a framework of hooks into Django's request ...
- SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入
全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...
- Go benchmark 详解
前言 基准测试(benchmark)是 go testing 库提供的,用来度量程序性能,算法优劣的利器. 在日常生活中,我们使用速度 m/s(单位时间内物体移动的距离)大小来衡量一辆跑车的性能,同理 ...
- PVD与CVD性能比较
PVD与CVD性能比较 CVD定义: 通过气态物质的化学反应在衬底上淀积一层薄膜材料的过程. CVD技术特点: 具有淀积温度低.薄膜成分和厚度易于控制.均匀性和重复性好.台阶覆盖优良.适用范围广.设备 ...
- MinkowskiBroadcast广播
MinkowskiBroadcast广播 MinkowskiBroadcastAddition广播加法 class MinkowskiEngine.MinkowskiBroadcastAddition ...
- Spring Cloud系列(五):服务网关Zuul
在前面的篇章都是一个服务消费者去调用一个服务提供者,但事实上我们的系统基本不会那么简单,如果真的是那么简单的业务架构我们也没必要用Spring Cloud,直接部署一个Spring Boot应用就够了 ...
- Redis 5种数据结构及对应使用场景
本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 也当过面试官,面试过不少应聘者,因为是我自己招人自己用,所以我不会看应聘者造火箭的 ...
- Spring Boot WebFlux-10——WebFlux 实战图书管理系统
前言 本篇内容我们会实现如下图所示的城市管理系统,因为上面案例都用的是 City,所以这里直接使用城市作为对象,写一个简单的城市管理系统,如图所示: 结构 类似上面讲的工程搭建,新建一个工程编写此案例 ...