Skip to content

Commit

Permalink
Record tags of configs received instead of requiring all be in a setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Apr 5, 2024
1 parent 9990cd1 commit e94af5b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/main/java/jasper/component/ConfigCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ConfigCache {
Expand Down Expand Up @@ -56,6 +58,8 @@ public class ConfigCache {
@Autowired
ComponentDtoMapper dtoMapper;

Set<String> configCacheTags = ConcurrentHashMap.newKeySet();

@PostConstruct
public void init() {
if (templateRepository.findByTemplateAndOrigin("_config/server", "").isEmpty()) {
Expand All @@ -65,6 +69,7 @@ public void init() {

@CacheEvict(value = "config-cache", allEntries = true)
public void clearConfigCache() {
configCacheTags.clear();
logger.info("Cleared config cache.");
}

Expand Down Expand Up @@ -107,6 +112,7 @@ public UserDto getUser(String tag) {
@Cacheable(value = "config-cache", key = "#tag + #origin + '@' + #url")
@Transactional(readOnly = true)
public <T> T getConfig(String url, String origin, String tag, Class<T> toValueType) {
configCacheTags.add(tag);
return refRepository.findOneByUrlAndOrigin(url, origin)
.map(r -> r.getPlugin(tag, toValueType))
.orElse(objectMapper.convertValue(objectMapper.createObjectNode(), toValueType));
Expand All @@ -115,6 +121,7 @@ public <T> T getConfig(String url, String origin, String tag, Class<T> toValueTy
@Cacheable(value = "config-cache", key = "#tag + #origin")
@Transactional(readOnly = true)
public <T> List<T> getAllConfigs(String origin, String tag, Class<T> toValueType) {
configCacheTags.add(tag);
return refRepository.findAll(
RefFilter.builder()
.origin(origin)
Expand All @@ -123,6 +130,10 @@ public <T> List<T> getAllConfigs(String origin, String tag, Class<T> toValueType
.toList();
}

public boolean isConfigTag(String tag) {
return configCacheTags.contains(tag);
}

@Cacheable(value = "plugin-config-cache", key = "#tag + #origin")
@Transactional(readOnly = true)
public <T> T getPluginConfig(String tag, String origin, Class<T> toValueType) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jasper/component/delta/ClearConfigCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ClearConfigCache {
@ServiceActivator(inputChannel = "tagRxChannel")
public void handleTagUpdate(Message<String> message) {
if (isBlank(configs.security(message.getHeaders().get("origin").toString()).getMode())) return;
if (!configs.root().getCacheTags().contains((String) message.getHeaders().get("tag"))) return;
if (!configs.isConfigTag((String) message.getHeaders().get("tag"))) return;
if (clearingConfig.compareAndSet(false, true)) {
clearConfig();
} else {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/jasper/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class ServerConfig implements Serializable {
private int maxSources = 1000;
private List<String> modSeals = List.of("seal", "+seal", "_seal", "_moderated");
private List<String> editorSeals = List.of("plugin/qc");
/**
* Tags for config Refs to be cached.
*/
private List<String> cacheTags = List.of("+plugin/scrape", "+plugin/oembed");
/**
* Whitelist origins to be allowed web access.
*/
Expand Down

0 comments on commit e94af5b

Please sign in to comment.