001    /**
002     * Copyright (c) 2000-present 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.AbstractPortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    
020    import java.io.Serializable;
021    
022    import java.util.List;
023    
024    import net.sf.ehcache.Ehcache;
025    import net.sf.ehcache.Element;
026    import net.sf.ehcache.event.NotificationScope;
027    import net.sf.ehcache.event.RegisteredEventListeners;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Edward Han
032     * @author Shuyang Zhou
033     */
034    public class EhcachePortalCache<K extends Serializable, V>
035            extends AbstractPortalCache<K, V> {
036    
037            public EhcachePortalCache(
038                    PortalCacheManager<K, V> portalCacheManager, Ehcache ehcache) {
039    
040                    _portalCacheManager = portalCacheManager;
041                    this.ehcache = ehcache;
042    
043                    RegisteredEventListeners registeredEventListeners =
044                            ehcache.getCacheEventNotificationService();
045    
046                    registeredEventListeners.registerListener(
047                            new PortalCacheCacheEventListener<K, V>(
048                                    aggregatedCacheListener, this),
049                            NotificationScope.ALL);
050            }
051    
052            @Override
053            public List<K> getKeys() {
054                    return ehcache.getKeys();
055            }
056    
057            @Override
058            public String getName() {
059                    return ehcache.getName();
060            }
061    
062            @Override
063            public PortalCacheManager<K, V> getPortalCacheManager() {
064                    return _portalCacheManager;
065            }
066    
067            @Override
068            public void removeAll() {
069                    ehcache.removeAll();
070            }
071    
072            @Override
073            protected V doGet(K key) {
074                    Element element = ehcache.get(key);
075    
076                    if (element == null) {
077                            return null;
078                    }
079    
080                    return (V)element.getObjectValue();
081            }
082    
083            @Override
084            protected void doPut(K key, V value, int timeToLive, boolean quiet) {
085                    Element element = new Element(key, value);
086    
087                    if (timeToLive != DEFAULT_TIME_TO_LIVE) {
088                            element.setTimeToLive(timeToLive);
089                    }
090    
091                    if (quiet) {
092                            ehcache.putQuiet(element);
093                    }
094                    else {
095                            ehcache.put(element);
096                    }
097            }
098    
099            @Override
100            protected V doPutIfAbsent(K key, V value, int timeToLive) {
101                    Element element = new Element(key, value);
102    
103                    if (timeToLive != DEFAULT_TIME_TO_LIVE) {
104                            element.setTimeToLive(timeToLive);
105                    }
106    
107                    Element oldElement = ehcache.putIfAbsent(element);
108    
109                    if (oldElement == null) {
110                            return null;
111                    }
112    
113                    return (V)oldElement.getObjectValue();
114            }
115    
116            @Override
117            protected void doRemove(K key) {
118                    ehcache.remove(key);
119            }
120    
121            @Override
122            protected boolean doRemove(K key, V value) {
123                    Element element = new Element(key, value);
124    
125                    return ehcache.removeElement(element);
126            }
127    
128            @Override
129            protected V doReplace(K key, V value, int timeToLive) {
130                    Element element = new Element(key, value);
131    
132                    if (timeToLive != DEFAULT_TIME_TO_LIVE) {
133                            element.setTimeToLive(timeToLive);
134                    }
135    
136                    Element oldElement = ehcache.replace(element);
137    
138                    if (oldElement == null) {
139                            return null;
140                    }
141    
142                    return (V)oldElement.getObjectValue();
143            }
144    
145            @Override
146            protected boolean doReplace(K key, V oldValue, V newValue, int timeToLive) {
147                    Element oldElement = new Element(key, oldValue);
148    
149                    Element newElement = new Element(key, newValue);
150    
151                    if (timeToLive != DEFAULT_TIME_TO_LIVE) {
152                            newElement.setTimeToLive(timeToLive);
153                    }
154    
155                    return ehcache.replace(oldElement, newElement);
156            }
157    
158            protected Ehcache ehcache;
159    
160            private final PortalCacheManager<K, V> _portalCacheManager;
161    
162    }