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.cache;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPool;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.cache.PortalCacheManager;
28  import com.liferay.portal.kernel.util.ConcurrentHashSet;
29  
30  import java.io.Serializable;
31  
32  import java.util.Map;
33  import java.util.Set;
34  
35  /**
36   * <a href="MultiVMPoolImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Michael Young
40   *
41   */
42  public class MultiVMPoolImpl implements MultiVMPool {
43  
44      public void clear() {
45          _portalCacheManager.clearAll();
46      }
47  
48      public void clear(String name) {
49          PortalCache portalCache = getCache(name);
50  
51          portalCache.removeAll();
52      }
53  
54      public void clearGroup(
55          Map<String, Set<String>> groups, String groupKey,
56          PortalCache portalCache) {
57  
58          synchronized (groups) {
59              if (!groups.containsKey(groupKey)) {
60                  return;
61              }
62  
63              Set<String> groupKeys = groups.get(groupKey);
64  
65              String[] keys = groupKeys.toArray(new String[groupKeys.size()]);
66  
67              for (int i = 0; i < keys.length; i++) {
68                  String key = keys[i];
69  
70                  // The functionality here pretty much mimics OSCache groups. It
71                  // is not necessary to remove the keys in dependent groups
72                  // because they will be cleared when the group itself is
73                  // cleared, resulting in a performance boost.
74  
75                  portalCache.remove(key);
76              }
77  
78              groupKeys.clear();
79          }
80      }
81  
82      public Object get(String name, String key) {
83          PortalCache portalCache = getCache(name);
84  
85          return get(portalCache, key);
86      }
87  
88      public Object get(PortalCache portalCache, String key) {
89          return portalCache.get(key);
90      }
91  
92      public PortalCache getCache(String name) {
93          return _portalCacheManager.getCache(name);
94      }
95  
96      public void put(String name, String key, Object obj) {
97          PortalCache portalCache = getCache(name);
98  
99          put(portalCache, key, obj);
100     }
101 
102     public void put(PortalCache portalCache, String key, Object obj) {
103         portalCache.put(key, obj);
104     }
105 
106     public void put(
107         PortalCache portalCache, String key, Map<String, Set<String>> groups,
108         String groupKey, Object obj) {
109 
110         put(portalCache, key, obj);
111 
112         updateGroup(groups, groupKey, key);
113     }
114 
115     public void put(String name, String key, Serializable obj) {
116         PortalCache portalCache = getCache(name);
117 
118         put(portalCache, key, obj);
119     }
120 
121     public void put(PortalCache portalCache, String key, Serializable obj) {
122         portalCache.put(key, obj);
123     }
124 
125     public void put(
126         PortalCache portalCache, String key, Map<String, Set<String>> groups,
127         String groupKey, Serializable obj) {
128 
129         put(portalCache, key, obj);
130 
131         updateGroup(groups, groupKey, key);
132     }
133 
134     public void remove(String name, String key) {
135         PortalCache portalCache = getCache(name);
136 
137         remove(portalCache, key);
138     }
139 
140     public void remove(PortalCache portalCache, String key) {
141         portalCache.remove(key);
142     }
143 
144     public void setPortalCacheManager(PortalCacheManager portalCacheManager) {
145         _portalCacheManager = portalCacheManager;
146     }
147 
148     public void updateGroup(
149         Map<String, Set<String>> groups, String groupKey, String key) {
150 
151         synchronized (groups) {
152             Set<String> groupKeys = null;
153 
154             if (groups.containsKey(groupKey)) {
155                 groupKeys = groups.get(groupKey);
156             }
157             else {
158                 groupKeys = new ConcurrentHashSet<String>();
159 
160                 groups.put(groupKey, groupKeys);
161             }
162 
163             groupKeys.add(key);
164         }
165     }
166 
167     private PortalCacheManager _portalCacheManager;
168 
169 }