Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1004094 - TimedCache - caches an object for a specified time [thread-safe]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (1938L/12K).

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, O function) {
13  
    this(function, timeoutSeconds);
14  
  }
15  
  
16  
  *(O function, double timeoutSeconds) {
17  
    this(timeoutSeconds);
18  
    this.function = function;
19  
  }
20  
21  
  // 0 = no timeout  
22  
  *(double timeoutSeconds) {
23  
    timeout = toMS(timeoutSeconds);
24  
  }
25  
  
26  
  A set(A a) {
27  
    lock lock;
28  
    ++stores;
29  
    value = a;
30  
    set = now();
31  
    ret a;
32  
  }
33  
  
34  
  bool has() {
35  
    lock lock;
36  
    clean();
37  
    if (set != 0) {
38  
      ++hits;
39  
      true;
40  
    }
41  
    ++fails;
42  
    false;
43  
  }
44  
  
45  
  A get() {
46  
    lock lock;
47  
    if (function != null) ret get(function);
48  
    clean();
49  
    if (set != 0) ++hits; else ++fails;
50  
    ret value;
51  
  }
52  
  
53  
  A get(O makerFunction) {
54  
    lock lock;
55  
    if (keepValueWhileCalculating) {
56  
      if (value == null || shouldClean())
57  
        set((A) callF(makerFunction));
58  
      ret value;
59  
    } else {
60  
      ret this.has() ? getNoClean() : set((A) callF(makerFunction));
61  
    }
62  
  }
63  
  
64  
  A getNoClean() {
65  
    lock lock; // lock to wait for potentially running calculation
66  
    ret value;
67  
  }
68  
  
69  
  // clear if timed out
70  
  void clean() {
71  
    lock lock;
72  
    if (shouldClean()) clear();
73  
  }
74  
  
75  
  bool shouldClean() {
76  
    ret timeout > 0 && now() > set+timeout;
77  
  }
78  
  
79  
  // clear now
80  
  void clear() {
81  
    lock lock;
82  
    set = 0;
83  
    value = null;
84  
  }
85  
  
86  
  S stats() {
87  
    ret "Stores: " + stores + ", hits: " + hits + ", fails: " + fails;
88  
  }
89  
  
90  
  A peek() {
91  
    ret value;
92  
  }
93  
}

download  show line numbers  debug dex   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, fehiwqupcyrn, gwrvuhgaqvyk, ishqpsrjomds, lhdilzshxjzv, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, 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/7
Text MD5: 52d20f8a91fb3ad95b135d0445fea9b6
Transpilation MD5: 5182fce10afed0d12f845aaa3a2b9b6d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-08 14:08:41
Source code size: 1767 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 412 / 2336
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1004094 & http://1004094.tinybrain.de