Libraryless. Click here for Pure Java version (10062L/55K).
1 | static class TimedCache<A> {
|
2 | long timeout; |
3 | volatile A value; // volatile for peek() |
4 | O function; |
5 | long set; |
6 | Lock lock = lock(); |
7 | bool keepValueWhileCalculating; // special mode to make peek() always return something |
8 | |
9 | // stats |
10 | int stores, fails, hits; |
11 | |
12 | *(double timeoutSeconds, IF0<A> function) {
|
13 | this(function, timeoutSeconds); |
14 | } |
15 | |
16 | *(double timeoutSeconds, O function) {
|
17 | this(function, timeoutSeconds); |
18 | } |
19 | |
20 | *(IF0<A> function, double timeoutSeconds) {
|
21 | this((O) function, timeoutSeconds); |
22 | } |
23 | |
24 | *(O function, double timeoutSeconds) {
|
25 | this(timeoutSeconds); |
26 | this.function = function; |
27 | } |
28 | |
29 | // 0 = no timeout |
30 | *(double timeoutSeconds) {
|
31 | timeout = toMS(timeoutSeconds); |
32 | } |
33 | |
34 | A set(A a) {
|
35 | lock lock; |
36 | ++stores; |
37 | value = a; |
38 | set = now(); |
39 | ret a; |
40 | } |
41 | |
42 | bool has() {
|
43 | lock lock; |
44 | clean(); |
45 | if (set != 0) {
|
46 | ++hits; |
47 | true; |
48 | } |
49 | ++fails; |
50 | false; |
51 | } |
52 | |
53 | A get() {
|
54 | lock lock; |
55 | if (function != null) ret get(function); |
56 | clean(); |
57 | if (set != 0) ++hits; else ++fails; |
58 | ret value; |
59 | } |
60 | |
61 | A get(IF0<A> makerFunction) {
|
62 | ret get((O) makerFunction); |
63 | } |
64 | |
65 | A get(O makerFunction) {
|
66 | lock lock; |
67 | if (keepValueWhileCalculating) {
|
68 | if (value == null || shouldClean()) |
69 | set((A) callF(makerFunction)); |
70 | ret value; |
71 | } else {
|
72 | ret this.has() ? getNoClean() : set((A) callF(makerFunction)); |
73 | } |
74 | } |
75 | |
76 | A getNoClean() {
|
77 | lock lock; // lock to wait for potentially running calculation |
78 | ret value; |
79 | } |
80 | |
81 | // clear if timed out |
82 | void clean() {
|
83 | lock lock; |
84 | if (shouldClean()) clear(); |
85 | } |
86 | |
87 | bool shouldClean() {
|
88 | ret timeout > 0 && now() > set+timeout; |
89 | } |
90 | |
91 | // clear now |
92 | void clear() {
|
93 | lock lock; |
94 | set = 0; |
95 | value = null; |
96 | } |
97 | |
98 | S stats() {
|
99 | ret "Stores: " + stores + ", hits: " + hits + ", fails: " + fails; |
100 | } |
101 | |
102 | A peek() {
|
103 | ret value; |
104 | } |
105 | } |
download show line numbers debug dex old transpilations
Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, fehiwqupcyrn, gwrvuhgaqvyk, ishqpsrjomds, lhdilzshxjzv, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv, xprdwmaupziu
No comments. add comment
| Snippet ID: | #1004094 |
| Snippet name: | TimedCache - caches an object for a specified time [thread-safe] |
| Eternal ID of this version: | #1004094/11 |
| Text MD5: | 7a62baa11c5f4c81c2500794629504cd |
| Transpilation MD5: | 12f8d7e9cbaaa048851ed4b5a94d156d |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-12-06 15:02:57 |
| Source code size: | 2032 bytes / 105 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1210 / 3678 |
| Version history: | 10 change(s) |
| Referenced in: | [show references] |