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