@Builder
public ExpiringAfterWriteCache(
final long duration, final TimeUnit timeUnit, final BiConsumer<IdT, ValueT> removalListener) {
cache =
Caffeine.newBuilder()
.expireAfterWrite(duration, timeUnit)
.scheduler(Scheduler.systemScheduler())
.removalListener(
(IdT key, ValueT value, RemovalCause cause) -> {
if (cause == RemovalCause.EXPIRED || cause == RemovalCause.EXPLICIT) {
removalListener.accept(key, value);
}
})
.build();
this.removalListener = removalListener;
}
@PostConstruct
public void setupCaches() {
log.info(String.format("Cache refresh timings: org cache: %ds, space cache: %ds, app cache: %ds, app summary cache: %ds",
this.refreshCacheOrgLevelInSeconds, this.refreshCacheSpaceLevelInSeconds, this.refreshCacheApplicationLevelInSeconds, this.refreshCacheApplicationLevelInSeconds));
log.info(String.format("Cache expiry timings: org cache: %ds, space cache: %ds, app cache: %ds, app summary cache: %ds",
this.expiryCacheOrgLevelInSeconds, this.expiryCacheSpaceLevelInSeconds, this.expiryCacheApplicationLevelInSeconds, this.expiryCacheApplicationLevelInSeconds));
Scheduler caffeineScheduler = Scheduler.forScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
this.orgCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheOrgLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheOrgLevelInSeconds, TimeUnit.SECONDS)
.scheduler(caffeineScheduler)
.recordStats()
.buildAsync(new OrgCacheLoader());
this.internalMetrics.addCaffeineCache("orgCache", this.orgCache);
this.allOrgIdCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheOrgLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheOrgLevelInSeconds, TimeUnit.SECONDS)
.recordStats()
.scheduler(caffeineScheduler)
.buildAsync(new AllOrgIdCacheLoader());
this.internalMetrics.addCaffeineCache("allOrgCache", this.allOrgIdCache);
this.spaceCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheSpaceLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheSpaceLevelInSeconds, TimeUnit.SECONDS)
.recordStats()
.scheduler(caffeineScheduler)
.buildAsync(new SpaceCacheLoader());
this.internalMetrics.addCaffeineCache("spaceCache", this.spaceCache);
this.spaceIdInOrgCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheSpaceLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheSpaceLevelInSeconds, TimeUnit.SECONDS)
.recordStats()
.scheduler(caffeineScheduler)
.buildAsync(new SpaceIdInOrgCacheLoader());
this.internalMetrics.addCaffeineCache("spaceInOrgCache", this.spaceIdInOrgCache);
this.appsInSpaceCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheApplicationLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheApplicationLevelInSeconds, TimeUnit.SECONDS)
.recordStats()
.scheduler(caffeineScheduler)
.buildAsync(new AppsInSpaceCacheLoader());
this.internalMetrics.addCaffeineCache("appsInSpace", this.appsInSpaceCache);
this.spaceSummaryCache = Caffeine.newBuilder()
.expireAfterAccess(this.expiryCacheApplicationLevelInSeconds, TimeUnit.SECONDS)
.refreshAfterWrite(this.refreshCacheApplicationLevelInSeconds, TimeUnit.SECONDS)
.recordStats()
.scheduler(caffeineScheduler)
.buildAsync(new SpaceSummaryCacheLoader());
this.internalMetrics.addCaffeineCache("spaceSummary", this.spaceSummaryCache);
}
public Scheduler scheduler() {
return scheduler;
}