Skip to content
Snippets Groups Projects

Update recorder to use standard cache and to better match keys

Files
8
@@ -46,24 +46,24 @@ import io.quarkus.cache.CaffeineCache;
public abstract class CacheRecorder<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(CacheRecorder.class);
@ConfigProperty(name = "quarkus.cache.caffeine.\"record\".expire-after-write", defaultValue = "P1H")
@ConfigProperty(name = "quarkus.cache.caffeine.\"default\".expire-after-write", defaultValue = "P1H")
Duration recorderTTL;
@Inject
@CacheName("record")
@CacheName("default")
Cache cache;
@Inject
ParameterHelper paramHelper;
protected ParameterHelper paramHelper;
/**
* Generates the record and does internal tracking of the key for the request. The tracked key is only updated on
* new cache entries, and fetching will not modify this time or extend it.
* Generates the record and does internal tracking of the key for the request. The tracked key is only updated on new
* cache entries, and fetching will not modify this time or extend it.
*
* @param key the key to generate a record for
* @param context the current requests context.
* @return the generated record for the current request.
*/
@CacheResult(cacheName = "record")
@CacheResult(cacheName = "default")
public T createCacheRecord(@CacheKey String key, RequestWrapper context) {
// track the key
long ttl = System.currentTimeMillis() + recorderTTL.toMillis();
@@ -73,8 +73,8 @@ public abstract class CacheRecorder<T> {
}
/**
* Calculates the record that will be applied to following requests with the same key. This method should not be
* called directly as it breaks the flow for recording cached key entries.
* Calculates the record that will be applied to following requests with the same key. This method should not be called
* directly as it breaks the flow for recording cached key entries.
*
* @param context the current request context.
* @return the recorded data to cache
@@ -94,24 +94,24 @@ public abstract class CacheRecorder<T> {
*
* @param key the key to invalidate
*/
@CacheInvalidate(cacheName = "record")
@CacheInvalidate(cacheName = "default")
public void remove(@CacheKey String key) {
}
/**
* Does best efforts checking of whether this cache key exists for this recorder and has not expired. If a key is
* checked and is expired, the key is removed from the map.
* Does best efforts checking of whether this cache key exists for this recorder and has not expired.
*
* @param key the key to check for a recorded value.
* @return true if value exists and is current, false otherwise.
*/
public boolean hasRecorded(String key) {
return cache.as(CaffeineCache.class).keySet().contains(key);
// cache.as(CaffeineCache.class).
return cache.as(CaffeineCache.class).keySet().stream().anyMatch(k -> k instanceof String && ((String) k).equals(key));
}
/**
* Generates recorder-specific key for the request which depends on the URI rather than an object ID which is used
* in the standard data cache.
* Generates recorder-specific key for the request which depends on the URI rather than an object ID which is used in
* the standard data cache.
*
* @param context the current request context
* @return recorder-specific cache key for the request
@@ -123,8 +123,13 @@ public abstract class CacheRecorder<T> {
sb.append(uri.getPath());
}
MultivaluedMap<String, String> params = context.asMap();
paramHelper.filterUnknownParameters(params).entrySet().stream().filter(e -> !e.getValue().isEmpty())
.map(e -> e.getKey() + '=' + String.join(",", e.getValue())).forEach(s -> sb.append('|').append(s));
paramHelper
.filterUnknownParameters(params)
.entrySet()
.stream()
.filter(e -> !e.getValue().isEmpty())
.map(e -> e.getKey() + '=' + String.join(",", e.getValue()))
.forEach(s -> sb.append('|').append(s));
return sb.toString();
}
}
Loading