GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

executeAndCache(ProceedingJoinPoint,Cache)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
crap 2
rs 9.65
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