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.spring.hibernate;
022:
023: import com.liferay.portal.util.PropsUtil;
024:
025: import com.opensymphony.oscache.base.CacheEntry;
026: import com.opensymphony.oscache.base.NeedsRefreshException;
027: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
028:
029: import java.util.Map;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import org.hibernate.cache.Cache;
035: import org.hibernate.cache.CacheException;
036: import org.hibernate.cache.Timestamper;
037:
038: /**
039: * <a href="OSCache.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Mathias Bogaert
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class OSCache implements Cache {
046:
047: public OSCache(int refreshPeriod, String cron, String region) {
048: _refreshPeriod = refreshPeriod;
049: _cron = cron;
050: _regionName = region;
051: _regionGroups = new String[] { region };
052: }
053:
054: public void clear() throws CacheException {
055: _cache.flushGroup(_regionName);
056: }
057:
058: public void destroy() throws CacheException {
059: synchronized (_cache) {
060: _cache.destroy();
061: }
062: }
063:
064: public Object get(Object key) throws CacheException {
065: String keyString = _encodeKey(key);
066:
067: try {
068: return _cache
069: .getFromCache(keyString, _refreshPeriod, _cron);
070: } catch (NeedsRefreshException nre) {
071: _cache.cancelUpdate(keyString);
072:
073: return null;
074: }
075: }
076:
077: public long getElementCountOnDisk() {
078: return -1;
079: }
080:
081: public long getElementCountInMemory() {
082: return -1;
083: }
084:
085: public String getRegionName() {
086: return _regionName;
087: }
088:
089: public long getSizeInMemory() {
090: return -1;
091: }
092:
093: public int getTimeout() {
094: return CacheEntry.INDEFINITE_EXPIRY;
095: }
096:
097: public void lock(Object key) throws CacheException {
098: }
099:
100: public long nextTimestamp() {
101: return Timestamper.next();
102: }
103:
104: public void put(Object key, Object value) throws CacheException {
105: _cache.putInCache(_encodeKey(key), value, _regionGroups);
106: }
107:
108: public Object read(Object key) throws CacheException {
109: return get(key);
110: }
111:
112: public void remove(Object key) throws CacheException {
113: _cache.flushEntry(_encodeKey(key));
114: }
115:
116: public Map toMap() {
117: return null;
118: }
119:
120: public void unlock(Object key) throws CacheException {
121: }
122:
123: public void update(Object key, Object value) throws CacheException {
124: _cache.flushEntry(_encodeKey(key));
125:
126: put(key, value);
127: }
128:
129: private String _encodeKey(Object key) {
130: String keyString = String.valueOf(key);
131:
132: if (_log.isDebugEnabled()) {
133: _log.debug("Key " + keyString);
134: }
135:
136: return keyString;
137: }
138:
139: private static Log _log = LogFactory.getLog(OSCache.class);
140:
141: private static GeneralCacheAdministrator _cache = new GeneralCacheAdministrator(
142: PropsUtil.getProperties());
143:
144: private int _refreshPeriod;
145: private String _cron;
146: private String _regionName;
147: private String[] _regionGroups;
148:
149: }
|