/** * Copyright (c) 2011-2012, James Zhan 詹波 (jfinal@126.com). * * 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 com.jfinal.plugin.ehcache; import java.util.List; import com.jfinal.log.Logger; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; /** * CacheKit. Useful tool box for EhCache. */ public class CacheKit { private static volatile CacheManager cacheManager; private static final Logger log = Logger.getLogger(CacheKit.class); static void init(CacheManager cacheManager) { CacheKit.cacheManager = cacheManager; } public static CacheManager getCacheManager() { return cacheManager; } static Cache getOrAddCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { synchronized(cacheManager) { cache = cacheManager.getCache(cacheName); if (cache == null) { log.warn("Could not find cache config [" + cacheName + "], using default."); cacheManager.addCacheIfAbsent(cacheName); cache = cacheManager.getCache(cacheName); log.debug("Cache [" + cacheName + "] started."); } } } return cache; } public static void put(String cacheName, Object key, Object value) { getOrAddCache(cacheName).put(new Element(key, value)); } @SuppressWarnings("unchecked") public static <T> T get(String cacheName, Object key) { Element element = getOrAddCache(cacheName).get(key); return element != null ? (T)element.getObjectValue() : null; } @SuppressWarnings("rawtypes") public static List getKeys(String cacheName) { return getOrAddCache(cacheName).getKeys(); } public static void remove(String cacheName, Object key) { getOrAddCache(cacheName).remove(key); } public static void removeAll(String cacheName) { getOrAddCache(cacheName).removeAll(); } @SuppressWarnings("unchecked") public static <T> T handle(String cacheName, Object key, IDataLoader dataLoader) { Object data = get(cacheName, key); if (data == null) { data = dataLoader.load(); put(cacheName, key, data); } return (T)data; } }