1 package com.atlassian.vcache;
2
3 import java.time.Duration;
4 import java.util.Optional;
5
6
7
8
9 public class JvmSample {
10 private final JvmCache<String, String> cache;
11
12 public JvmSample(VCacheFactory factory) {
13 JvmCacheSettings settings = new JvmCacheSettingsBuilder().
14 maxEntries(10).
15 defaultTtl(Duration.ofSeconds(42)).
16 build();
17 cache = factory.getJvmCache("my.cache", settings);
18 }
19
20 public String findFoo(String id) {
21
22 Optional<String> cachedValue = cache.get(id);
23 if (cachedValue.isPresent()) {
24 return cachedValue.get();
25 }
26
27 final String value = "id-" + id;
28 Optional<String> afterPut = cache.putIfAbsent(id, value);
29
30 return afterPut.orElse(value);
31 }
32
33 public String findFoo2(String id) {
34
35 return cache.get(id, () -> "id-" + id);
36 }
37
38 public int cacheSize() {
39 return cache.getKeys().size();
40 }
41 }