001    /**
002     * Copyright (c) 2000-2010 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.BasePortalCache;
018    
019    import java.io.Serializable;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class MemoryPortalCache extends BasePortalCache {
031    
032            public MemoryPortalCache(int initialCapacity) {
033                     _map = new ConcurrentHashMap<String, Object>(initialCapacity);
034            }
035    
036            public Collection<Object> get(Collection<String> keys) {
037                    List<Object> values = new ArrayList<Object>(keys.size());
038    
039                    for (String key : keys) {
040                            values.add(get(key));
041                    }
042    
043                    return values;
044            }
045    
046            public Object get(String key) {
047                    String processedKey = processKey(key);
048    
049                    return _map.get(processedKey);
050            }
051    
052            public void put(String key, Object obj) {
053                    String processedKey = processKey(key);
054    
055                    _map.put(processedKey, obj);
056            }
057    
058            public void put(String key, Object obj, int timeToLive) {
059                    String processedKey = processKey(key);
060    
061                    _map.put(processedKey, obj);
062            }
063    
064            public void put(String key, Serializable obj) {
065                    String processedKey = processKey(key);
066    
067                    _map.put(processedKey, obj);
068            }
069    
070            public void put(String key, Serializable obj, int timeToLive) {
071                    String processedKey = processKey(key);
072    
073                    _map.put(processedKey, obj);
074            }
075    
076            public void remove(String key) {
077                    String processedKey = processKey(key);
078    
079                    _map.remove(processedKey);
080            }
081    
082            public void removeAll() {
083                    _map.clear();
084            }
085    
086            private Map<String, Object> _map;
087    
088    }