001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.cache.ehcache;
016    
017    import com.liferay.portal.kernel.cache.BasePortalCache;
018    
019    import java.io.Serializable;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.List;
024    
025    import net.sf.ehcache.Ehcache;
026    import net.sf.ehcache.Element;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class EhcachePortalCache extends BasePortalCache {
032    
033            public EhcachePortalCache(Ehcache cache) {
034                    _cache = cache;
035            }
036    
037            public Object get(String key) {
038                    String processedKey = processKey(key);
039    
040                    Element element = _cache.get(processedKey);
041    
042                    if (element == null) {
043                            return null;
044                    }
045                    else {
046                            return element.getObjectValue();
047                    }
048            }
049    
050            public Collection<Object> get(Collection<String> keys) {
051                    List<Object> values = new ArrayList<Object>(keys.size());
052    
053                    for (String key : keys) {
054                            values.add(get(key));
055                    }
056    
057                    return values;
058            }
059    
060            public void put(String key, Object obj) {
061                    Element element = createElement(key, obj);
062    
063                    _cache.put(element);
064            }
065    
066            public void put(String key, Object obj, int timeToLive) {
067                    Element element = createElement(key, obj);
068    
069                    element.setTimeToLive(timeToLive);
070    
071                    _cache.put(element);
072            }
073    
074            public void put(String key, Serializable obj) {
075                    Element element = createElement(key, obj);
076    
077                    _cache.put(element);
078            }
079    
080            public void put(String key, Serializable obj, int timeToLive) {
081                    Element element = createElement(key, obj);
082    
083                    element.setTimeToLive(timeToLive);
084    
085                    _cache.put(element);
086            }
087    
088            public void remove(String key) {
089                    String processedKey = processKey(key);
090    
091                    _cache.remove(processedKey);
092            }
093    
094            public void removeAll() {
095                    _cache.removeAll();
096            }
097    
098            protected Element createElement(String key, Object obj) {
099                    String processedKey = processKey(key);
100    
101                    Element element = new Element(processedKey, obj);
102    
103                    return element;
104            }
105    
106            private Ehcache _cache;
107    
108    }