forked from Apicurio/apicurio-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistryStorageConfigCache.java
106 lines (89 loc) · 3.55 KB
/
RegistryStorageConfigCache.java
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
95
96
97
98
99
100
101
102
103
104
105
106
package io.apicurio.registry.config;
import io.apicurio.common.apps.config.DynamicConfigPropertyDto;
import io.apicurio.common.apps.config.Info;
import io.apicurio.registry.storage.decorator.RegistryStorageDecorator;
import io.apicurio.registry.storage.decorator.RegistryStorageDecoratorBase;
import io.apicurio.registry.storage.decorator.RegistryStorageDecoratorOrderConstants;
import io.apicurio.registry.storage.error.RegistryStorageException;
import io.quarkus.scheduler.Scheduled;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.slf4j.Logger;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static io.quarkus.scheduler.Scheduled.ConcurrentExecution.SKIP;
@ApplicationScoped
public class RegistryStorageConfigCache extends RegistryStorageDecoratorBase implements RegistryStorageDecorator {
private static final DynamicConfigPropertyDto NULL_DTO = new DynamicConfigPropertyDto();
@Inject
Logger log;
@ConfigProperty(name = "apicurio.config.cache.enabled", defaultValue = "true")
@Info(category = "cache", description = "Registry cache enabled", availableSince = "2.2.2.Final")
boolean enabled;
private Map<String, DynamicConfigPropertyDto> configCache = new ConcurrentHashMap<>();
private Instant lastRefresh = null;
/**
* @see io.apicurio.registry.storage.decorator.RegistryStorageDecorator#isEnabled()
*/
@Override
public boolean isEnabled() {
return enabled;
}
/**
* @see io.apicurio.registry.storage.decorator.RegistryStorageDecorator#order()
*/
@Override
public int order() {
return RegistryStorageDecoratorOrderConstants.CONFIG_CACHE_DECORATOR;
}
/**
* @see io.apicurio.registry.storage.decorator.RegistryStorageDecorator#setConfigProperty(io.apicurio.common.apps.config.DynamicConfigPropertyDto)
*/
@Override
public void setConfigProperty(DynamicConfigPropertyDto property) throws RegistryStorageException {
super.setConfigProperty(property);
invalidateCache();
}
/**
* @see io.apicurio.registry.storage.decorator.RegistryStorageDecorator#getConfigProperty(java.lang.String)
*/
@Override
public DynamicConfigPropertyDto getConfigProperty(String propertyName) {
DynamicConfigPropertyDto propertyDto = configCache.computeIfAbsent(propertyName, (key) -> {
DynamicConfigPropertyDto dto = super.getConfigProperty(key);
if (dto == null) {
dto = NULL_DTO;
}
return dto;
});
return propertyDto == NULL_DTO ? null : propertyDto;
}
private void invalidateCache() {
configCache.clear();
}
@Scheduled(concurrentExecution = SKIP, every = "{apicurio.config.refresh.every}")
void run() {
if (!enabled) {
return;
}
try {
log.debug("Running config property refresh job at {}", Instant.now());
refresh();
} catch (Exception ex) {
log.error("Exception thrown when running config property refresh job.", ex);
}
}
private void refresh() {
Instant now = Instant.now();
if (lastRefresh != null) {
List<DynamicConfigPropertyDto> staleConfigProperties = this.getStaleConfigProperties(lastRefresh);
if (!staleConfigProperties.isEmpty()) {
invalidateCache();
}
}
lastRefresh = now;
}
}