1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.Validator;
22  import com.liferay.util.servlet.filters.CacheResponseData;
23  
24  /**
25   * <a href="CacheUtil.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Alexander Chow
28   * @author Michael Young
29   */
30  public class CacheUtil {
31  
32      public static String CACHE_NAME = CacheUtil.class.getName();
33  
34      public static void clearCache() {
35          _cache.removeAll();
36      }
37  
38      public static void clearCache(long companyId) {
39          clearCache();
40      }
41  
42      public static CacheResponseData getCacheResponseData(
43          long companyId, String key) {
44  
45          if (Validator.isNull(key)) {
46              return null;
47          }
48  
49          key = _encodeKey(companyId, key);
50  
51          CacheResponseData data = (CacheResponseData)_cache.get(key);
52  
53          return data;
54      }
55  
56      public static void putCacheResponseData(
57          long companyId, String key, CacheResponseData data) {
58  
59          if (data != null) {
60              key = _encodeKey(companyId, key);
61  
62              _cache.put(key, data);
63          }
64      }
65  
66      private static String _encodeKey(long companyId, String key) {
67          StringBundler sb = new StringBundler(5);
68  
69          sb.append(CACHE_NAME);
70          sb.append(StringPool.POUND);
71          sb.append(companyId);
72          sb.append(StringPool.POUND);
73          sb.append(key);
74  
75          return sb.toString();
76      }
77  
78      private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
79  
80  }