1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class OSCache implements Cache {
46  
47      public OSCache(int refreshPeriod, String cron, String region) {
48          _refreshPeriod = refreshPeriod;
49          _cron = cron;
50          _regionName = region;
51          _regionGroups = new String[] {region};
52      }
53  
54      public void clear() throws CacheException {
55          _cache.flushGroup(_regionName);
56      }
57  
58      public void destroy() throws CacheException {
59          synchronized (_cache) {
60              _cache.destroy();
61          }
62      }
63  
64      public Object get(Object key) throws CacheException {
65          String keyString = _encodeKey(key);
66  
67          try {
68              return _cache.getFromCache(keyString, _refreshPeriod, _cron);
69          }
70          catch (NeedsRefreshException nre) {
71              _cache.cancelUpdate(keyString);
72  
73              return null;
74          }
75      }
76  
77      public long getElementCountOnDisk() {
78          return -1;
79      }
80  
81      public long getElementCountInMemory() {
82          return -1;
83      }
84  
85      public String getRegionName() {
86          return _regionName;
87      }
88  
89      public long getSizeInMemory() {
90          return -1;
91      }
92  
93      public int getTimeout() {
94          return CacheEntry.INDEFINITE_EXPIRY;
95      }
96  
97      public void lock(Object key) throws CacheException {
98      }
99  
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 = LogFactoryUtil.getLog(OSCache.class);
140 
141     private static GeneralCacheAdministrator _cache =
142         new GeneralCacheAdministrator(PropsUtil.getProperties());
143 
144     private int _refreshPeriod;
145     private String _cron;
146     private String _regionName;
147     private String[] _regionGroups;
148 
149 }