defaultcachemanager.java

来自「采用tapestry的简单OA系统」· Java 代码 · 共 50 行

JAVA
50
字号
package com.ejsun.entapps.core.cache;

import java.io.IOException;

import net.sf.ehcache.Cache;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author	Quake Wang
 * @since	2004-5-18
 * @version $Revision: 1.1 $
 * 
 **/
public class DefaultCacheManager implements InitializingBean, CacheManager {
	private static final Log log = LogFactory.getLog(CacheInterceptor.class);
	private net.sf.ehcache.CacheManager ehcacheManager;

	public void afterPropertiesSet() throws Exception {
		ehcacheManager = net.sf.ehcache.CacheManager.create();
	}

	public Cache getCache(String cacheName) {
		if(!ehcacheManager.cacheExists(cacheName)){
			log.warn("Can not find predefined cache: " + cacheName + ", create the default one.");
			try {
				ehcacheManager.addCache(cacheName);
			} catch (Exception e) {
				log.error(e);
				throw new CacheException(e.getMessage());	
			} 
		}
		return ehcacheManager.getCache(cacheName);
	}

	public void flushCaches() {
		for (int i = 0; i < ehcacheManager.getCacheNames().length; i++) {
			String cacheName = ehcacheManager.getCacheNames()[i];
			try {
				ehcacheManager.getCache(cacheName).removeAll();
			} catch (IOException e) {
				log.error("Could not flush cache: " + e, e);
			}
		}
	}

}

⌨️ 快捷键说明

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