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.servlet.filters.cache;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.util.servlet.filters.CacheResponseData;
023    
024    /**
025     * @author Alexander Chow
026     * @author Michael Young
027     */
028    public class CacheUtil {
029    
030            public static String CACHE_NAME = CacheUtil.class.getName();
031    
032            public static void clearCache() {
033                    _portalCache.removeAll();
034            }
035    
036            public static void clearCache(long companyId) {
037                    clearCache();
038            }
039    
040            public static CacheResponseData getCacheResponseData(
041                    long companyId, String key) {
042    
043                    if (Validator.isNull(key)) {
044                            return null;
045                    }
046    
047                    key = _encodeKey(companyId, key);
048    
049                    CacheResponseData data = (CacheResponseData)_portalCache.get(key);
050    
051                    return data;
052            }
053    
054            public static void putCacheResponseData(
055                    long companyId, String key, CacheResponseData data) {
056    
057                    if (data != null) {
058                            key = _encodeKey(companyId, key);
059    
060                            _portalCache.put(key, data);
061                    }
062            }
063    
064            private static String _encodeKey(long companyId, String key) {
065                    StringBundler sb = new StringBundler(5);
066    
067                    sb.append(CACHE_NAME);
068                    sb.append(StringPool.POUND);
069                    sb.append(companyId);
070                    sb.append(StringPool.POUND);
071                    sb.append(key);
072    
073                    return sb.toString();
074            }
075    
076            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
077                    CACHE_NAME);
078    
079    }