001
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
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 }