blob: 5852c94b25d622467469de7c3e4ea0409acd296c (
plain) (
blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package org.openslx.util;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.log4j.Logger;
public abstract class GenericDataCache<T> {
private static final Logger LOGGER = Logger.getLogger(GenericDataCache.class);
/**
* How long the cached data is valid after fetching
*/
private final int validMs;
/**
* Deadline when the cache goes invalid
*/
private long validUntil = 0;
/**
* The data being held
*/
private final AtomicReference<T> item = new AtomicReference<>();
public GenericDataCache(int validMs) {
this.validMs = validMs;
}
/**
* Get the cached object, but refresh the cache first if
* the cached instance is too old.
*
* @return
*/
public T get() {
return get(CacheMode.DEFAULT);
}
/**
* Get the cached object, using the given cache access strategy.
* ALWAYS_CACHED: Never refresh the cache, except if it has never been fetched before
* DEFAULT: Only fetch from remote if the cached value is too old
* NEVER_CACHED: Always fetch from remote. If it fails, return null
*
* @param mode Cache access strategy as described above
* @return T
*/
public T get(CacheMode mode) {
switch (mode) {
case ALWAYS_CACHED:
if (validUntil == 0)
ensureUpToDate(true);
break;
case DEFAULT:
ensureUpToDate(false);
break;
case NEVER_CACHED:
if (!ensureUpToDate(true))
return null;
break;
}
return item.get();
}
private synchronized boolean ensureUpToDate(boolean force) {
final long now = System.currentTimeMillis();
if (!force && now < validUntil)
return true;
T fetched;
try {
fetched = update();
if (fetched == null)
return false;
} catch (Exception e) {
LOGGER.warn("Could not fetch fresh data", e);
return false;
}
item.set(fetched);
validUntil = now + validMs;
return true;
}
protected abstract T update() throws Exception;
//
public static enum CacheMode {
ALWAYS_CACHED,
DEFAULT,
NEVER_CACHED
}
}
|