Use Spring and EHCACHE cache results

xiaoxiao2021-03-06  14

Use Spring and EHCACHE Cache Results Oriminary address: http://opensource.atlassian.com/conflunce/spring/display/disc/caching The SULT OF METHODS USING SPRING ET EHCACHE Introduction

preface

Starting from Spring 1.1.1, EHCACHE is integrated into Spring as a general cache solution.

I will demonstrate an example of the interceptor, which can cache the results returned.

Configuring EHCACHE with Spring IoC

Configuring EHCACHE in Spring is very simple. You only need an ehcache.xml file, which is used to configure EHCACHE:

interceptor will Use the "org.taha.cache.method_cache" area cache method to return the result. Below, Spring IoC will be used to allow Beans to access this area.

classpath: ehcache.xml Org.taha.cache.method_cache Build our MethodCacheInterceptor

This interceptor implements the org.aopalliance.Intercept.MethodInterceptor interface. Once you run (Kicks-in), it first checks if the intercepting method is configured to cache. This will selectively configure the Bean method you want to cache. As long as the way the call is configured to cache, the interceptor will generate a cache key for the method and check whether the result returned by the method has cached. If you have caught, return the result of the cache, otherwise call the intercepted method again, and the cache result is for the next time.

Org.taha.interceptor.MethodcacheInterceptor

/ * * Copyright 2002-2004 the original author or authors * * Licensed under the Apache License, Version 2.0 (the "License");. * You may not use this file except in compliance with the License * You may obtain a copy. of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, . * WITHOUT WARRANTIES oR CONDITIONS OF ANY KIND, either express or implied * See the License for the specific language governing permissions and * limitations under the License * / package org.taha.interceptor;. import java.io.Serializable; import org. aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; import org.springframework.beans.factory.InitializingBean; import org .springframework.util.assert; import net.sf.ehcache.cache; import net.sf.ehcache.elemen t; / ** * @author Omar Irbouh * @since 2004.10.07 * / public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean {private static final Log logger = Logfactory.getlog (MethodCacheInterceptor.class); private cache cache; / ** * Set the cache name * / public void setche (cache) {this.cache = cache;} / ** * Check if the necessary parameters are provided.

* / Public void afterpropertiesset () throws exception {assert.notnull (cache, "a cache is required. Use setche (cache) to provide one.");} / ** * Main method * If a method can be cached to cache * method results the results must be serializable (serializable) * / public Object invoke (MethodInvocation invocation) throws Throwable {String targetName = invocation.getThis () getClass () getName ();.. String methodName = invocation.getMethod ( ) .getName (); Object [] arguments = invocation.getArguments (); Object result; logger.debug ( "looking for method result in cache"); String cacheKey = getCacheKey (targetName, methodName, arguments); Element element = cache .get (cachekey); if (element == null) {// call target / sub-interceptor logger.debug ("calling intercepted method"); result = invocation.proceed (); // cache method result logger.debug "CACHING 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 ( "." ) .append (mthodname); if (arguments! = null) && (arguments.length! = 0)) {for (int i = 0; i

Under default, all methods return results are cached (MethodNames is NULL) buffer to form the factor of cacheKey, including method parameters, including method parameters (CacheKey) Using MethodCacheInterceptor

How do I configure MethodCacheInterceptor below?

* methodOdone . * methodTwo > methodcachepointcut

Translation

Xia Yu has "Hibernate Development Guide", where he describes the EHCACHE configuration file:

If you want to cache method is FINDXXX, Then the regular expression should write this: ". * Find. *".

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

New Post(0)