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