Skip to content

Instantly share code, notes, and snippets.

public interface NavigationRepository extends
AerospikeRepository<CachedNavigation, String> {
}
@Value
@Document(collection = "navigation_CachedNavigationMenu")
public class CachedNavigation implements Cacheable<List<LegacyNavMenuItem>> {
@Id
String id;
List<LegacyNavMenuItem> navigation;
static <R> Cache<String, R> cache(String name, MeterRegistry meterRegistry) {
Cache<String, R> cache = Caffeine.newBuilder()
.recordStats()
.expireAfterWrite(Duration.ofHours(24))
.maximumSize(6000)
.build();
CaffeineCacheMetrics.monitor(meterRegistry, cache, name);
return cache;
}
private SyncFallbackCache<Collection<SupplierUserPermission>> permissions;
private Collection<SupplierUserPermission> getPermissions(
int userId,
int supplierId) {
return permissions.get(
() -> String.format("id:%s;suId:%s", userId, supplierId),
() -> client.getUserPermissions(userId, supplierId)
.getData()
.getSupplierUserMyInformation()
private T getFromCacheOrThrow(String key, RuntimeException exception) {
var value = cache.findById(key);
value.ifPresent((v) -> {
log.info(String.format("FallbackCache[%s] got cached value", cb.getName()), v);
});
return value
.map(Cacheable::data)
.orElseThrow(() -> {
private T getAndCache(String key, Supplier<R> valueSupplier) {
/*
This has the disadvantage that the [get result, cache it] is not atomic. So if two requests try to update the same key at the same time, it might create an undetermined result.
However, two calls to the same service should return the same value, so we should be able to live with that.
This is not happening inside the compute method anymore due to problems acquiring the lock that led to significant timeouts.
*/
var result = valueSupplier.get();
if (result == null) {
public T get(Supplier<String> keySupplier, Supplier<R> valueSupplier) {
Supplier<T> decoratedSupplier = Decorators.ofSupplier(() -> getAndCache(key, valueSupplier))
.withRetry(retry)
.withCircuitBreaker(cb).decorate();
var key = keySupplier.get();
return Try.ofSupplier(decoratedSupplier)
.recover(RuntimeException.class, (exception) -> getFromCacheOrThrow(key, exception))
.get();
public class SyncFallbackCache<T, R extends Cacheable<T>> extends AbstractFallbackCache<T, R> {
public T get(Supplier<String> keySupplier, Supplier<R> valueSupplier);
private T getAndCache(String key, Supplier<R> valueSupplier);
private T getFromCacheOrThrow(String key, RuntimeException exception);
}
Retry getRetry(String name, MeterRegistry meterRegistry) {
RetryRegistry retryRegistry = RetryRegistry.ofDefaults();
TaggedRetryMetrics.ofRetryRegistry(retryRegistry).bindTo(meterRegistry);
return retryRegistry.retry(name);
}
CircuitBreaker getCircuitBreaker(String name, MeterRegistry meterRegistry) {
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
.ofDefaults();
TaggedCircuitBreakerMetrics
locals {
quick_window_long = "1h"
quick_window_short = "5m"
quick_burn_rate = 14.4
quick_time_window = "30d"
}
locals {
slow_window_long = "6h"
slow_window_short = "30m"
resource "datadog_monitor" "slow_burn_rate" {
name = "${var.service_name} ${var.environment} - Error Rate SLO [${var.slo}, ${local.slow_burn_rate}x]"
type = "slo alert"
query = <<-EOT
burn_rate("${module.error_rate_slo.id}").over("${local.slow_time_window}").long_window("${local.slow_window_long}").short_window("${local.slow_window_short}") > ${local.slow_burn_rate}
EOT
message = templatefile("${path.module}/message.md", {