001    /**
002     * Copyright (c) 2000-2013 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.keypool;
016    
017    import com.liferay.portal.kernel.cache.CacheListener;
018    import com.liferay.portal.kernel.cache.CacheListenerScope;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    
021    import java.io.Serializable;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    
027    /**
028     * @author Edward Han
029     * @author Brian Wing Shun Chan
030     */
031    public class MultiVMKeyPoolPortalCache implements PortalCache {
032    
033            public MultiVMKeyPoolPortalCache(
034                    PortalCache clusterPortalCache, PortalCache localPortalCache) {
035    
036                    _clusterPortalCache = clusterPortalCache;
037                    _localPortalCache = localPortalCache;
038            }
039    
040            @Override
041            public void destroy() {
042            }
043    
044            @Override
045            public Collection<Object> get(Collection<Serializable> keys) {
046                    List<Object> values = new ArrayList<Object>(keys.size());
047    
048                    for (Serializable key : keys) {
049                            values.add(get(key));
050                    }
051    
052                    return values;
053            }
054    
055            @Override
056            public Object get(Serializable key) {
057                    if (key == null) {
058                            return null;
059                    }
060    
061                    return _localPortalCache.get(key);
062            }
063    
064            @Override
065            public String getName() {
066                    return _clusterPortalCache.getName();
067            }
068    
069            @Override
070            public void put(Serializable key, Object obj) {
071                    _clusterPortalCache.put(key, key);
072    
073                    _localPortalCache.put(key, obj);
074            }
075    
076            @Override
077            public void put(Serializable key, Object obj, int timeToLive) {
078                    _clusterPortalCache.put(key, key, timeToLive);
079    
080                    _localPortalCache.put(key, obj, timeToLive);
081            }
082    
083            @Override
084            public void put(Serializable key, Serializable obj) {
085                    _clusterPortalCache.put(key, key);
086    
087                    _localPortalCache.put(key, obj);
088            }
089    
090            @Override
091            public void put(Serializable key, Serializable obj, int timeToLive) {
092                    _clusterPortalCache.put(key, key, timeToLive);
093    
094                    _localPortalCache.put(key, obj, timeToLive);
095            }
096    
097            @Override
098            public void registerCacheListener(CacheListener cacheListener) {
099                    _clusterPortalCache.registerCacheListener(cacheListener);
100            }
101    
102            @Override
103            public void registerCacheListener(
104                    CacheListener cacheListener, CacheListenerScope cacheListenerScope) {
105    
106                    _clusterPortalCache.registerCacheListener(
107                            cacheListener, cacheListenerScope);
108            }
109    
110            @Override
111            public void remove(Serializable key) {
112                    _clusterPortalCache.remove(key);
113                    _localPortalCache.remove(key);
114            }
115    
116            @Override
117            public void removeAll() {
118                    _clusterPortalCache.removeAll();
119                    _localPortalCache.removeAll();
120            }
121    
122            @Override
123            public void unregisterCacheListener(CacheListener cacheListener) {
124                    _clusterPortalCache.unregisterCacheListener(cacheListener);
125            }
126    
127            @Override
128            public void unregisterCacheListeners() {
129                    _clusterPortalCache.unregisterCacheListeners();
130            }
131    
132            private PortalCache _clusterPortalCache;
133            private PortalCache _localPortalCache;
134    
135    }