1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.blocking; |
35 |
|
|
36 |
|
import info.magnolia.module.cache.BlockingCache; |
37 |
|
import info.magnolia.module.cache.CacheModule; |
38 |
|
import info.magnolia.cache.concurrent.LockType; |
39 |
|
import info.magnolia.cache.concurrent.ReadWriteLockSync; |
40 |
|
import info.magnolia.cache.concurrent.StripedReadWriteLockSync; |
41 |
|
import info.magnolia.module.cache.exception.MgnlLockTimeoutException; |
42 |
|
import info.magnolia.module.cache.listeners.AbstractListeningCacheWrapper; |
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 60 (60) |
Complexity: 16 |
Complexity Density: 0.42 |
|
47 |
|
public abstract class AbstractBlockingCache extends AbstractListeningCacheWrapper implements BlockingCache { |
48 |
|
|
49 |
|
private volatile int blockingTimeout; |
50 |
|
private final String name; |
51 |
|
private final StripedReadWriteLockSync cacheLockProvider = new StripedReadWriteLockSync(); |
52 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
53 |
0 |
public AbstractBlockingCache(String name, CacheModule cacheModule, Integer blockingTimeout) {... |
54 |
0 |
super(cacheModule); |
55 |
0 |
this.blockingTimeout = blockingTimeout; |
56 |
0 |
this.name = name; |
57 |
|
} |
58 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
59 |
0 |
@Override... |
60 |
|
public final Object get(Object key) { |
61 |
0 |
super.get(key); |
62 |
0 |
ReadWriteLockSync lock = getLockForKey(key); |
63 |
0 |
acquiredLockForKey(key, lock, LockType.READ); |
64 |
0 |
Object element; |
65 |
0 |
try { |
66 |
0 |
element = this.getQuiet(key); |
67 |
|
} finally { |
68 |
0 |
lock.unlock(LockType.READ); |
69 |
|
} |
70 |
0 |
if (element == null) { |
71 |
0 |
acquiredLockForKey(key, lock, LockType.WRITE); |
72 |
0 |
element = this.getQuiet(key); |
73 |
0 |
if (element != null) { |
74 |
0 |
lock.unlock(LockType.WRITE); |
75 |
|
} |
76 |
|
} |
77 |
0 |
return element; |
78 |
|
} |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
80 |
0 |
@Override... |
81 |
|
public final boolean hasElement(Object key) { |
82 |
0 |
return get(key) != null; |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
0 |
@Override... |
86 |
|
public final void put(Object key, Object value) { |
87 |
0 |
put(key, value, -1); |
88 |
|
} |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
90 |
0 |
@Override... |
91 |
|
public final void put(Object key, Object value, int timeToLiveInSeconds) { |
92 |
0 |
ReadWriteLockSync lock = getLockForKey(key); |
93 |
0 |
if (!lock.isHeldByCurrentThread(LockType.WRITE)) { |
94 |
0 |
lock.lock(LockType.WRITE); |
95 |
|
} |
96 |
0 |
try { |
97 |
0 |
if (value != null) { |
98 |
0 |
putQuiet(key, value, timeToLiveInSeconds); |
99 |
|
} |
100 |
|
} finally { |
101 |
|
|
102 |
0 |
lock.unlock(LockType.WRITE); |
103 |
|
} |
104 |
0 |
super.put(key, value, timeToLiveInSeconds); |
105 |
|
} |
106 |
|
|
107 |
|
protected abstract void putQuiet(Object key, Object value, int timeToLiveInSeconds); |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
109 |
0 |
@Override... |
110 |
|
public final void unlock(Object key) { |
111 |
0 |
ReadWriteLockSync lock = getLockForKey(key); |
112 |
0 |
if (lock.isHeldByCurrentThread(LockType.WRITE)) { |
113 |
0 |
lock.unlock(LockType.WRITE); |
114 |
|
} |
115 |
|
} |
116 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
117 |
|
@Override... |
118 |
|
public int getBlockingTimeout() { |
119 |
|
return blockingTimeout; |
120 |
|
} |
121 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
122 |
|
@Override... |
123 |
|
public String getName() { |
124 |
|
return name; |
125 |
|
} |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
127 |
0 |
private ReadWriteLockSync getLockForKey(final Object key) {... |
128 |
0 |
return cacheLockProvider.getSyncForKey(key); |
129 |
|
} |
130 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
131 |
0 |
private void acquiredLockForKey(final Object key, final ReadWriteLockSync lock, final LockType lockType) {... |
132 |
0 |
if (blockingTimeout > 0) { |
133 |
0 |
try { |
134 |
0 |
boolean acquired = lock.tryLock(lockType, blockingTimeout); |
135 |
0 |
if (!acquired) { |
136 |
0 |
StringBuilder message = new StringBuilder("Lock timeout. Waited more than ") |
137 |
|
.append(blockingTimeout) |
138 |
|
.append("ms to acquire lock for key ") |
139 |
|
.append(key).append(" on blocking cache ").append(this.getName()); |
140 |
0 |
throw new MgnlLockTimeoutException(message.toString()); |
141 |
|
} |
142 |
|
} catch (InterruptedException e) { |
143 |
0 |
throw new MgnlLockTimeoutException("Got interrupted while trying to acquire lock for key " + key, e); |
144 |
|
} |
145 |
|
} else { |
146 |
0 |
lock.lock(lockType); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
} |