001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PropsUtil;
020    
021    import com.opensymphony.oscache.base.CacheEntry;
022    import com.opensymphony.oscache.base.NeedsRefreshException;
023    import com.opensymphony.oscache.general.GeneralCacheAdministrator;
024    
025    import java.util.Map;
026    
027    import org.hibernate.cache.Cache;
028    import org.hibernate.cache.CacheException;
029    import org.hibernate.cache.Timestamper;
030    
031    /**
032     * @author         Mathias Bogaert
033     * @author         Brian Wing Shun Chan
034     * @deprecated
035     */
036    public class OSCache implements Cache {
037    
038            public OSCache(int refreshPeriod, String cron, String region) {
039                    _refreshPeriod = refreshPeriod;
040                    _cron = cron;
041                    _regionName = region;
042                    _regionGroups = new String[] {region};
043            }
044    
045            public void clear() throws CacheException {
046                    _cache.flushGroup(_regionName);
047            }
048    
049            public void destroy() throws CacheException {
050                    synchronized (_cache) {
051                            _cache.destroy();
052                    }
053            }
054    
055            public Object get(Object key) throws CacheException {
056                    String keyString = _encodeKey(key);
057    
058                    try {
059                            return _cache.getFromCache(keyString, _refreshPeriod, _cron);
060                    }
061                    catch (NeedsRefreshException nre) {
062                            _cache.cancelUpdate(keyString);
063    
064                            return null;
065                    }
066            }
067    
068            public long getElementCountOnDisk() {
069                    return -1;
070            }
071    
072            public long getElementCountInMemory() {
073                    return -1;
074            }
075    
076            public String getRegionName() {
077                    return _regionName;
078            }
079    
080            public long getSizeInMemory() {
081                    return -1;
082            }
083    
084            public int getTimeout() {
085                    return CacheEntry.INDEFINITE_EXPIRY;
086            }
087    
088            public void lock(Object key) throws CacheException {
089            }
090    
091            public long nextTimestamp() {
092                    return Timestamper.next();
093            }
094    
095            public void put(Object key, Object value) throws CacheException {
096                    _cache.putInCache(_encodeKey(key), value, _regionGroups);
097            }
098    
099            public Object read(Object key) throws CacheException {
100                    return get(key);
101            }
102    
103            public void remove(Object key) throws CacheException {
104                    _cache.flushEntry(_encodeKey(key));
105            }
106    
107            public Map<Object, Object> toMap() {
108                    return null;
109            }
110    
111            public void unlock(Object key) throws CacheException {
112            }
113    
114            public void update(Object key, Object value) throws CacheException {
115                    _cache.flushEntry(_encodeKey(key));
116    
117                    put(key, value);
118            }
119    
120            private String _encodeKey(Object key) {
121                    String keyString = String.valueOf(key);
122    
123                    if (_log.isDebugEnabled()) {
124                            _log.debug("Key " + keyString);
125                    }
126    
127                    return keyString;
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(OSCache.class);
131    
132            private static GeneralCacheAdministrator _cache =
133                    new GeneralCacheAdministrator(PropsUtil.getProperties());
134    
135            private int _refreshPeriod;
136            private String _cron;
137            private String _regionName;
138            private String[] _regionGroups;
139    
140    }