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.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.util.CollectionFactory;
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 groups, String groupKey, PortalCache portalCache) {
56  
57          if (!groups.containsKey(groupKey)) {
58              return;
59          }
60  
61          Set groupKeys = (Set)groups.get(groupKey);
62  
63          String[] keys = (String[])groupKeys.toArray(new String[0]);
64  
65          for (int i = 0; i < keys.length; i++) {
66              String key = keys[i];
67  
68              // The functionality here pretty much mimics OSCache groups. It is
69              // not necessary to remove the keys in dependent groups because they
70              // will be cleared when the group itself is cleared, resulting in a
71              // performance boost.
72  
73              portalCache.remove(key);
74          }
75  
76          groupKeys.clear();
77      }
78  
79      public Object get(String name, String key) {
80          PortalCache portalCache = getCache(name);
81  
82          return get(portalCache, key);
83      }
84  
85      public Object get(PortalCache portalCache, String key) {
86          return portalCache.get(key);
87      }
88  
89      public PortalCache getCache(String name) {
90          return _portalCacheManager.getCache(name);
91      }
92  
93      public void put(String name, String key, Object obj) {
94          PortalCache portalCache = getCache(name);
95  
96          put(portalCache, key, obj);
97      }
98  
99      public void put(PortalCache portalCache, String key, Object obj) {
100         portalCache.put(key, obj);
101     }
102 
103     public void put(
104         PortalCache portalCache, String key, Map groups, String groupKey,
105         Object obj) {
106 
107         put(portalCache, key, obj);
108 
109         updateGroup(groups, groupKey, key);
110     }
111 
112     public void put(String name, String key, Serializable obj) {
113         PortalCache portalCache = getCache(name);
114 
115         put(portalCache, key, obj);
116     }
117 
118     public void put(PortalCache portalCache, String key, Serializable obj) {
119         portalCache.put(key, obj);
120     }
121 
122     public void put(
123         PortalCache portalCache, String key, Map groups, String groupKey,
124         Serializable obj) {
125 
126         put(portalCache, key, obj);
127 
128         updateGroup(groups, groupKey, key);
129     }
130 
131     public void remove(String name, String key) {
132         PortalCache portalCache = getCache(name);
133 
134         remove(portalCache, key);
135     }
136 
137     public void remove(PortalCache portalCache, String key) {
138         portalCache.remove(key);
139     }
140 
141     public void setPortalCacheManager(PortalCacheManager portalCacheManager) {
142         _portalCacheManager = portalCacheManager;
143     }
144 
145     public void updateGroup(Map groups, String groupKey, String key) {
146         Set groupKeys = null;
147 
148         if (groups.containsKey(groupKey)) {
149             groupKeys = (Set)groups.get(groupKey);
150         }
151         else {
152             groupKeys = CollectionFactory.getSyncHashSet();
153 
154             groups.put(groupKey, groupKeys);
155         }
156 
157         groupKeys.add(key);
158     }
159 
160     private PortalCacheManager _portalCacheManager;
161 
162 }