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