1
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.util.PropsUtil;
28 import com.liferay.util.CollectionFactory;
29
30 import java.io.Serializable;
31
32 import java.net.URL;
33
34 import java.util.Map;
35 import java.util.Set;
36
37 import net.sf.ehcache.Cache;
38 import net.sf.ehcache.CacheManager;
39 import net.sf.ehcache.Element;
40
41
48 public class MultiVMPoolImpl implements MultiVMPool {
49
50 public void clear() {
51 _cacheManager.clearAll();
52 }
53
54 public void clear(String name) {
55 PortalCache portalCache = getCache(name);
56
57 portalCache.removeAll();
58 }
59
60 public void clearGroup(
61 Map groups, String groupKey, PortalCache portalCache) {
62
63 if (!groups.containsKey(groupKey)) {
64 return;
65 }
66
67 Set groupKeys = (Set)groups.get(groupKey);
68
69 String[] keys = (String[])groupKeys.toArray(new String[0]);
70
71 for (int i = 0; i < keys.length; i++) {
72 String key = keys[i];
73
74
79 portalCache.remove(key);
80 }
81
82 groupKeys.clear();
83 }
84
85 public Object get(String name, String key) {
86 PortalCache portalCache = getCache(name);
87
88 return get(portalCache, key);
89 }
90
91 public Object get(PortalCache portalCache, String key) {
92 Element element = (Element)portalCache.get(key);
93
94 if (element == null) {
95 return null;
96 }
97 else {
98 return element.getObjectValue();
99 }
100 }
101
102 public PortalCache getCache(String name) {
103 Cache cache = _cacheManager.getCache(name);
104
105 if (cache == null) {
106 _cacheManager.addCache(name);
107
108 cache = _cacheManager.getCache(name);
109 }
110
111 return new PortalCacheImpl(cache);
112 }
113
114 public void put(String name, String key, Object obj) {
115 PortalCache portalCache = getCache(name);
116
117 put(portalCache, key, obj);
118 }
119
120 public void put(PortalCache portalCache, String key, Object obj) {
121 portalCache.put(key, obj);
122 }
123
124 public void put(
125 PortalCache portalCache, String key, Map groups, String groupKey,
126 Object obj) {
127
128 put(portalCache, key, obj);
129
130 updateGroup(groups, groupKey, key);
131 }
132
133 public void put(String name, String key, Serializable obj) {
134 PortalCache portalCache = getCache(name);
135
136 put(portalCache, key, obj);
137 }
138
139 public void put(PortalCache portalCache, String key, Serializable obj) {
140 portalCache.put(key, obj);
141 }
142
143 public void put(
144 PortalCache portalCache, String key, Map groups, String groupKey,
145 Serializable obj) {
146
147 put(portalCache, key, obj);
148
149 updateGroup(groups, groupKey, key);
150 }
151
152 public void remove(String name, String key) {
153 PortalCache portalCache = getCache(name);
154
155 remove(portalCache, key);
156 }
157
158 public void remove(PortalCache portalCache, String key) {
159 portalCache.remove(key);
160 }
161
162 public void updateGroup(Map groups, String groupKey, String key) {
163 Set groupKeys = null;
164
165 if (groups.containsKey(groupKey)) {
166 groupKeys = (Set)groups.get(groupKey);
167 }
168 else {
169 groupKeys = CollectionFactory.getSyncHashSet();
170
171 groups.put(groupKey, groupKeys);
172 }
173
174 groupKeys.add(key);
175 }
176
177 private MultiVMPoolImpl() {
178 String configLocation = PropsUtil.get(
179 PropsUtil.EHCACHE_MULTI_VM_CONFIG_LOCATION);
180
181 URL url = getClass().getResource(configLocation);
182
183 _cacheManager = new CacheManager(url);
184 }
185
186 private CacheManager _cacheManager;
187
188 }