1 |
| package com.opensymphony.oscache.hibernate; |
2 |
| |
3 |
| import java.util.Map; |
4 |
| |
5 |
| import org.hibernate.cache.Cache; |
6 |
| import org.hibernate.cache.CacheException; |
7 |
| import org.hibernate.cache.Timestamper; |
8 |
| |
9 |
| import com.opensymphony.oscache.base.NeedsRefreshException; |
10 |
| import com.opensymphony.oscache.general.GeneralCacheAdministrator; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| public class OSCache implements Cache { |
21 |
| |
22 |
| |
23 |
| private GeneralCacheAdministrator cache; |
24 |
| private final int refreshPeriod; |
25 |
| private final String cron; |
26 |
| private final String regionName; |
27 |
| private final String[] regionGroups; |
28 |
| |
29 |
0
| public OSCache(GeneralCacheAdministrator cache, int refreshPeriod, String cron, String region) {
|
30 |
0
| this.cache = cache;
|
31 |
0
| this.refreshPeriod = refreshPeriod;
|
32 |
0
| this.cron = cron;
|
33 |
0
| this.regionName = region;
|
34 |
0
| this.regionGroups = new String[] {region};
|
35 |
| } |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
0
| public Object get(Object key) throws CacheException {
|
41 |
0
| try {
|
42 |
0
| return cache.getFromCache( toString(key), refreshPeriod, cron );
|
43 |
| } |
44 |
| catch (NeedsRefreshException e) { |
45 |
0
| cache.cancelUpdate( toString(key) );
|
46 |
0
| return null;
|
47 |
| } |
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
0
| public void put(Object key, Object value) throws CacheException {
|
54 |
0
| cache.putInCache( toString(key), value, regionGroups );
|
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
0
| public void remove(Object key) throws CacheException {
|
61 |
0
| cache.flushEntry( toString(key) );
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
0
| public void clear() throws CacheException {
|
68 |
0
| cache.flushGroup(regionName);
|
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
0
| public void destroy() throws CacheException {
|
75 |
0
| synchronized (cache) {
|
76 |
0
| cache.destroy();
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
0
| public void lock(Object key) throws CacheException {
|
84 |
| |
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
0
| public void unlock(Object key) throws CacheException {
|
91 |
| |
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
0
| public long nextTimestamp() {
|
98 |
0
| return Timestamper.next();
|
99 |
| } |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
0
| public int getTimeout() {
|
105 |
0
| return Timestamper.ONE_MS * 60000;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public Map toMap() {
|
112 |
0
| throw new UnsupportedOperationException();
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
0
| public long getElementCountOnDisk() {
|
119 |
0
| return -1;
|
120 |
| } |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
0
| public long getElementCountInMemory() {
|
126 |
0
| return -1;
|
127 |
| } |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
0
| public long getSizeInMemory() {
|
133 |
0
| return -1;
|
134 |
| } |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
0
| public String getRegionName() {
|
140 |
0
| return regionName;
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
0
| public void update(Object key, Object value) throws CacheException {
|
147 |
0
| put(key, value);
|
148 |
| } |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
0
| public Object read(Object key) throws CacheException {
|
154 |
0
| return get(key);
|
155 |
| } |
156 |
| |
157 |
0
| private String toString(Object key) {
|
158 |
0
| return String.valueOf(key) + "." + regionName;
|
159 |
| } |
160 |
| |
161 |
| } |