001    /**
002     * Copyright (c) 2000-2012 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.memory;
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.concurrent.ConcurrentHashSet;
021    
022    import java.io.Serializable;
023    
024    import java.util.ArrayList;
025    import java.util.Collection;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Edward Han
034     * @author Shuyang Zhou
035     */
036    public class MemoryPortalCache<K extends Serializable, V>
037            implements PortalCache<K, V> {
038    
039            public MemoryPortalCache(String name, int initialCapacity) {
040                    _name = name;
041                    _map = new ConcurrentHashMap<K, V>(initialCapacity);
042            }
043    
044            public void destroy() {
045                    removeAll();
046    
047                    _cacheListeners = null;
048                    _map = null;
049                    _name = null;
050            }
051    
052            public Collection<V> get(Collection<K> keys) {
053                    List<V> values = new ArrayList<V>(keys.size());
054    
055                    for (K key : keys) {
056                            values.add(get(key));
057                    }
058    
059                    return values;
060            }
061    
062            public V get(K key) {
063                    return _map.get(key);
064            }
065    
066            public String getName() {
067                    return _name;
068            }
069    
070            public void put(K key, V value) {
071                    V oldValue = _map.put(key, value);
072    
073                    notifyPutEvents(key, value, oldValue != null);
074            }
075    
076            public void put(K key, V value, int timeToLive) {
077                    V oldValue = _map.put(key, value);
078    
079                    notifyPutEvents(key, value, oldValue != null);
080            }
081    
082            public void registerCacheListener(CacheListener<K, V> cacheListener) {
083                    _cacheListeners.add(cacheListener);
084            }
085    
086            public void registerCacheListener(
087                    CacheListener<K, V> cacheListener,
088                    CacheListenerScope cacheListenerScope) {
089    
090                    registerCacheListener(cacheListener);
091            }
092    
093            public void remove(K key) {
094                    V value = _map.remove(key);
095    
096                    for (CacheListener<K, V> cacheListener : _cacheListeners) {
097                            cacheListener.notifyEntryRemoved(this, key, value);
098                    }
099            }
100    
101            public void removeAll() {
102                    _map.clear();
103    
104                    for (CacheListener<K, V> cacheListener : _cacheListeners) {
105                            cacheListener.notifyRemoveAll(this);
106                    }
107            }
108    
109            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
110                    _cacheListeners.remove(cacheListener);
111            }
112    
113            public void unregisterCacheListeners() {
114                    _cacheListeners.clear();
115            }
116    
117            protected void notifyPutEvents(K key, V value, boolean updated) {
118                    if (updated) {
119                            for (CacheListener<K, V> cacheListener : _cacheListeners) {
120                                    cacheListener.notifyEntryUpdated(this, key, value);
121                            }
122                    }
123                    else {
124                            for (CacheListener<K, V> cacheListener : _cacheListeners) {
125                                    cacheListener.notifyEntryPut(this, key, value);
126                            }
127                    }
128            }
129    
130            private Set<CacheListener<K, V>> _cacheListeners =
131                    new ConcurrentHashSet<CacheListener<K, V>>();
132            private Map<K, V> _map;
133            private String _name;
134    
135    }