1
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
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 }