Java自定义注解实现Redis自动缓存的方法
在实际开发中,可能经常会有这样的需要:从MySQL中查询一条数据(比如用户信息),此时需要将用户信息保存至Redis。
刚开始我们可能会在查询的业务逻辑之后再写一段Redis相关操作的代码,时间长了后发现这部分代码实际上仅仅做了Redis的写入动作,跟业务逻辑没有实质的联系,那么有没有什么方法能让我们省略这些重复劳动呢?
首先想到用AOP,在查询到某些数据这一切入点(Pointcut)完成我们的切面相关处理(也就是写入Redis)。那么,如何知道什么地方需要进行缓存呢,也就是什么地方需要用到AOP呢?参考数据库事务的实现用到了@Transactional,那我们也可以自定义一个注解@RedisCache,将此注解用在需要的方法上,方法的返回结果作为需要保存的信息,方法的查询参数(比如用户的id)可以用来作为key。
上面的分析考虑下来貌似可行,那么接下来就动手实践吧!
详细步骤
1.创建一个自定义注解@RedisCache
packageredis; importjava.lang.annotation.*; /** *自定义注解,结合AOP实现Redis自动缓存 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited @Documented public@interfaceRedisCache{ }
2.创建缓存写入的辅助类:RedisHelper.java,其中包含一个范型方法用于接收不同类的实例对象,以保证我们的方法能够通用。这里比较简单,直接把对象转成json,在Redis中用string保存。而且不管什么情况统统写入,实际还可以完善下具体逻辑,比如判断缓存是否已存在,缓存信息是否最新等等。
packageredis; importcom.alibaba.fastjson.JSONObject; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.data.redis.core.StringRedisTemplate; importorg.springframework.stereotype.Component; @Component publicclassRedisHelper{ @Autowired privateStringRedisTemplatestringRedisTemplate; publicvoidsaveCache(Stringkey,Tt){ Stringjson=JSONObject.toJSONString(t); stringRedisTemplate.opsForValue().set(key,json); } }
3.创建RedisCacheAspect.java,利用AOP框架AspectJ完成切面处理(用万金油环绕通知吧,按需要有取舍地使用具体某些类型的通知吧),我们这里用到了返回通知,也就是方法调用成功得到返回结果后进行切面处理动作
packageredis; importorg.aspectj.lang.ProceedingJoinPoint; importorg.aspectj.lang.annotation.Around; importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.Pointcut; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Component; @Aspect @Component publicclassRedisCacheAspect{ @Autowired privateRedisHelperredisHelper; @Pointcut("@annotation(redis.RedisCache)") publicvoidsetJoinPoint(){} //环绕通知:可以获取返回值 @Around(value="setJoinPoint()") publicObjectaroundMethod(ProceedingJoinPointproceedingJoinPoint){ Objectresult=null; try{ //前置通知 result=proceedingJoinPoint.proceed(); //返回通知 //缓存至Redis Object[]args=proceedingJoinPoint.getArgs(); //key策略:需要缓存的对象的全类名-id,如:entity.User-1 redisHelper.saveCache(result.getClass().getName()+"-"+args[0],result); }catch(Throwablee){ //异常通知 } //后置通知 returnresult; } }
4.接下来是具体业务相关的代码
UserController.java
packagecontroller; importcom.alibaba.fastjson.JSONObject; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.PathVariable; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.ResponseBody; importservice.UserService; @SuppressWarnings("unused") @Controller publicclassUserController{ @Autowired privateUserServiceuserService; @RequestMapping(value="/user/{id}",method=RequestMethod.GET,produces="application/json;charset=utf-8") @ResponseBody publicStringtest(@PathVariableLongid){ returnJSONObject.toJSONString(userService.get(id)); } }
UserService.java,其中get方法上使用了自定义注解@RedisCache
packageservice; importdao.UserDao; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Service; importredis.RedisCache; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; @Service publicclassUserServiceimplementsBaseService { @Autowired privateUserDaouserDao; publicMapadd(Useruser){ returnnull; } publicMapupdate(Useruser){ returnnull; } @RedisCache publicUserget(Longid){ return(User)userDao.get(id); } publicList query(Useruser){ List list=newArrayList (); list=userDao.query(user); returnlist; } publicMapdelete(Useruser){ returnnull; } }
5.测试
浏览器直接访问http://localhost:8080/user/1,得到返回结果
http://localhost:8080/user/1
连接Redis查看结果
127.0.0.1:6381>keysentity* 1)"entity.User-1" 127.0.0.1:6381>getentity.User-1 "{\"id\":1,\"mobile\":\"110\",\"name\":\"\xe7\x94\xa8\xe6\x88\xb71\",\"password\":\"123456\",\"username\":\"0001\"}" 127.0.0.1:6381>
好了,到此我们已经看到开头的想法验证成功了,只需要在查询的方法上加上注解@RedisCache,就自动地悄无声息地写入Redis了,是不是很方便!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。