X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fwamblee%2Fcache%2FEhCache.java;fp=src%2Forg%2Fwamblee%2Fcache%2FEhCache.java;h=4f56cd3a0fd74dc203c5aa91da49989a8b8b6dc8;hb=dd05ce755afaefa6d3c64c6febf7d242b25b36c0;hp=0000000000000000000000000000000000000000;hpb=bc8798f6191bcb9b8ed6fc20994474fafd01a0b8;p=utils diff --git a/src/org/wamblee/cache/EhCache.java b/src/org/wamblee/cache/EhCache.java new file mode 100644 index 00000000..4f56cd3a --- /dev/null +++ b/src/org/wamblee/cache/EhCache.java @@ -0,0 +1,123 @@ +/* + * Copyright 2005 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.wamblee.cache; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheException; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; + +import org.apache.log4j.Logger; +import org.wamblee.io.InputResource; + +/** + * Cache implemented on top of EhCache. + */ +public class EhCache + implements org.wamblee.cache.Cache { + + private static final Logger LOGGER = Logger.getLogger(EhCache.class); + + /** + * EH Cache manager. + */ + private CacheManager _manager; + + /** + * EH cache. + */ + private Cache _cache; + + /** + * Constructs a cache based on EHCache. + * @param aResource Resource containing the configuration file for + * EHCache. + * @param aCacheName Name of the cache to use. If a cache with this name does + * not exist, one is created based on default settings. + * @throws IOException + * @throws CacheException + */ + public EhCache(InputResource aResource, String aCacheName) + throws IOException, CacheException { + InputStream is = aResource.getInputStream(); + try { + _manager = CacheManager.create(); + _cache = _manager.getCache(aCacheName); + if (_cache == null) { + LOGGER.warn("Creating cache '" + aCacheName + "' because it is not configured");; + _manager.addCache(aCacheName); + _cache = _manager.getCache(aCacheName); + } + assert _cache != null; + } finally { + is.close(); + } + + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.cache.Cache#put(KeyType, ValueType) + */ + public void put(KeyType aKey, ValueType aValue) { + _cache.put(new Element(aKey, aValue)); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.cache.Cache#get(KeyType) + */ + public ValueType get(KeyType aKey) { + try { + Element element = _cache.get(aKey); + if ( element == null ) { + return null; + } + return (ValueType)element.getValue(); + } catch (CacheException e) { + throw new RuntimeException("Cache problem key = '" + aKey + "'", e); + } + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.cache.Cache#remove(KeyType) + */ + public void remove(KeyType aKey) { + _cache.remove(aKey); + } + + /* + * (non-Javadoc) + * + * @see org.wamblee.cache.Cache#clear() + */ + public void clear() { + try { + _cache.removeAll(); + } catch (IOException e) { + throw new RuntimeException("Problem removing items from cache", e); + } + } +}