1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.dao.orm.hibernate;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.util.PropsUtil;
25  
26  import com.opensymphony.oscache.base.CacheEntry;
27  import com.opensymphony.oscache.base.NeedsRefreshException;
28  import com.opensymphony.oscache.general.GeneralCacheAdministrator;
29  
30  import java.util.Map;
31  
32  import org.hibernate.cache.Cache;
33  import org.hibernate.cache.CacheException;
34  import org.hibernate.cache.Timestamper;
35  
36  /**
37   * <a href="OSCache.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Mathias Bogaert
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class OSCache implements Cache {
44  
45      public OSCache(int refreshPeriod, String cron, String region) {
46          _refreshPeriod = refreshPeriod;
47          _cron = cron;
48          _regionName = region;
49          _regionGroups = new String[] {region};
50      }
51  
52      public void clear() throws CacheException {
53          _cache.flushGroup(_regionName);
54      }
55  
56      public void destroy() throws CacheException {
57          synchronized (_cache) {
58              _cache.destroy();
59          }
60      }
61  
62      public Object get(Object key) throws CacheException {
63          String keyString = _encodeKey(key);
64  
65          try {
66              return _cache.getFromCache(keyString, _refreshPeriod, _cron);
67          }
68          catch (NeedsRefreshException nre) {
69              _cache.cancelUpdate(keyString);
70  
71              return null;
72          }
73      }
74  
75      public long getElementCountOnDisk() {
76          return -1;
77      }
78  
79      public long getElementCountInMemory() {
80          return -1;
81      }
82  
83      public String getRegionName() {
84          return _regionName;
85      }
86  
87      public long getSizeInMemory() {
88          return -1;
89      }
90  
91      public int getTimeout() {
92          return CacheEntry.INDEFINITE_EXPIRY;
93      }
94  
95      public void lock(Object key) throws CacheException {
96      }
97  
98      public long nextTimestamp() {
99          return Timestamper.next();
100     }
101 
102     public void put(Object key, Object value) throws CacheException {
103         _cache.putInCache(_encodeKey(key), value, _regionGroups);
104     }
105 
106     public Object read(Object key) throws CacheException {
107         return get(key);
108     }
109 
110     public void remove(Object key) throws CacheException {
111         _cache.flushEntry(_encodeKey(key));
112     }
113 
114     public Map toMap() {
115         return null;
116     }
117 
118     public void unlock(Object key) throws CacheException {
119     }
120 
121     public void update(Object key, Object value) throws CacheException {
122         _cache.flushEntry(_encodeKey(key));
123 
124         put(key, value);
125     }
126 
127     private String _encodeKey(Object key) {
128         String keyString = String.valueOf(key);
129 
130         if (_log.isDebugEnabled()) {
131             _log.debug("Key " + keyString);
132         }
133 
134         return keyString;
135     }
136 
137     private static Log _log = LogFactoryUtil.getLog(OSCache.class);
138 
139     private static GeneralCacheAdministrator _cache =
140         new GeneralCacheAdministrator(PropsUtil.getProperties());
141 
142     private int _refreshPeriod;
143     private String _cron;
144     private String _regionName;
145     private String[] _regionGroups;
146 
147 }