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