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.PortalCache;
018    import com.liferay.portal.kernel.cache.PortalCacheManager;
019    
020    import java.io.Serializable;
021    
022    import java.net.URL;
023    
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class MemoryPortalCacheManager<K extends Serializable, V>
031            implements PortalCacheManager<K, V> {
032    
033            public void afterPropertiesSet() {
034                    _portalCaches = new ConcurrentHashMap<String, PortalCache<K, V>>(
035                            _cacheManagerInitialCapacity);
036            }
037    
038            @Override
039            public void clearAll() {
040                    _portalCaches.clear();
041            }
042    
043            @Override
044            public PortalCache<K, V> getCache(String name) {
045                    return getCache(name, false);
046            }
047    
048            @Override
049            public PortalCache<K, V> getCache(String name, boolean blocking) {
050                    PortalCache<K, V> portalCache = _portalCaches.get(name);
051    
052                    if (portalCache == null) {
053                            portalCache = new MemoryPortalCache<K, V>(
054                                    name, _cacheInitialCapacity);
055    
056                            _portalCaches.put(name, portalCache);
057                    }
058    
059                    return portalCache;
060            }
061    
062            @Override
063            public void reconfigureCaches(URL configurationURL) {
064            }
065    
066            @Override
067            public void removeCache(String name) {
068                    _portalCaches.remove(name);
069            }
070    
071            public void setCacheInitialCapacity(int cacheInitialCapacity) {
072                    _cacheInitialCapacity = cacheInitialCapacity;
073            }
074    
075            public void setCacheManagerInitialCapacity(
076                    int cacheManagerInitialCapacity) {
077    
078                    _cacheManagerInitialCapacity = cacheManagerInitialCapacity;
079            }
080    
081            private int _cacheInitialCapacity = 10000;
082            private int _cacheManagerInitialCapacity = 10000;
083            private Map<String, PortalCache<K, V>> _portalCaches;
084    
085    }