1   /**
2    * Copyright (c) 2000-2008 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.layoutcache;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.util.StringMaker;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.util.servlet.filters.CacheResponseData;
31  
32  import java.util.Map;
33  import java.util.Set;
34  import java.util.concurrent.ConcurrentHashMap;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="LayoutCacheUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Alexander Chow
43   * @author Michael Young
44   *
45   */
46  public class LayoutCacheUtil {
47  
48      public static String CACHE_NAME = LayoutCacheUtil.class.getName();
49  
50      public static void clearCache() {
51          MultiVMPoolUtil.clear(CACHE_NAME);
52      }
53  
54      public static void clearCache(long companyId) {
55          String groupKey = _encodeGroupKey(companyId);
56  
57          MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
58  
59          if (_log.isInfoEnabled()) {
60              _log.info("Cleared layout cache for " + companyId);
61          }
62      }
63  
64      public static CacheResponseData getCacheResponseData(
65          long companyId, String key) {
66  
67          if (Validator.isNull(key)) {
68              return null;
69          }
70  
71          key = _encodeKey(companyId, key);
72  
73          CacheResponseData data =
74              (CacheResponseData)MultiVMPoolUtil.get(_cache, key);
75  
76          return data;
77      }
78  
79      public static void putCacheResponseData(
80          long companyId, String key, CacheResponseData data) {
81  
82          if (data != null) {
83              key = _encodeKey(companyId, key);
84  
85              String groupKey = _encodeGroupKey(companyId);
86  
87              MultiVMPoolUtil.put(_cache, key, _groups, groupKey, data);
88          }
89      }
90  
91      private static String _encodeGroupKey(long companyId) {
92          StringMaker sm = new StringMaker();
93  
94          sm.append(CACHE_NAME);
95          sm.append(StringPool.POUND);
96          sm.append(companyId);
97  
98          return sm.toString();
99      }
100 
101     private static String _encodeKey(long companyId, String key) {
102         StringMaker sm = new StringMaker();
103 
104         sm.append(CACHE_NAME);
105         sm.append(StringPool.POUND);
106         sm.append(companyId);
107         sm.append(StringPool.POUND);
108         sm.append(key);
109 
110         return sm.toString();
111     }
112 
113     private static Log _log = LogFactory.getLog(LayoutCacheUtil.class);
114 
115     private static PortalCache _cache = MultiVMPoolUtil.getCache(CACHE_NAME);
116 
117     private static Map<String, Set<String>> _groups =
118         new ConcurrentHashMap<String, Set<String>>();
119 
120 }