Spring AOP结合Redis利用自定义注解实现通用缓存
1.目的
1. 缓存: 减少与数据库的交互, 提升查询性能;
2. 降低代码的侵入性
3. 提高代码的复用性
2.业务逻辑
1. 去缓存查询;
2. 如果缓存中没有数据, 则代表缓存穿透, 则去数据库查询, 再将数据存入缓存;
3. 如果缓存中有数据, 则直接返回;
3.技术点
1. spring AOP
2. Redis缓存
4.准备操作
1. 对象必须实现serializable接口, 可以被序列化;
2. 在spring核心配置文件中实现cglib动态代理;
3. redis的Template配置为JdkSerializationRedisSerializer; 目的可以将对象序列化为字节数组
<aop:aspectj-autoproxy proxy-target-class="true">
5.spring整合redis的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.