1 package com.atlassian.cache.vcache;
2
3 import javax.annotation.Nonnull;
4
5 import com.atlassian.cache.CacheSettings;
6 import com.atlassian.cache.CachedReference;
7 import com.atlassian.cache.CachedReferenceListener;
8 import com.atlassian.cache.Supplier;
9 import com.atlassian.vcache.JvmCache;
10
11 import static java.util.Objects.requireNonNull;
12
13 class JvmCachedReference<V>
14 extends ManagedCacheSupport
15 implements CachedReference<V>
16 {
17 private static final String REFERENCE_KEY = "ReferenceKey";
18
19 private final JvmCache<String, V> delegate;
20 private final Supplier<V> supplier;
21
22 public JvmCachedReference(String name,
23 JvmCache<String, V> delegate,
24 Supplier<V> supplier,
25 CacheSettings settings)
26 {
27 super(name, settings);
28 this.delegate = requireNonNull(delegate);
29 this.supplier = requireNonNull(supplier);
30 }
31
32 @Nonnull
33 @Override
34 public V get()
35 {
36 return delegate.get(REFERENCE_KEY, supplier::get);
37 }
38
39 @Override
40 public void reset()
41 {
42 delegate.remove(REFERENCE_KEY);
43 }
44
45 @Override
46 public void addListener(@Nonnull CachedReferenceListener<V> listener, boolean includeValues)
47 {
48 throw new UnsupportedOperationException("Unsupported when using the VCache implementation");
49 }
50
51 @Override
52 public void removeListener(@Nonnull CachedReferenceListener<V> listener)
53 {
54 throw new UnsupportedOperationException("Unsupported when using the VCache implementation");
55 }
56
57 @Override
58 public void clear()
59 {
60 if (isFlushable())
61 {
62 reset();
63 }
64 }
65
66 @Override
67 public boolean isLocal()
68 {
69 return true;
70 }
71 }