1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache.ehcache3.configuration;
35
36 import info.magnolia.module.cache.filter.CachedEntry;
37
38 import java.time.Duration;
39 import java.util.function.Supplier;
40
41 import org.ehcache.expiry.ExpiryPolicy;
42
43
44
45
46
47
48 public class EhCache3Expiry<K, V> implements ExpiryPolicy<K, V> {
49
50 private Duration create = ExpiryPolicy.INFINITE;
51 private Duration access = null;
52 private Duration update = null;
53
54 @Override
55 public Duration getExpiryForCreation(K key, V value) {
56 Duration duration = create;
57 if (value instanceof CachedEntry) {
58 int ttl = ((CachedEntry) value).getTimeToLiveInSeconds();
59 duration = ttl == -1 ? create : Duration.ofSeconds(ttl);
60 }
61 return duration;
62 }
63
64 @Override
65 public Duration getExpiryForAccess(K key, Supplier<? extends V> value) {
66 return access;
67 }
68
69 @Override
70 public Duration getExpiryForUpdate(K key, Supplier<? extends V> oldValue, V newValue) {
71 return update;
72 }
73
74 public Long getCreate() {
75 return create == null ? null : create.getSeconds();
76 }
77
78 public void setCreate(Long create) {
79 this.create = Duration.ofSeconds(create);
80 }
81
82 public Long getAccess() {
83 return access == null ? null : access.getSeconds();
84 }
85
86 public void setAccess(Long access) {
87 this.access = Duration.ofSeconds(access);
88 }
89
90 public Long getUpdate() {
91 return update == null ? null : update.getSeconds();
92 }
93
94 public void setUpdate(Long update) {
95 this.update = Duration.ofSeconds(update);
96 }
97 }