001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.cache;
022:
023: import com.liferay.portal.kernel.cache.MultiVMPool;
024: import com.liferay.portal.kernel.cache.PortalCache;
025: import com.liferay.portal.kernel.cache.PortalCacheManager;
026: import com.liferay.util.CollectionFactory;
027:
028: import java.io.Serializable;
029:
030: import java.util.Map;
031: import java.util.Set;
032:
033: /**
034: * <a href="MultiVMPoolImpl.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: * @author Michael Young
038: *
039: */
040: public class MultiVMPoolImpl implements MultiVMPool {
041:
042: public void clear() {
043: _portalCacheManager.clearAll();
044: }
045:
046: public void clear(String name) {
047: PortalCache portalCache = getCache(name);
048:
049: portalCache.removeAll();
050: }
051:
052: public void clearGroup(Map groups, String groupKey,
053: PortalCache portalCache) {
054:
055: if (!groups.containsKey(groupKey)) {
056: return;
057: }
058:
059: Set groupKeys = (Set) groups.get(groupKey);
060:
061: String[] keys = (String[]) groupKeys.toArray(new String[0]);
062:
063: for (int i = 0; i < keys.length; i++) {
064: String key = keys[i];
065:
066: // The functionality here pretty much mimics OSCache groups. It is
067: // not necessary to remove the keys in dependent groups because they
068: // will be cleared when the group itself is cleared, resulting in a
069: // performance boost.
070:
071: portalCache.remove(key);
072: }
073:
074: groupKeys.clear();
075: }
076:
077: public Object get(String name, String key) {
078: PortalCache portalCache = getCache(name);
079:
080: return get(portalCache, key);
081: }
082:
083: public Object get(PortalCache portalCache, String key) {
084: return portalCache.get(key);
085: }
086:
087: public PortalCache getCache(String name) {
088: return _portalCacheManager.getCache(name);
089: }
090:
091: public void put(String name, String key, Object obj) {
092: PortalCache portalCache = getCache(name);
093:
094: put(portalCache, key, obj);
095: }
096:
097: public void put(PortalCache portalCache, String key, Object obj) {
098: portalCache.put(key, obj);
099: }
100:
101: public void put(PortalCache portalCache, String key, Map groups,
102: String groupKey, Object obj) {
103:
104: put(portalCache, key, obj);
105:
106: updateGroup(groups, groupKey, key);
107: }
108:
109: public void put(String name, String key, Serializable obj) {
110: PortalCache portalCache = getCache(name);
111:
112: put(portalCache, key, obj);
113: }
114:
115: public void put(PortalCache portalCache, String key,
116: Serializable obj) {
117: portalCache.put(key, obj);
118: }
119:
120: public void put(PortalCache portalCache, String key, Map groups,
121: String groupKey, Serializable obj) {
122:
123: put(portalCache, key, obj);
124:
125: updateGroup(groups, groupKey, key);
126: }
127:
128: public void remove(String name, String key) {
129: PortalCache portalCache = getCache(name);
130:
131: remove(portalCache, key);
132: }
133:
134: public void remove(PortalCache portalCache, String key) {
135: portalCache.remove(key);
136: }
137:
138: public void setPortalCacheManager(
139: PortalCacheManager portalCacheManager) {
140: _portalCacheManager = portalCacheManager;
141: }
142:
143: public void updateGroup(Map groups, String groupKey, String key) {
144: Set groupKeys = null;
145:
146: if (groups.containsKey(groupKey)) {
147: groupKeys = (Set) groups.get(groupKey);
148: } else {
149: groupKeys = CollectionFactory.getSyncHashSet();
150:
151: groups.put(groupKey, groupKeys);
152: }
153:
154: groupKeys.add(key);
155: }
156:
157: private PortalCacheManager _portalCacheManager;
158:
159: }
|