1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.servlet.filters.cache;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.util.servlet.filters.CacheResponseData;
24  
25  /**
26   * <a href="CacheUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Alexander Chow
29   * @author Michael Young
30   */
31  public class CacheUtil {
32  
33      public static String CACHE_NAME = CacheUtil.class.getName();
34  
35      public static void clearCache() {
36          _portalCache.removeAll();
37      }
38  
39      public static void clearCache(long companyId) {
40          clearCache();
41      }
42  
43      public static CacheResponseData getCacheResponseData(
44          long companyId, String key) {
45  
46          if (Validator.isNull(key)) {
47              return null;
48          }
49  
50          key = _encodeKey(companyId, key);
51  
52          CacheResponseData data = (CacheResponseData)_portalCache.get(key);
53  
54          return data;
55      }
56  
57      public static void putCacheResponseData(
58          long companyId, String key, CacheResponseData data) {
59  
60          if (data != null) {
61              key = _encodeKey(companyId, key);
62  
63              _portalCache.put(key, data);
64          }
65      }
66  
67      private static String _encodeKey(long companyId, String key) {
68          StringBundler sb = new StringBundler(5);
69  
70          sb.append(CACHE_NAME);
71          sb.append(StringPool.POUND);
72          sb.append(StringUtil.toHexString(companyId));
73          sb.append(StringPool.POUND);
74          sb.append(key);
75  
76          return sb.toString();
77      }
78  
79      private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
80          CACHE_NAME);
81  
82  }