Use the Spring AOP Cache Method Results Set

xiaoxiao2021-03-06  63

We all know that Hibernate can use ehcache as a Second Level Cache. Mainly for Pojo's cache, and the cache read

It is written in Hibernate. It feels very flexible in practical use. I saw an introduction to cache the specified with Spring Interceptor.

Example of the results of the method, feeling very good, fully understand the power of AOP :)

First configure ehcache.xml

Next, define the EHCACHE component in the Spring configuration file

classpath: ehcache.xml < bean id = "methodCache" class = "org.springframework.cache.ehcache.EhCacheFactoryBean"> < Value> Org.taha.cache.Method_Cache

Establish our own method interceptor MethodCacheInterceptor.

MethodCacheInterceptor implements the org.aopalliance.Intercept.MethodInterceptor interface.

import java.io.Serializable; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.beans. Factory.initializingbean; / ** * interceptor, used for cache method returns the result. * * @version $ ID: MethodCacheInterceptor.java v 1.0 2004-11-28 14:57:00 ZnJQ EXP $ * @Author Znjq * / public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean {private Cache cache; / ** * sets cache name to be used * / public void setCache (Cache cache) {this .cache = cache;} / * * (non-Javadoc) * * @see org.aopalliance.intercept.MethodInterceptor # invoke (org.aopalliance.intercept.MethodInvocation) * / public Object invoke (MethodInvocation invocation) throws Throwable {String targetName = Invocation.getthis (). getClass (). getname (); string methodname = invoc . Ation.getMethod () getName (); Object [] arguments = invocation.getArguments (); Object result; String cacheKey = getCacheKey (targetName, methodName, arguments); Element element = cache.get (cacheKey); if (element = = null) {// call target / sub-interceptor result = invocation.proceed (); // cache method result element = new Element (cacheKey, (Serializable) result); cache.put (element);} return element.getValue ();

} / ** * creates cache key: targetName.methodName.argument0.argument1 ... * / private String getCacheKey (String targetName, String methodName, Object [] arguments) {StringBuffer sb = new StringBuffer (); sb.append (targetName ) .append ("."); if (arguments! = null) && (arguments.length! = 0)) {for (int i = 0; i

* methodOdone . * methodTwo > methodcachepointcut Here org.SpringFramework.Aop.support.RegexpMethodPointCutadvisor is a regular expression entry point,

The syntax of the regular expression of Perl 5, the base Jakarta ORO. (Write a document with time, study it yourself).

Org.taha.beans.mybean is a class we need to make cache processing.

MethodCachepointcut

. * Methodone . * MethodTwo

The specified mode matching method corresponds to the method in org.taha.beans.mybean. This specified 2 methods need to make cache processing.

Oh, it is so simple. This time, each time the Methodone method of Org.taha. Beans.mybean will first look up from the cache,

Secondly, the database will be queried. So I don't need to specify the annoying cache in xx.hbm.xml. You don't need to be careful in the development phase.

转载请注明原文地址:https://www.9cbs.com/read-85297.html

New Post(0)