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 存储的更多相关文章

  1. Spring Boot:使用Redis存储技术

    综合概述 Redis是一个开源免费的高性能key-value数据库,读取速度达110000次/s,写入速度达81000次/s.Redis支持丰富的数据类型,如Lists, Hashes, Sets 及 ...

  2. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  3. spring boot 中使用redis session

    spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...

  4. 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 多数 ...

  5. Spring Boot AOP解析

    Spring Boot AOP 面向切面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面. AOP(Aspec ...

  6. Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理

    Spring Boot AOP之对请求的参数入参与返回结果进行拦截处理   本文链接:https://blog.csdn.net/puhaiyang/article/details/78146620 ...

  7. redis分布式锁-spring boot aop+自定义注解实现分布式锁

    接这这一篇redis分布式锁-java实现末尾,实现aop+自定义注解 实现分布式锁 1.为什么需要 声明式的分布式锁 编程式分布式锁每次实现都要单独实现,但业务量大功能复杂时,使用编程式分布式锁无疑 ...

  8. spring boot 中使用 Redis 与 Log

    spring boot + mybatis + redis 配置 1.application.yml #配置访问的URLserver: servlet-path: /web port: spring: ...

  9. Spring Boot中使用Redis数据库

    引入依赖 Spring Boot提供的数据访问框架Spring Data Redis基于Jedis.可以通过引入spring-boot-starter-redis来配置依赖关系. <depend ...

随机推荐

  1. docker获取Let's Encrypt永久免费SSL证书

    一 起因 官方的cerbot太烦了,不建议使用 还不如野蛮生长的acme.sh,而这里介绍docker运行cerbot获取Let's Encrypt永久免费SSL证书 二 选型 cerbot的证书不会 ...

  2. C# 9 新特性 —— 补充篇

    C# 9 新特性 -- 补充篇 Intro 前面我们分别介绍了一些 C# 9 中的新特性,还有一些我觉得需要了解一下的新特性,写一篇作为补充. Top-Level Statements 在以往的代码里 ...

  3. String被final修饰

    源码:

  4. CQRS与Event Sourcing之浅见

    引言 DDD是近年软件设计的热门.CQRS与Event Sourcing作为实施DDD的一种选择,也逐步进入人们的视野.围绕这两个主题,软件开发的大咖[Martin Fowler].[Greg You ...

  5. 查找linux系统下的端口被占用进程的两种方法 【转】

    在linux下开发时,你的软件可能要使用某一个端口,或者想查找某一个端口是否被占用.需要怎么做呢??这的确是一个比较烦恼的问题,我也此为这个苦恼过.但是通过查找man手册,还是同事的交流.总结出来两种 ...

  6. d3 zoom 抖动问题 事件

    最近在使用d3 zoom得时候   遇到一个小坑 直接对元素添加 zoom事件 会有很大得抖动,查文档 看代码之后发现是 由于元素在不断变化, 所以计算基础值也不不断变化,所以会导致计算出来得值 忽大 ...

  7. Dubbo+Zookeeper(二)Dubbo架构

    上次更新博客已经是一年前,这一年发生了很多事,并不顺利,甚至有些痛苦,不过不管怎样,不要停止学习,只有学习才能让你变强,应对更多不安定. 一.RPC概念 Dubbo服务是一个RPC框架,那我们首先就要 ...

  8. Nginx安装步骤及本地浏览器不通解决方案,Nginx在Linux发布项目,Tomcat 与本地浏览器不通解决方案

    Nginx安装步骤及本地浏览器不通解决方案 1.将安装包放到usr/local文件夹下 2..进入local目录,解压 tar -zxvf nginx-1.17.5.tar.gz 3.进入 nginx ...

  9. 【Docker】runtime create failed: container_linux.go:345: 解决

    ------------------------------------------------------------------------------------------------- | ...

  10. 【Oracle】删除(释放)数据文件/表空间流程

    oracle删除(释放)数据文件/表空间流程 生产环境:数据库里空间不足,niptest 表空间251G,只使用了17G 再alter database datafile '...../niptest ...