注解+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 ...
随机推荐
- VIM 三种模式和常用命令
引言 大数据开发工作中,周围的同事不是用 VIM 就是 Emacs,你要是用 UltraEdit 或 notepad++ 都不好意思跟人家打招呼...什么插件呀.语法高亮呀.拼写检查呀,能给它开的都给 ...
- MyBatis 高级查询环境准备(八)
MyBatis 高级查询 之前在学习 Mapper XML 映射文件时,说到 resultMap 标记是 MyBatis 中最重要最强大也是最复杂的标记,而且还提到后面会详细介绍它的高级用法. 听到高 ...
- 9.10 nohup:用户退出系统进程继续工作
nohup命令 可以将程序以忽略挂起信号的方式运行起来,被运行程序的输出信息将不会显示到终端. 无论是否将nohup命令的输出重定向到终端,输出都将写入到当前目录的nohup.out文件 ...
- 小程序中在设置了textarea后三个祖级内事件失效
在一次写小程序项目中收货地址中的详细地址时,我用的是文本域,下边的三个bindtap事件却不能使用了:下图: 报错信息如下图: 通过一番查找以及尝试之后,我发现是因为textarea标签的问题,但是依 ...
- 『动善时』JMeter基础 — 34、JMeter接口关联【XPath提取器】
目录 1.XPath提取器介绍 2.XPath提取器界面详解 3.XPath提取器的使用 (1)测试计划内包含的元件 (2)网易首页请求界面内容 (3)XPath提取器界面内容 (4)百度首页请求界面 ...
- pytest - 失败重运行机制:rerun
失败重运行机制 用例失败的情况下,可以重新运行用例 一旦用例失败,马上重新运行 安装插件:pip install pytest-rerunfailures 使用命令:--reruns 重试次数 如 - ...
- 纯C++代码实现将像素矩阵保存为bmp图片
由于工作需要,时常需要将像素矩阵保存图片显示观看.为此,特地总结了三种使用纯C++代码生成bmp图片的方法.分别是使用自定义数据.从外界导入的txt和csv以及从图片中导入的数据. 1.使用自定义数据 ...
- ML Pipelines管道
ML Pipelines管道 In this section, we introduce the concept of ML Pipelines. ML Pipelines provide a uni ...
- MySQL慢查询日志如何开启以及分析
1.MySQL慢查询日志是什么 (1)MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录MySQL中查询时间超过(大于)设置阈值(long_query_time)的语句,记录到慢查询日志 ...
- 【SQLite】SQLite文件突然变大怎么办?瘦身办法
使用VACUUM命令即可: VACUUM 命令通过复制主数据库中的内容到一个临时数据库文件,然后清空主数据库,并从副本中重新载入原始的数据库文件.这消除了空闲页,把表中的数据排列为连续的,另外会清理数 ...