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. 【并发编程】- ThreadPoolExecutor篇

    Executor框架 Executor框架的两级调度模型(基于HotSpot) 在上层,Java多线程程序通常把应用分解为若干个任务,然后使用用户级的调度器(Executor框架)将这些任务映射为固定 ...

  2. MATLAB在读取excel文件是发生错误,怎么解决?

    转载:https://blog.csdn.net/qq_38712026/article/details/78783422?utm_source=blogxgwz4

  3. springboot集成轻量级权限认证框架sa-token

    sa-token是什么? sa-token是一个JavaWeb轻量级权限认证框架,主要解决项目中登录认证.权限认证.Session会话等一系列由此衍生的权限相关业务.相比于其他安全性框架较容易上手. ...

  4. HP Proliant DL580 gen9 阵列卡P440AR 高速缓存 被禁用

    摘录内容: IMPORTANT: This issue does NOT occur when the operating system shuts down gracefully. In addit ...

  5. Oracle 锁表以及解锁

    -- kill_exec 列为解锁的语句,copy出来执行即可.select 'alter system kill session ''' || s.sid || ',' || s.serial# | ...

  6. python学习笔记 | 列表去重

    ''' @author: 人人都爱小雀斑 @time: 2020/3/10 10:29 @desc: ''' L=[1,5,7,4,6,3,0,5,8,4,4] 方法1:for循环 L1=[] for ...

  7. python函数3-函数嵌套/递归/匿名函数

    2 .函数递归: 3.匿名函数

  8. MySQL 使用sql添加和创建用户

    用户管理 SQL 命令操作 用户表:mysql.user 本质:对mysql.user 表进行增删改查 -- ============== 用户管理 ============= -- 创建用户 -- ...

  9. FI_F4_ZTERM付款条件代码

    这个函数可以弹出一个选择帮助,返回一个付款条件代码 CALL FUNCTION 'FI_F4_ZTERM' EXPORTING I_KOART = 'K' " K为供应商,D为客户 * I_ ...

  10. WCNSS_qcom_cfg.ini WIFI配置文件参数详细解析

    STA相关的一般配置 参数 含义 最小值 最大值 默认值 gNeighborLookupThreshold 1 触发roam scan发生的条件在WCNSS_qcom_cfg.ini文件中gNeigh ...