1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.filters.cache;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.util.servlet.filters.CacheResponseData;
32  
33  import java.util.Map;
34  import java.util.Set;
35  import java.util.concurrent.ConcurrentHashMap;
36  
37  /**
38   * <a href="CacheUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Alexander Chow
41   * @author Michael Young
42   *
43   */
44  public class CacheUtil {
45  
46      public static String CACHE_NAME = CacheUtil.class.getName();
47  
48      public static void clearCache() {
49          MultiVMPoolUtil.clear(CACHE_NAME);
50      }
51  
52      public static void clearCache(long companyId) {
53          String groupKey = _encodeGroupKey(companyId);
54  
55          MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("Cleared layout cache for " + companyId);
59          }
60      }
61  
62      public static CacheResponseData getCacheResponseData(
63          long companyId, String key) {
64  
65          if (Validator.isNull(key)) {
66              return null;
67          }
68  
69          key = _encodeKey(companyId, key);
70  
71          CacheResponseData data =
72              (CacheResponseData)MultiVMPoolUtil.get(_cache, key);
73  
74          return data;
75      }
76  
77      public static void putCacheResponseData(
78          long companyId, String key, CacheResponseData data) {
79  
80          if (data != null) {
81              key = _encodeKey(companyId, key);
82  
83              String groupKey = _encodeGroupKey(companyId);
84  
85              MultiVMPoolUtil.put(_cache, key, _groups, groupKey, data);
86          }
87      }
88  
89      private static String _encodeGroupKey(long companyId) {
90          StringBuilder sb = new StringBuilder();
91  
92          sb.append(CACHE_NAME);
93          sb.append(StringPool.POUND);
94          sb.append(companyId);
95  
96          return sb.toString();
97      }
98  
99      private static String _encodeKey(long companyId, String key) {
100         StringBuilder sb = new StringBuilder();
101 
102         sb.append(CACHE_NAME);
103         sb.append(StringPool.POUND);
104         sb.append(companyId);
105         sb.append(StringPool.POUND);
106         sb.append(key);
107 
108         return sb.toString();
109     }
110 
111     private static Log _log = LogFactoryUtil.getLog(CacheUtil.class);
112 
113     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
114 
115     private static Map<String, Set<String>> _groups =
116         new ConcurrentHashMap<String, Set<String>>();
117 
118 }