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.transactional;
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 Shuyang Zhou
029     * @author Edward Han
030     */
031    public class TransactionalPortalCache<K extends Serializable, V>
032            implements PortalCache<K, V> {
033    
034            public TransactionalPortalCache(PortalCache<K, V> portalCache) {
035                    _portalCache = portalCache;
036            }
037    
038            @Override
039            public void destroy() {
040            }
041    
042            @Override
043            public Collection<V> get(Collection<K> keys) {
044                    List<V> values = new ArrayList<V>(keys.size());
045    
046                    for (K key : keys) {
047                            values.add(get(key));
048                    }
049    
050                    return values;
051            }
052    
053            @Override
054            public V get(K key) {
055                    V result = null;
056    
057                    if (TransactionalPortalCacheHelper.isEnabled()) {
058                            result = TransactionalPortalCacheHelper.get(_portalCache, key);
059    
060                            if (result == NULL_HOLDER) {
061                                    return null;
062                            }
063                    }
064    
065                    if (result == null) {
066                            result = _portalCache.get(key);
067                    }
068    
069                    return result;
070            }
071    
072            @Override
073            public String getName() {
074                    return _portalCache.getName();
075            }
076    
077            @Override
078            public void put(K key, V value) {
079                    if (TransactionalPortalCacheHelper.isEnabled()) {
080                            if (value == null) {
081                                    TransactionalPortalCacheHelper.put(
082                                            _portalCache, key, (V)NULL_HOLDER);
083                            }
084                            else {
085                                    TransactionalPortalCacheHelper.put(_portalCache, key, value);
086                            }
087                    }
088                    else {
089                            _portalCache.put(key, value);
090                    }
091            }
092    
093            @Override
094            public void put(K key, V value, int timeToLive) {
095                    if (TransactionalPortalCacheHelper.isEnabled()) {
096                            if (value == null) {
097                                    TransactionalPortalCacheHelper.put(
098                                            _portalCache, key, (V)NULL_HOLDER);
099                            }
100                            else {
101                                    TransactionalPortalCacheHelper.put(_portalCache, key, value);
102                            }
103                    }
104                    else {
105                            _portalCache.put(key, value, timeToLive);
106                    }
107            }
108    
109            @Override
110            public void registerCacheListener(CacheListener<K, V> cacheListener) {
111                    _portalCache.registerCacheListener(cacheListener);
112            }
113    
114            @Override
115            public void registerCacheListener(
116                    CacheListener<K, V> cacheListener,
117                    CacheListenerScope cacheListenerScope) {
118    
119                    _portalCache.registerCacheListener(cacheListener, cacheListenerScope);
120            }
121    
122            @Override
123            public void remove(K key) {
124                    if (TransactionalPortalCacheHelper.isEnabled()) {
125                            TransactionalPortalCacheHelper.put(
126                                    _portalCache, key, (V)NULL_HOLDER);
127                    }
128                    else {
129                            _portalCache.remove(key);
130                    }
131            }
132    
133            @Override
134            public void removeAll() {
135                    if (TransactionalPortalCacheHelper.isEnabled()) {
136                            TransactionalPortalCacheHelper.removeAll(_portalCache);
137                    }
138                    else {
139                            _portalCache.removeAll();
140                    }
141            }
142    
143            @Override
144            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
145                    _portalCache.unregisterCacheListener(cacheListener);
146            }
147    
148            @Override
149            public void unregisterCacheListeners() {
150                    _portalCache.unregisterCacheListeners();
151            }
152    
153            protected static Serializable NULL_HOLDER = "NULL_HOLDER";
154    
155            private PortalCache<K, V> _portalCache;
156    
157    }