此方案借助aop自定义注解来创建redis缓存机制。

1、创建自定义注解类

package com.tp.soft.common.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheAnnotation {
String name() default "";
String value() default "";
}

2、创建aop切面类

package com.tp.soft.aop;

import java.lang.reflect.Field;
import java.lang.reflect.Method; 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.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import com.tp.soft.common.util.CacheAnnotation;
import com.tp.soft.entity.User;
import com.tp.soft.redis.RedisCacheAn; @Aspect
public class CacheRedisAop { @Pointcut("@annotation(com.tp.soft.common.util.CacheAnnotation)")
public void pointCutMethod(){ } @Around("pointCutMethod()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
//获取缓存的唯一key格式ID
String cacheKey = getCacheKey(pjp); RedisCacheAn ra = new RedisCacheAn(cacheKey);
//从redis获取缓存数据
Object obj = (Object) ra.getObject(cacheKey);
//存在直接返回,不再接下去执行查询数据库操作
if(obj != null){
return obj;
} //不存在执行数据库操作
Object proceed = pjp.proceed(); //将查询的对象存入redis缓存
ra.putObject(cacheKey,proceed);
return proceed;
} private String getCacheKey(ProceedingJoinPoint pjp) throws NoSuchMethodException, SecurityException {
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method targetMethod = methodSignature.getMethod(); Class<? extends Object> cls = pjp.getTarget().getClass(); Object[] args = pjp.getArgs(); Method method = cls.getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes()); String name = method.getAnnotation(CacheAnnotation.class).name();
String value = method.getAnnotation(CacheAnnotation.class).value();
String[] split = value.split(","); for (String field : split) {
name += "." + field;
} String id = ""; if(args != null && args.length>0){
id = String.valueOf(args[0]);
} name += "=" + id;
String redisKey = method + "." + name;
//查询缓存是否存在
return redisKey;
}
}

切面注解@annotation 只要在方法上注解@CacheAnnotation  就进入AOP该类进行处理,所以在要进入缓存机制的业务层注入这个写的自定义注解,具体的一些redis获取缓存链接对象的类在之前的

ssm+redis整合(通过cache方式)

ssm+redis整合之redis连接池注入

都有写到,可以参考

3、接下来就是在业务层需要缓存的方法上加入注解就可以了,其中name 和value 2个属性主要是为了生成唯一的redis  keyid

package com.tp.soft.service.sys.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tp.soft.common.exception.BaseServiceException;
import com.tp.soft.common.util.CacheAnnotation;
import com.tp.soft.dao.UserMapper;
import com.tp.soft.entity.User;
import com.tp.soft.service.sys.UserSvc; @Service("userService")
public class UserSvcImpl implements UserSvc{ @Resource
private UserMapper userMapper; @CacheAnnotation(name="user",value="id")
public User getUser(int id) throws BaseServiceException{
return userMapper.getUserById(id);
} }

ssm+redis整合(通过aop自定义注解方式)的更多相关文章

  1. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  2. springboot aop 自定义注解方式实现完善日志记录(完整源码)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...

  3. springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)

    https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...

  4. ssm+redis整合(通过cache方式)

    这几天的研究ssm redis 终于进入主题了,今天参考了网上一些文章搭建了一下ssm+redis整合,特别记录下来以便以后可以查询使用,有什么不足请大牛们提点 项目架构 1.pom.xml < ...

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

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

  6. SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!

    往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...

  7. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  8. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  9. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

随机推荐

  1. C#基础加强(5)之装箱与拆箱

    定义 装箱:将值类型赋值给 Object 类型变量时,就是装箱操作,即包装为 Object 对象. 因为值类型都是 ValueType 类型,而 ValueType 页继承了 Object(CLR 内 ...

  2. 如何从零开始系统化学习视觉SLAM?

    由于显示格式问题,建议阅读原文:如何从零开始系统化学习视觉SLAM? 什么是SLAM? SLAM是 Simultaneous Localization And Mapping的 英文首字母组合,一般翻 ...

  3. FB面经prepare: 3 Window Max Sum

    Given a num array, find a window of size k, that has a maximum sum of the k entries. follow-up: Find ...

  4. 关于git的认证方式

    之前对github的使用,形成了两种观点.就是有两种url的模式,一种是http或https的,另一种是git专属的.然后git专属的url方式可以配置公钥认证,http(s)的则需要输入密码. 近期 ...

  5. puppeteer(六)启动参数——浏览器扩展应用的添加及应用

    前言 最近再做浏览器的自动化,首页是定制化的,是通过extension(扩展)实现的,由于通过puppeteer默认是以无参(即首次以干净的环境)运行的,导致登录页无法正常显示,首先想当然是直接找扩展 ...

  6. 304. Range Sum Query 2D - Immutable(动态规划)

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. 303. Range Sum Query - Immutable(动态规划)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  8. An owner of this repository has limited the ability to open a pull request to users that are collaborators on this repository.

    git 无法发起:pull request,提示:An owner of this repository has limited the ability to open a pull request ...

  9. Vfox数据库导出EXCEL,含有备注型子段

    1. 选择菜单“数据”-> “自其他来源”->“来自 Microsoft Query ”. 2. 在出来的“选择数据源” 里面双击第一个选项“<新数据源>”会出来一个“创建新数 ...

  10. JS 页面表格的操作

    var showObj = null;var arr = [ ['编号','姓名','性别','年龄','备注','操作'], ['1','lisi','nan','12','66666'], ['2 ...