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.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            @Override
045            public void destroy() {
046                    removeAll();
047    
048                    _cacheListeners = null;
049                    _map = null;
050                    _name = null;
051            }
052    
053            @Override
054            public Collection<V> get(Collection<K> keys) {
055                    List<V> values = new ArrayList<V>(keys.size());
056    
057                    for (K key : keys) {
058                            values.add(get(key));
059                    }
060    
061                    return values;
062            }
063    
064            @Override
065            public V get(K key) {
066                    return _map.get(key);
067            }
068    
069            @Override
070            public String getName() {
071                    return _name;
072            }
073    
074            @Override
075            public void put(K key, V value) {
076                    V oldValue = _map.put(key, value);
077    
078                    notifyPutEvents(key, value, oldValue != null);
079            }
080    
081            @Override
082            public void put(K key, V value, int timeToLive) {
083                    V oldValue = _map.put(key, value);
084    
085                    notifyPutEvents(key, value, oldValue != null);
086            }
087    
088            @Override
089            public void registerCacheListener(CacheListener<K, V> cacheListener) {
090                    _cacheListeners.add(cacheListener);
091            }
092    
093            @Override
094            public void registerCacheListener(
095                    CacheListener<K, V> cacheListener,
096                    CacheListenerScope cacheListenerScope) {
097    
098                    registerCacheListener(cacheListener);
099            }
100    
101            @Override
102            public void remove(K key) {
103                    V value = _map.remove(key);
104    
105                    for (CacheListener<K, V> cacheListener : _cacheListeners) {
106                            cacheListener.notifyEntryRemoved(this, key, value);
107                    }
108            }
109    
110            @Override
111            public void removeAll() {
112                    _map.clear();
113    
114                    for (CacheListener<K, V> cacheListener : _cacheListeners) {
115                            cacheListener.notifyRemoveAll(this);
116                    }
117            }
118    
119            @Override
120            public void unregisterCacheListener(CacheListener<K, V> cacheListener) {
121                    _cacheListeners.remove(cacheListener);
122            }
123    
124            @Override
125            public void unregisterCacheListeners() {
126                    _cacheListeners.clear();
127            }
128    
129            protected void notifyPutEvents(K key, V value, boolean updated) {
130                    if (updated) {
131                            for (CacheListener<K, V> cacheListener : _cacheListeners) {
132                                    cacheListener.notifyEntryUpdated(this, key, value);
133                            }
134                    }
135                    else {
136                            for (CacheListener<K, V> cacheListener : _cacheListeners) {
137                                    cacheListener.notifyEntryPut(this, key, value);
138                            }
139                    }
140            }
141    
142            private Set<CacheListener<K, V>> _cacheListeners =
143                    new ConcurrentHashSet<CacheListener<K, V>>();
144            private Map<K, V> _map;
145            private String _name;
146    
147    }