1
|
|
|
package io.mcarle.sciurus.cache; |
2
|
|
|
|
3
|
|
|
import io.mcarle.sciurus.ExecutionIdentifier; |
4
|
|
|
import io.mcarle.sciurus.annotation.Cache; |
5
|
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
6
|
|
|
import org.slf4j.Logger; |
7
|
|
|
import org.slf4j.LoggerFactory; |
8
|
|
|
|
9
|
|
|
import java.io.Serializable; |
10
|
|
|
import java.time.Duration; |
11
|
|
|
|
12
|
1 |
|
class CacheAspectHandler { |
13
|
|
|
|
14
|
1 |
|
private static final Logger LOG = LoggerFactory.getLogger(CacheAspectHandler.class); |
15
|
1 |
|
private final static Object EMPTY = CustomCache.EMPTY; |
16
|
|
|
|
17
|
|
|
@SuppressWarnings({"finally", "ReturnInsideFinallyBlock"}) |
18
|
|
|
static Object executeAndCache(ProceedingJoinPoint joinPoint, Cache cache) throws Throwable { |
19
|
1 |
|
ExecutionIdentifier executionIdentifier = null; |
20
|
1 |
|
Object result = EMPTY; |
21
|
|
|
try { |
22
|
|
|
// check for any cached value |
23
|
1 |
|
executionIdentifier = new ExecutionIdentifier(joinPoint.getSignature().toLongString(), joinPoint.getArgs()); |
24
|
1 |
|
result = loadFromCache(executionIdentifier, cache); |
25
|
1 |
|
} finally { |
26
|
1 |
|
if (result != EMPTY) { |
27
|
|
|
// if cached value was found, return it |
28
|
1 |
|
return result; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// if no cached value is present, execute method |
32
|
1 |
|
result = joinPoint.proceed(); |
33
|
|
|
|
34
|
|
|
// if successfull |
35
|
|
|
try { |
36
|
|
|
// save the result into cache |
37
|
1 |
|
saveIntoCache(executionIdentifier, (Serializable) result, cache); |
38
|
1 |
|
} finally { |
39
|
|
|
// despite any errors while saving to cache, ignore them and return the result |
40
|
1 |
|
return result; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
private static Object loadFromCache(ExecutionIdentifier executionIdentifier, Cache cache) { |
47
|
1 |
|
CustomCache customCache = getCache(cache, executionIdentifier); |
48
|
1 |
|
if (customCache != null) { |
49
|
|
|
try { |
50
|
1 |
|
return customCache.get(executionIdentifier); |
51
|
1 |
|
} catch (Throwable t) { |
52
|
1 |
|
LOG.warn("Got exception from cache '{}' while reading: {}", cache.cacheName(), executionIdentifier, t); |
53
|
|
|
} |
54
|
|
|
} |
55
|
1 |
|
return EMPTY; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static void saveIntoCache(ExecutionIdentifier executionIdentifier, Serializable result, Cache cache) { |
59
|
1 |
|
CustomCache customCache = getCache(cache, executionIdentifier); |
60
|
1 |
|
if (customCache != null) { |
61
|
|
|
try { |
62
|
1 |
|
customCache.put(executionIdentifier, result, Duration.of(cache.time(), cache.unit())); |
63
|
1 |
|
} catch (Throwable t) { |
64
|
1 |
|
LOG.warn("Got exception from cache '{}' while storing: {}", cache.cacheName(), executionIdentifier, t); |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
private static CustomCache getCache(Cache cache, ExecutionIdentifier executionIdentifier) { |
70
|
|
|
try { |
71
|
1 |
|
return CacheRegister.INSTANCE.getCache(cache.cacheName()); |
72
|
1 |
|
} catch (CacheException.UnknownCache ce) { |
73
|
1 |
|
LOG.warn("No cache with name '{}' registered: {}", cache.cacheName(), executionIdentifier); |
74
|
1 |
|
} catch (CacheException.CacheSupplierReturnedNull ce) { |
75
|
1 |
|
LOG.warn("Cache returned from supplier for '{}' was null: {}", cache.cacheName(), executionIdentifier); |
76
|
1 |
|
} catch (CacheException.CacheSupplierThrewException ce) { |
77
|
1 |
|
LOG.warn("Got exception from cache supplier for '{}': {}", cache.cacheName(), executionIdentifier, ce.getCause()); |
78
|
|
|
} |
79
|
1 |
|
return null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|