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