Libraryless. Click here for Pure Java version (4222L/24K).
1 | sclass ExpiringMap2<A, B> extends AbstractMap<A, B> { |
2 | Map<A, Pair<Long, B>> byKey = new HashMap; // key -> pair(expiry sys time, value) |
3 | new PriorityBlockingQueue<Pair<Long, A>> queue; // queue(pair(expiry sys time, key)) |
4 | long standardExpiryTime; // ms |
5 | bool renewOnOverwrite = true, renewOnGet; |
6 | O onChange; |
7 | bool useCountdown = true; |
8 | new RestartableCountdown countdown; |
9 | |
10 | *() {} |
11 | *(long *standardExpiryTime) {} |
12 | *(long *standardExpiryTime, O *onChange) {} |
13 | *(double standardExpirySeconds) { standardExpiryTime = toMS(standardExpirySeconds); } |
14 | |
15 | synchronized bool clean() { |
16 | bool changes; |
17 | Pair<Long, A> p; |
18 | long time = sysTime(); |
19 | while ((p = queue.peek()) != null && time >= p.a) { |
20 | if (useCountdown) countdown.stop(); |
21 | |
22 | p = queue.poll(); |
23 | ifdef ExpiringMap2_debug |
24 | print("ExpiringMap: retiring key " + p.b); |
25 | endifdef |
26 | Pair<Long, B> v = byKey.get(p.b); |
27 | if (v != null /*&& v.a == p.a*/) { |
28 | ifdef ExpiringMap2_debug |
29 | print("ExpiringMap: retired key " + p.b); |
30 | endifdef |
31 | byKey.remove(p.b); |
32 | set changes; |
33 | change(); |
34 | } |
35 | } |
36 | if (useCountdown) |
37 | countdown.setTargetTime(p != null ? p.a : 0, r clean); |
38 | ret changes; |
39 | } |
40 | |
41 | void change() { callF(onChange); } |
42 | |
43 | synchronized public B put(A a, B b) { |
44 | clean(); |
45 | long timeout = sysTime()+standardExpiryTime; |
46 | Pair<Long, B> p = byKey.get(a); |
47 | if (p != null && renewOnOverwrite) |
48 | queue.remove(Pair(p.a, a)); |
49 | byKey.put(a, pair(timeout, b)); |
50 | change(); |
51 | if (p == null || renewOnOverwrite) |
52 | queue.add(Pair(timeout, a)); |
53 | ret pairB(p); |
54 | } |
55 | |
56 | synchronized public B remove(O a) { |
57 | clean(); |
58 | Pair<Long, B> p = byKey.get(a); |
59 | if (p == null) null; |
60 | queue.remove(Pair(p.a, a)); |
61 | byKey.remove(a); |
62 | change(); |
63 | ret p.b; |
64 | } |
65 | |
66 | synchronized public B get(O a) { |
67 | clean(); |
68 | Pair<Long, B> p = byKey.get(a); |
69 | if (renewOnGet && p != null) { |
70 | queue.remove(Pair(p.a, a)); |
71 | long timeout = sysTime()+standardExpiryTime; |
72 | byKey.put((A) a, pair(timeout, p.b)); |
73 | queue.add(Pair(timeout, a)); |
74 | } |
75 | ret pairB(p); |
76 | } |
77 | |
78 | synchronized public Set<Map.Entry<A,B>> entrySet() { |
79 | clean(); |
80 | // TODO: mutex |
81 | ret synchronizedSet(mapValues pairB(byKey).entrySet()); |
82 | } |
83 | |
84 | synchronized public Set<A> keySet() { |
85 | clean(); |
86 | ret synchronizedSet(byKey.keySet()); |
87 | } |
88 | |
89 | synchronized public int size() { |
90 | clean(); |
91 | ret byKey.size(); |
92 | } |
93 | |
94 | void setStandardExpiryTime(long ms) { standardExpiryTime = ms; } |
95 | |
96 | synchronized ExpiringMap2<A, B> setMap(Map innerMap) { |
97 | byKey = innerMap; |
98 | this; |
99 | } |
100 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1016561 |
Snippet name: | ExpiringMap2 - now synchronized |
Eternal ID of this version: | #1016561/40 |
Text MD5: | 90af88bccf36d4a1fef32af34289547a |
Transpilation MD5: | c0f26d4b963f896008e16074476a6514 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-14 22:48:59 |
Source code size: | 2768 bytes / 100 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 484 / 1222 |
Version history: | 39 change(s) |
Referenced in: | [show references] |