lovflushinterceptor.java

来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 85 行

JAVA
85
字号
package net.java.workeffort.service.support;import java.lang.reflect.Method;import net.java.workeffort.infrastructure.cache.ICacheController;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.aop.AfterReturningAdvice;/** * Interceptor which flushes cache keys of lovs. * <p> * This implements an 'after returning' advise. When methods are invoked on a * service which modify values pertaining to a cached lov, this advise will * invalidate the cache entry for the pertinent lov. The next time the lov is * accessed no value will be available in the cache and the database will be * accessed to get the latest values. This interceptor has to be configured for * the appropriate services in Spring. * <p> * Note: The cache keys have the format: getCached*Lov (example * "getCachedDeliverableLov" ) and assumes that the service method names which * need to invalidate the lov cache have the format "insert*" or "update*" or * "delete*" (example insertDeliverable, updateDeliverable, deleteDeliverable) * @author Antony Joseph */public class LovFlushInterceptor implements AfterReturningAdvice {    private static final Log logger = LogFactory            .getLog(LovFlushInterceptor.class);    private ICacheController cacheController;    /**     * Advise which invalidates a cache entry for an lov     * @param returnObj The object returned from invoking the method on the     *            service layer     * @param method The method invoked on the service layer     * @param args The arguments to the method invoked on the service layer     * @param target The target object the above method was invoked on.     */    public void afterReturning(Object returnObj, Method method, Object[] args,            Object target) throws Throwable {        // get the part of the method name after "insert" or "update" and create        // the cache key from it.        StringBuffer cacheKey = new StringBuffer(100);        String methodSuffix = null;        if (StringUtils.substringAfter(method.getName(), "insert").length() > 0) {            methodSuffix = StringUtils.substringAfter(method.getName(),                    "insert");        }        else if (StringUtils.substringAfter(method.getName(), "update")                .length() > 0) {            methodSuffix = StringUtils.substringAfter(method.getName(),                    "update");        }        else if (StringUtils.substringAfter(method.getName(), "delete")                .length() > 0) {            methodSuffix = StringUtils.substringAfter(method.getName(),                    "delete");        }        cacheKey.append("getCached");        cacheKey.append(methodSuffix);        cacheKey.append("Lov");        if (logger.isInfoEnabled()) {            logger.info("LovFlushInterceptor.afterReturning() invoked. method="                    + method.getName() + ". Invalidating cacheKey=" + cacheKey);        }        cacheController.removeObject(cacheKey.toString());    }    /**     * @param cacheController The cacheController to set. IOC     */    public void setCacheController(ICacheController cacheController) {        this.cacheController = cacheController;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?