001    /**
002     * Copyright (c) 2000-2011 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.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class MemoryPortalCacheManager implements PortalCacheManager {
027    
028            public void afterPropertiesSet() {
029                    _portalCaches = new ConcurrentHashMap<String, PortalCache>(
030                            _cacheManagerInitialCapacity);
031            }
032    
033            public void clearAll() {
034                    _portalCaches.clear();
035            }
036    
037            public PortalCache getCache(String name) {
038                    return getCache(name, false);
039            }
040    
041            public PortalCache getCache(String name, boolean blocking) {
042                    PortalCache portalCache = _portalCaches.get(name);
043    
044                    if (portalCache == null) {
045                            portalCache = new MemoryPortalCache(_cacheInitialCapacity);
046    
047                            portalCache.setDebug(_debug);
048    
049                            _portalCaches.put(name, portalCache);
050                    }
051    
052                    return portalCache;
053            }
054    
055            public void removeCache(String name) {
056                    _portalCaches.remove(name);
057            }
058    
059            public void setCacheInitialCapacity(int cacheInitialCapacity) {
060                    _cacheInitialCapacity = cacheInitialCapacity;
061            }
062    
063            public void setCacheManagerInitialCapacity(
064                    int cacheManagerInitialCapacity) {
065    
066                    _cacheManagerInitialCapacity = cacheManagerInitialCapacity;
067            }
068    
069            public void setDebug(boolean debug) {
070                    _debug = debug;
071            }
072    
073            private int _cacheInitialCapacity = 10000;
074            private int _cacheManagerInitialCapacity = 10000;
075            private boolean _debug;
076            private Map<String, PortalCache> _portalCaches;
077    
078    }