Spring boot AOP 实现Redis 存储
package com.carloan.common.web.annotation; import java.lang.annotation.*; /**
* 自定义redis缓存注解、只要在service上添加该注解。数据第一次访问都会加载到redis里
*
*
*
* @author 周志伟
*
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisCache {
/**
* SYS 系统级别
* INFO 业务级别
* @return
*/
String type() default "SYS";
}
package com.carloan.common.web.aspect; import com.carloan.common.redisTemplate.service.RedisUtils;
import com.carloan.common.utils.SpringUtil;
import com.carloan.common.web.annotation.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import java.lang.reflect.Method; /**
* @author 周志伟
* @projectname 项目名称: ${project_name}
* @classname: RedisAspect
* @description:
*
* 定义redis缓存AOP
*
* @date 2018/7/18:16:44
*/
@Component
@Aspect
public class RedisAspect {
Logger logger = LoggerFactory.getLogger(RedisAspect.class); @Around("@annotation(redisCache)")
public Object doValid(ProceedingJoinPoint joinPoint, RedisCache redisCache) throws Throwable {
RedisUtils redisUtils=(RedisUtils) SpringUtil.getObject("com.carloan.common.redisTemplate.service.RedisUtils");
Object object=null;
String key=this.getKey(redisCache.type(),joinPoint);
try {
if (!redisUtils.exists(key)) {
object = joinPoint.proceed();
redisUtils.set(key, object);
logger.info("插入redis缓存OK---方法名称{},redis--key{}",joinPoint.getSignature().getName(),key);
} else {
object = redisUtils.get(key);
logger.info("获取redis缓存OK---方法名称{},redis--key{}",joinPoint.getSignature().getName(),key);
}
}catch (Exception e){
object = joinPoint.proceed();
logger.info("执行异常-RedisAspect---方法名称{0},redis--key{}",joinPoint.getSignature().getName(),key);
e.printStackTrace();
}
return object ;
} public String getKey(String type,ProceedingJoinPoint point) throws NoSuchMethodException {
StringBuffer sb = new StringBuffer();
Object[] arguments = point.getArgs();
Signature sig = point.getSignature();
MethodSignature msig = null;
msig = (MethodSignature) sig;
Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
String methodName = currentMethod.getName();
String className = point.getTarget().getClass().getName();
sb.append(type).append(":").append(className).append(":").append(methodName);
if (arguments != null && arguments.length != 0) {
for (Object a : arguments) {
sb.append(":").append(StringUtils.substringBefore(a.toString(),"@"));
}
}
return sb.toString();
} }
调用
Spring boot AOP 实现Redis 存储的更多相关文章
- Spring Boot:使用Redis存储技术
综合概述 Redis是一个开源免费的高性能key-value数据库,读取速度达110000次/s,写入速度达81000次/s.Redis支持丰富的数据类型,如Lists, Hashes, Sets 及 ...
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...
- Spring Boot 2.x Redis多数据源配置(jedis,lettuce)
Spring Boot 2.x Redis多数据源配置(jedis,lettuce) 96 不敢预言的预言家 0.1 2018.11.13 14:22* 字数 65 阅读 727评论 0喜欢 2 多数 ...
- Spring Boot AOP解析
Spring Boot AOP 面向切面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面. AOP(Aspec ...
- Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理
Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理 本文链接:https://blog.csdn.net/puhaiyang/article/details/78146620 ...
- redis分布式锁-spring boot aop+自定义注解实现分布式锁
接这这一篇redis分布式锁-java实现末尾,实现aop+自定义注解 实现分布式锁 1.为什么需要 声明式的分布式锁 编程式分布式锁每次实现都要单独实现,但业务量大功能复杂时,使用编程式分布式锁无疑 ...
- spring boot 中使用 Redis 与 Log
spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...
- Spring Boot中使用Redis数据库
引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedis.可以通过引入spring-boot-starter-redis来配置依赖关系. <depend ...
随机推荐
- 聊一聊这个总下载量36039K的XSS-NPM库,是如何工作的?
上篇文章这一次,彻底理解XSS攻击讲解了XSS攻击的类型和预防方式,本篇文章我们来看这个36039K的XSS-NPM库(你没有看错就是3603W次, 36039K次,36,039,651次,数据来自h ...
- 每日一个linux命令6 -- mv
mv test.log test1.txt 文件改名 mv test1.log test3 文件移动 mv test1.log test2.log test3.log test4 将1,2,3.log ...
- Head First 设计模式 —— 04. 工厂 (Factory) 模式
思考题 如何将实例化具体类的代码从应用中抽离,或者封装起来,使它们不会干扰应用的其他部分? P111 将实例化具体类的代码放入一个对象中管理,通过不同入参决定实例化具体的类 简单工厂 不是23种GOF ...
- js 的关键字
1.get / set var test = { _Name: "Limei", _Age: 20, get name() { return this._Name;}, set a ...
- jxl解析多个excel工作表-java代码
@Override public ResultBean txImportDqKpi(String filePath) { ResultBean rb = new ResultBean(); int s ...
- Educational Codeforces Round 102 (Rated for Div. 2)
比赛地址 A(水题) 题目链接 题目: 给出一个数组\(a\)并能进行一个操作使得数组元素更改为数组任意其他两元素之和,问是否可以让数组元素全部小于等于\(d\) 解析: 排序后判断最大值是否小于等于 ...
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- Nginx 安装与配置教程
标签: Nginx Linux Windows 配置 描述: Ubuntu 下以及 Windows 下 Nginx 的配置:配置详解:有关 Nginx 如何配置 Nginx 在 Ubuntu 下的安装 ...
- wpf 中用 C# 代码创建 PropertyPath ,以对间接目标进行 Storyboard 动画.
如图,一个 Rectangle 一个 Button ,点击按钮时要通过动画完成对 Rectangle填充色的渐变动画. Xaml: 1 <Window 2 x:Class="WpfAp ...
- linux硬盘分区和fdisk命令
分区的几个概念 硬盘分区有三种,主分区.扩展分区.逻辑分区.一个硬盘主分区至少有1个,最多4个,扩展分区可以没有,最多1个.且主分区+扩展分区总共不能超过4个.逻辑分区可以有若干个.在windows下 ...