1
14
15 package com.liferay.portal.dao.orm.hibernate;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.util.PropsUtil;
20
21 import com.opensymphony.oscache.base.CacheEntry;
22 import com.opensymphony.oscache.base.NeedsRefreshException;
23 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
24
25 import java.util.Map;
26
27 import org.hibernate.cache.Cache;
28 import org.hibernate.cache.CacheException;
29 import org.hibernate.cache.Timestamper;
30
31
38 public class OSCache implements Cache {
39
40 public OSCache(int refreshPeriod, String cron, String region) {
41 _refreshPeriod = refreshPeriod;
42 _cron = cron;
43 _regionName = region;
44 _regionGroups = new String[] {region};
45 }
46
47 public void clear() throws CacheException {
48 _cache.flushGroup(_regionName);
49 }
50
51 public void destroy() throws CacheException {
52 synchronized (_cache) {
53 _cache.destroy();
54 }
55 }
56
57 public Object get(Object key) throws CacheException {
58 String keyString = _encodeKey(key);
59
60 try {
61 return _cache.getFromCache(keyString, _refreshPeriod, _cron);
62 }
63 catch (NeedsRefreshException nre) {
64 _cache.cancelUpdate(keyString);
65
66 return null;
67 }
68 }
69
70 public long getElementCountOnDisk() {
71 return -1;
72 }
73
74 public long getElementCountInMemory() {
75 return -1;
76 }
77
78 public String getRegionName() {
79 return _regionName;
80 }
81
82 public long getSizeInMemory() {
83 return -1;
84 }
85
86 public int getTimeout() {
87 return CacheEntry.INDEFINITE_EXPIRY;
88 }
89
90 public void lock(Object key) throws CacheException {
91 }
92
93 public long nextTimestamp() {
94 return Timestamper.next();
95 }
96
97 public void put(Object key, Object value) throws CacheException {
98 _cache.putInCache(_encodeKey(key), value, _regionGroups);
99 }
100
101 public Object read(Object key) throws CacheException {
102 return get(key);
103 }
104
105 public void remove(Object key) throws CacheException {
106 _cache.flushEntry(_encodeKey(key));
107 }
108
109 public Map<Object, Object> toMap() {
110 return null;
111 }
112
113 public void unlock(Object key) throws CacheException {
114 }
115
116 public void update(Object key, Object value) throws CacheException {
117 _cache.flushEntry(_encodeKey(key));
118
119 put(key, value);
120 }
121
122 private String _encodeKey(Object key) {
123 String keyString = String.valueOf(key);
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("Key " + keyString);
127 }
128
129 return keyString;
130 }
131
132 private static Log _log = LogFactoryUtil.getLog(OSCache.class);
133
134 private static GeneralCacheAdministrator _cache =
135 new GeneralCacheAdministrator(PropsUtil.getProperties());
136
137 private int _refreshPeriod;
138 private String _cron;
139 private String _regionName;
140 private String[] _regionGroups;
141
142 }