001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }