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.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 final 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    }