1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.spi.config.cache.infinispan;
17
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.locks.Lock;
20 import java.util.concurrent.locks.ReentrantReadWriteLock;
21 import java.util.logging.Logger;
22
23 import de.smartics.properties.spi.config.cache.CompoundKey;
24 import de.smartics.properties.spi.config.cache.UnawareCache;
25 import de.smartics.util.lang.StaticAnalysis;
26
27
28
29
30
31
32
33 public final class InfinispanCompoundKeyDelegatingCache<K, V> implements
34 UnawareCache<K, V>
35 {
36
37
38
39
40
41 @SuppressWarnings("unused")
42 private final Logger log = Logger
43 .getLogger(InfinispanCompoundKeyDelegatingCache.class.getName());
44
45
46
47
48
49
50 private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
51
52
53
54
55
56
57 private final Lock readLock = lock.readLock();
58
59
60
61
62
63
64 private final Lock writeLock = lock.writeLock();
65
66
67
68
69 @SuppressWarnings(StaticAnalysis.RAWTYPES)
70 private final org.infinispan.Cache cache;
71
72
73
74
75 private final String cacheName;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public InfinispanCompoundKeyDelegatingCache(final String cacheName,
92 @SuppressWarnings("rawtypes") final org.infinispan.Cache cache)
93 {
94 this.cacheName = cacheName;
95 this.cache = cache;
96 }
97
98
99
100
101
102
103
104
105
106
107
108 @SuppressWarnings(StaticAnalysis.UNCHECKED)
109 @Override
110 public V get(final Object key)
111 {
112 readLock.lock();
113 try
114 {
115 final CompoundKey compoundKey = new CompoundKey(cacheName, key);
116 final String compoundKeyString = compoundKey.toString();
117 return (V) cache.get(compoundKeyString);
118 }
119 finally
120 {
121 readLock.unlock();
122 }
123 }
124
125 @SuppressWarnings({ StaticAnalysis.UNCHECKED })
126 @Override
127 public void put(final K key, final V value, final long interval,
128 final TimeUnit unit)
129 {
130 writeLock.lock();
131 try
132 {
133 final CompoundKey compoundKey = new CompoundKey(cacheName, key);
134 final String compoundKeyString = compoundKey.toString();
135 cache.put(compoundKeyString, value, interval, unit);
136 }
137 finally
138 {
139 writeLock.unlock();
140 }
141 }
142
143 @SuppressWarnings({ StaticAnalysis.UNCHECKED })
144 @Override
145 public void put(final K key, final V value)
146 {
147 writeLock.lock();
148 try
149 {
150 final CompoundKey compoundKey = new CompoundKey(cacheName, key);
151 final String compoundKeyString = compoundKey.toString();
152 cache.put(compoundKeyString, value);
153 }
154 finally
155 {
156 writeLock.unlock();
157 }
158 }
159
160 @SuppressWarnings({ StaticAnalysis.UNCHECKED })
161 @Override
162
163 public void put(final K key, final V value, final int interval,
164 final TimeUnit unit, final int maxIdleTime,
165 final TimeUnit maxIdleTimeTimeUnit)
166 {
167 writeLock.lock();
168 try
169 {
170 final CompoundKey compoundKey = new CompoundKey(cacheName, key);
171 final String compoundKeyString = compoundKey.toString();
172 cache.put(compoundKeyString, value, interval, unit, maxIdleTime,
173 maxIdleTimeTimeUnit);
174 }
175 finally
176 {
177 writeLock.unlock();
178 }
179 }
180
181
182
183 @SuppressWarnings({ StaticAnalysis.UNCHECKED })
184 @Override
185 public V remove(final Object key)
186 {
187 writeLock.lock();
188 try
189 {
190 final CompoundKey compoundKey = new CompoundKey(cacheName, key);
191 final String compoundKeyString = compoundKey.toString();
192 return (V) cache.remove(compoundKeyString);
193 }
194 finally
195 {
196 writeLock.unlock();
197 }
198 }
199
200
201
202 }