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