Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011-2013, 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;
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) {
 		. = cacheManager;
 	}
 	
 	public static CacheManager getCacheManager() {
 		return ;
 	}
 	
 	static Cache getOrAddCache(String cacheName) {
 		Cache cache = .getCache(cacheName);
 		if (cache == null) {
 			synchronized() {
 				cache = .getCache(cacheName);
 				if (cache == null) {
 					.warn("Could not find cache config [" + cacheName + "], using default.");
 					cache = .getCache(cacheName);
 					.debug("Cache [" + cacheName + "] started.");
 				}
 			}
 		}
 		return cache;
 	}
 	
 	public static void put(String cacheNameObject keyObject value) {
 		getOrAddCache(cacheName).put(new Element(keyvalue));
 	}
 	
 	@SuppressWarnings("unchecked")
 	public static <T> T get(String cacheNameObject 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 cacheNameObject key) {
 		getOrAddCache(cacheName).remove(key);
 	}
 	
 	public static void removeAll(String cacheName) {
 		getOrAddCache(cacheName).removeAll();
 	}
 	
 	@SuppressWarnings("unchecked")
 	public static <T> T get(String cacheNameObject keyIDataLoader dataLoader) {
 		Object data = get(cacheNamekey);
 		if (data == null) {
 			data = dataLoader.load();
 			put(cacheNamekeydata);
 		}
 		return (T)data;
 	}
 	
 	@SuppressWarnings("unchecked")
 	public static <T> T get(String cacheNameObject keyClass<? extends IDataLoaderdataLoaderClass) {
 		Object data = get(cacheNamekey);
 		if (data == null) {
 			try {
 				IDataLoader dataLoader = dataLoaderClass.newInstance();
 				data = dataLoader.load();
 				put(cacheNamekeydata);
 			} catch (Exception e) {
 				throw new RuntimeException(e);
			}
		}
		return (T)data;
	}
New to GrepCode? Check out our FAQ X