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.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.ExportImportThreadLocal;
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 final String CACHE_NAME = CacheUtil.class.getName();
033    
034            public static void clearCache() {
035                    if (ExportImportThreadLocal.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                    return _portalCache.get(key);
056            }
057    
058            public static void putCacheResponseData(
059                    long companyId, String key, CacheResponseData data) {
060    
061                    if (data != null) {
062                            key = _encodeKey(companyId, key);
063    
064                            _portalCache.put(key, data);
065                    }
066            }
067    
068            private static String _encodeKey(long companyId, String key) {
069                    StringBundler sb = new StringBundler(5);
070    
071                    sb.append(CACHE_NAME);
072                    sb.append(StringPool.POUND);
073                    sb.append(StringUtil.toHexString(companyId));
074                    sb.append(StringPool.POUND);
075                    sb.append(key);
076    
077                    return sb.toString();
078            }
079    
080            private static PortalCache<String, CacheResponseData> _portalCache =
081                    MultiVMPoolUtil.getCache(CACHE_NAME);
082    
083    }