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