Skip to content

Commit a217eb2

Browse files
author
qiujiayu
committed
4.0发布
1 parent 3b95fbc commit a217eb2

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Redis 配置:
9595
<!-- 可以通过implements com.jarvis.cache.serializer.ISerializer<Object> 实现 Kryo 和 FST Serializer 工具,框架的核对不在这里,所以不提供过多的实现 -->
9696
<bean id="hessianSerializer" class="com.jarvis.cache.serializer.HessianSerializer" />
9797

98-
<bean id="cachePointCut" class="com.jarvis.cache.redis.ShardedCachePointCut" destroy-method="destroy">
98+
<bean id="cacheManager" class="com.jarvis.cache.redis.ShardedCachePointCut" destroy-method="destroy">
9999
<constructor-arg ref="autoLoadConfig" />
100100
<property name="serializer" ref="hessianSerializer" />
101101
<property name="shardedJedisPool" ref="shardedJedisPool" />
@@ -123,7 +123,7 @@ Memcache 配置:
123123
</bean>
124124

125125
<bean id="hessianSerializer" class="com.jarvis.cache.serializer.HessianSerializer" />
126-
<bean id="cachePointCut" class="com.jarvis.cache.memcache.CachePointCut" destroy-method="destroy">
126+
<bean id="cacheManager" class="com.jarvis.cache.memcache.CachePointCut" destroy-method="destroy">
127127
<constructor-arg ref="autoLoadConfig" />
128128
<property name="serializer" ref="hessianSerializer" />
129129
<property name="memcachedClient", ref="memcachedClient" />
@@ -137,12 +137,15 @@ Memcache 配置:
137137

138138
###AOP 配置:
139139

140+
<bean id="cacheInterceptor" class="com.jarvis.cache.aop.aspectj.AspectjAopInterceptor">
141+
<property name="cacheManager" ref="cacheManager" />
142+
</bean>
140143
<aop:config proxy-target-class="true">
141-
<aop:aspect ref="cachePointCut">
144+
<aop:aspect ref="cacheInterceptor">
142145
<aop:pointcut id="daoCachePointcut" expression="execution(public !void com.jarvis.cache_example.common.dao..*.*(..)) &amp;&amp; @annotation(cache)" />
143146
<aop:around pointcut-ref="daoCachePointcut" method="proceed" />
144147
</aop:aspect>
145-
<aop:aspect ref="cachePointCut" order="1000"><!-- order 参数控制 aop通知的优先级,值越小,优先级越高 ,在事务提交后删除缓存 -->
148+
<aop:aspect ref="cacheInterceptor" order="1000"><!-- order 参数控制 aop通知的优先级,值越小,优先级越高 ,在事务提交后删除缓存 -->
146149
<aop:pointcut id="deleteCachePointcut" expression="execution(* com.jarvis.cache_example.common.dao..*.*(..)) &amp;&amp; @annotation(cacheDelete)" />
147150
<aop:after-returning pointcut-ref="deleteCachePointcut" method="deleteCache" returning="retVal"/>
148151
</aop:aspect>
@@ -151,7 +154,20 @@ Memcache 配置:
151154

152155
通过Spring配置,能更好地支持,不同的数据使用不同的缓存服务器的情况。
153156

154-
***注意*** 如果需要在MyBatis Mapper中使用,则需要使用com.jarvis.cache.mybatis.CachePointCutProxy 来处理。
157+
***注意*** 如果需要在MyBatis Mapper中使用,则需按如下配置:
158+
159+
<!-- proxy-target-class=false为jdk代理,为true的话,会导致拦截不了mybatis的mapper -->
160+
<aop:config proxy-target-class="false">
161+
<!-- 拦截mybatis的mapper -->
162+
<aop:aspect ref="cacheInterceptor">
163+
<aop:pointcut id="daoCachePointcut1" expression="execution(public !void com.jarvis.cache_example.common.mapper..*.*(..))" />
164+
<aop:around pointcut-ref="daoCachePointcut1" method="checkAndProceed" />
165+
</aop:aspect>
166+
<aop:aspect ref="cacheInterceptor" order="1000"><!-- order 参数控制 aop通知的优先级,值越小,优先级越高 ,在事务提交后删除缓存 -->
167+
<aop:pointcut id="deleteCachePointcut1" expression="execution(* com.jarvis.cache_example.common.mapper..*.*(..))" />
168+
<aop:after-returning pointcut-ref="deleteCachePointcut1" method="checkAndDeleteCache" returning="retVal" />
169+
</aop:aspect>
170+
</aop:config>
155171

156172
###3. 将需要使用缓存操作的方法前增加 @Cache@CacheDelete注解(Redis为例子)
157173

@@ -530,7 +546,13 @@ web.xml配置:
530546

531547
## 更新日志
532548

533-
* ####4.0 对AOP实现可扩展
549+
* ####4.0 实现AOP的可扩展
550+
551+
受网友Rekoe 将AutoLoadCache 和 nutz整合的启发([https://github.com/Rekoe/AutoLoadCache](https://github.com/Rekoe/AutoLoadCache)),将AutoLoadCache 中的AOP相关功能进行抽取,以达到可扩展
552+
553+
* 把AOP拦截方法从AbstractCacheManager中抽取出来,并使用CacheAopProxyChain 和 DeleteCacheAopProxyChain 两个代理类进行封装拦截到的请求。
554+
* 实现了使用Aspectj进行AOP拦截:com.jarvis.cache.aop.aspectj.AspectjAopInterceptor
555+
* 升级时一定要注意配置文件的变化,可以参考[cache-example](https://github.com/qiujiayu/cache-example) 中的配置
534556

535557
* ####3.7 细节优化:
536558

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.github.qiujiayu</groupId>
44
<artifactId>autoload-cache</artifactId>
5-
<version>4.0-SNAPSHOT</version>
5+
<version>4.0</version>
66
<packaging>jar</packaging>
77
<name>AutoLoadCache</name>
88
<description>User Spring AOP and annotation to do with cache.</description>

src/main/java/com/jarvis/cache/aop/aspectj/AspectjAopInterceptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
/**
1515
* 使用Aspectj 实现AOP挂载
16+
* 注意:拦截器不能有相同名字的Method
1617
* @author jiayu.qiu
1718
*/
1819
public class AspectjAopInterceptor {
1920

2021
private AbstractCacheManager cacheManager;
2122

22-
public Object proceed1(ProceedingJoinPoint pjp) throws Throwable {
23+
public Object checkAndProceed(ProceedingJoinPoint pjp) throws Throwable {
2324
Signature signature=pjp.getSignature();
2425
MethodSignature methodSignature=(MethodSignature)signature;
2526
Method method=methodSignature.getMethod();
@@ -35,7 +36,7 @@ public Object proceed1(ProceedingJoinPoint pjp) throws Throwable {
3536
}
3637
}
3738

38-
public void deleteCache1(JoinPoint jp, Object retVal) {
39+
public void checkAndDeleteCache(JoinPoint jp, Object retVal) {
3940
Signature signature=jp.getSignature();
4041
MethodSignature methodSignature=(MethodSignature)signature;
4142
Method method=methodSignature.getMethod();

0 commit comments

Comments
 (0)