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