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

  1. ssm+redis 如何更简洁的利用自定义注解+AOP实现redis缓存

    基于 ssm + maven + redis 使用自定义注解 利用aop基于AspectJ方式 实现redis缓存 如何能更简洁的利用aop实现redis缓存,话不多说,上demo 需求: 数据查询时 ...

  2. SpringBoot AOP控制Redis自动缓存和更新

    导入redis的jar包 <!-- redis --> <dependency> <groupId>org.springframework.boot</gro ...

  3. 基于aop的redis自动缓存实现

    目的: 对于查询接口所得到的数据,只需要配置注解,就自动存入redis!此后一定时间内,都从redis中获取数据,从而减轻数据库压力. 示例: package com.itliucheng.biz; ...

  4. 使用AOP 实现Redis缓存注解,支持SPEL

    公司项目对Redis使用比较多,因为之前没有做AOP,所以缓存逻辑和业务逻辑交织在一起,维护比较艰难所以最近实现了针对于Redis的@Cacheable,把缓存的对象依照类别分别存放到redis的Ha ...

  5. spring aop搭建redis缓存

    SpringAOP与Redis搭建缓存 近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做 ...

  6. SpringBoot集成Redis实现缓存处理(Spring AOP实现)

    第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的A ...

  7. SpringCloud微服务实战——搭建企业级开发框架(三十九):使用Redis分布式锁(Redisson)+自定义注解+AOP实现微服务重复请求控制

      通常我们可以在前端通过防抖和节流来解决短时间内请求重复提交的问题,如果因网络问题.Nginx重试机制.微服务Feign重试机制或者用户故意绕过前端防抖和节流设置,直接频繁发起请求,都会导致系统防重 ...

  8. SpringAOP与Redis搭建缓存

    近期项目查询数据库太慢,持久层也没有开启二级缓存,现希望采用Redis作为缓存.为了不改写原来代码,在此采用AOP+Redis实现. 目前由于项目需要,只需要做查询部分: 数据查询时每次都需要从数据库 ...

  9. Redis+Spring缓存实例

    转自:小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).lis ...

随机推荐

  1. linux 修改IP, DNS -(转自fighter)

    linux下修改IP.DNS.路由命令行设置 ubuntu 版本命令行设置IP cat /etc/network/interfaces # This file describes the networ ...

  2. 回归(regression)与分类(classification)的区别

    术语监督学习,意指给出一个算法,需要部分数据集已经有正确的答案. " 分类和回归的区别在于输出变量的类型. 定量输出称为回归,或者说是连续变量预测:定性输出称为分类,或者说是离散变量预测. ...

  3. wxPython开发之密码管理程序

    不想记密码?密码全设置成一样担心安全?用别人程序担心密码泄露?看完本博客,开发一个属于自己的密码管理程序吧 我们用到的是python的wxPython界面库包 先来看下成果界面:简洁主题明确  要想开 ...

  4. Steam游戏《Northgard(北境之地)》修改器制作

    日期:2021.06.07 博客期:181 星期一 [温馨提示]: 我现在把资源先放到开头,不想研究学习的就直接取用.如果修改器失效了,你们可以在博客园本页直接评论,也可以给我发邮件告诉我,就是不要到 ...

  5. Yolo:实时目标检测实战(下)

    Yolo:实时目标检测实战(下) YOLO:Real-Time Object Detection After a few minutes, this script will generate all ...

  6. SLAM架构的两篇顶会论文解析

    SLAM架构的两篇顶会论文解析 一. 基于superpoint的词袋和图验证的鲁棒闭环检测 标题:Robust Loop Closure Detection Based on Bag of Super ...

  7. CVPR2020:基于层次折叠的跳跃式注意网络点云完成

    CVPR2020:基于层次折叠的跳跃式注意网络点云完成 Point Cloud Completion by Skip-Attention Network With Hierarchical Foldi ...

  8. Python_Selenium之basepage 识别元素、浏览器操作、获取属性、鼠标事件、键盘事件、弹窗、切换frame、切换句柄等封装

    #coding=gbkimport osimport timefrom selenium import webdriverfrom selenium.webdriver.common.by impor ...

  9. 七、SSL加密网站(待解决)

    keytool -genkeypair -alias tomcat -keyalg RSA -keystore /usr/local/tomcat/keystore  //创建私钥和证书文件提示输入密 ...

  10. 二、特殊DNS解析

    一.DNS轮询 1.为站点 www.tedu.cn 提供DNS轮询解析,三台Web服务器节点的IP地址分别为: 192.168.4.10.192.168.4.20.192.168,4.30 步骤: 虚 ...