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 {
11 private final JvmCache<String, String> cache;
12
13 public JvmSample(VCacheFactory factory)
14 {
15 JvmCacheSettings settings = new JvmCacheSettingsBuilder().
16 maxEntries(10).
17 defaultTtl(Duration.ofSeconds(42)).
18 build();
19 cache = factory.getJvmCache("my.cache", settings);
20 }
21
22 public String findFoo(String id)
23 {
24
25 Optional<String> cachedValue = cache.get(id);
26 if (cachedValue.isPresent())
27 {
28 return cachedValue.get();
29 }
30
31 final String value = "id-" + id;
32 Optional<String> afterPut = cache.putIfAbsent(id, value);
33
34 return afterPut.orElse(value);
35 }
36
37 public String findFoo2(String id)
38 {
39
40 return cache.get(id, () -> "id-" + id);
41 }
42
43 public int cacheSize()
44 {
45 return cache.getKeys().size();
46 }
47 }