1 package com.atlassian.vcache.internal.core.service;
2
3 import com.atlassian.vcache.internal.core.ExternalCacheKeyGenerator;
4 import io.atlassian.util.concurrent.Lazy;
5
6 import javax.annotation.Nullable;
7 import java.time.Duration;
8 import java.util.function.Function;
9 import java.util.function.Supplier;
10
11
12
13
14
15
16
17
18 public class VersionedExternalCacheRequestContext<V> extends AbstractExternalCacheRequestContext<V> {
19 protected final String externalCacheVersionKey;
20 private final Supplier<Long> cacheVersionSupplier;
21
22 @Nullable
23 private Long cacheVersion;
24
25 public VersionedExternalCacheRequestContext(ExternalCacheKeyGenerator keyGenerator,
26 String name,
27 Supplier<String> partitionSupplier,
28 Function<String, Long> cacheVersionSupplier,
29 Duration lockTimeout) {
30 super(keyGenerator, name, partitionSupplier, lockTimeout);
31 this.externalCacheVersionKey =
32 keyGenerator.cacheVersionKey(partitionSupplier.get(), name);
33
34 this.cacheVersionSupplier = Lazy.supplier(() -> cacheVersionSupplier.apply(externalCacheVersionKey));
35 }
36
37 @Override
38 protected long cacheVersion() {
39 return cacheVersion == null
40 ? cacheVersionSupplier.get()
41 : cacheVersion;
42 }
43
44 public void updateCacheVersion(final Function<String, Long> cacheVersionSupplier) {
45 this.cacheVersion = cacheVersionSupplier.apply(externalCacheVersionKey);
46 clearKeyMaps();
47 }
48 }