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.servlet.filters.layoutcache;
022:
023: import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
024: import com.liferay.portal.kernel.cache.PortalCache;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.util.CollectionFactory;
029: import com.liferay.util.servlet.filters.CacheResponseData;
030:
031: import java.util.Map;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * <a href="LayoutCacheUtil.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Alexander Chow
040: * @author Michael Young
041: *
042: */
043: public class LayoutCacheUtil {
044:
045: public static String CACHE_NAME = LayoutCacheUtil.class.getName();
046:
047: public static void clearCache() {
048: MultiVMPoolUtil.clear(CACHE_NAME);
049: }
050:
051: public static void clearCache(long companyId) {
052: String groupKey = _encodeGroupKey(companyId);
053:
054: MultiVMPoolUtil.clearGroup(_groups, groupKey, _cache);
055:
056: if (_log.isInfoEnabled()) {
057: _log.info("Cleared layout cache for " + companyId);
058: }
059: }
060:
061: public static CacheResponseData getCacheResponseData(
062: long companyId, String key) {
063:
064: if (Validator.isNull(key)) {
065: return null;
066: }
067:
068: key = _encodeKey(companyId, key);
069:
070: CacheResponseData data = (CacheResponseData) MultiVMPoolUtil
071: .get(_cache, key);
072:
073: return data;
074: }
075:
076: public static void putCacheResponseData(long companyId, String key,
077: CacheResponseData data) {
078:
079: if (data != null) {
080: key = _encodeKey(companyId, key);
081:
082: String groupKey = _encodeGroupKey(companyId);
083:
084: MultiVMPoolUtil.put(_cache, key, _groups, groupKey, data);
085: }
086: }
087:
088: private static String _encodeGroupKey(long companyId) {
089: StringMaker sm = new StringMaker();
090:
091: sm.append(CACHE_NAME);
092: sm.append(StringPool.POUND);
093: sm.append(companyId);
094:
095: return sm.toString();
096: }
097:
098: private static String _encodeKey(long companyId, String key) {
099: StringMaker sm = new StringMaker();
100:
101: sm.append(CACHE_NAME);
102: sm.append(StringPool.POUND);
103: sm.append(companyId);
104: sm.append(StringPool.POUND);
105: sm.append(key);
106:
107: return sm.toString();
108: }
109:
110: private static Log _log = LogFactory.getLog(LayoutCacheUtil.class);
111:
112: private static PortalCache _cache = MultiVMPoolUtil
113: .getCache(CACHE_NAME);
114:
115: private static Map _groups = CollectionFactory.getSyncHashMap();
116:
117: }
|