1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.cache;
21
22 import java.time.Duration;
23 import java.time.Instant;
24 import java.time.temporal.ChronoUnit;
25 import java.util.function.Supplier;
26
27 import org.ehcache.expiry.ExpiryPolicy;
28
29
30
31
32
33 public class EHCacheExpiry implements ExpiryPolicy<String, EHCacheValue> {
34
35
36
37
38 public static final long DEFAULT_TTL = 3600L;
39
40
41
42
43 public static final long MAX_TTL = DEFAULT_TTL * 12L;
44
45
46 @Override
47 public Duration getExpiryForCreation(String s, EHCacheValue ehCacheValue) {
48 Instant expiry = ehCacheValue.getExpiry();
49 Instant now = Instant.now();
50
51 if (expiry == null || expiry.isBefore(now) || expiry.isAfter(now.plusSeconds(MAX_TTL))) {
52 return Duration.of(DEFAULT_TTL, ChronoUnit.SECONDS);
53 }
54
55 return Duration.of(expiry.toEpochMilli() - now.toEpochMilli(), ChronoUnit.MILLIS);
56 }
57
58 @Override
59 public Duration getExpiryForAccess(String s, Supplier<? extends EHCacheValue> supplier) {
60 return null;
61 }
62
63 @Override
64 public Duration getExpiryForUpdate(String s, Supplier<? extends EHCacheValue> supplier, EHCacheValue ehCacheValue) {
65 return null;
66 }
67
68
69 }