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

105
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 (10062L/55K).

static class TimedCache<A> {
  long timeout;
  volatile A value; // volatile for peek()
  O function;
  long set;
  Lock lock = lock();
  bool keepValueWhileCalculating; // special mode to make peek() always return something
  
  // stats
  int stores, fails, hits;
  
  *(double timeoutSeconds, IF0<A> function) {
    this(function, timeoutSeconds);
  }
  
  *(double timeoutSeconds, O function) {
    this(function, timeoutSeconds);
  }
  
  *(IF0<A> function, double timeoutSeconds) {
    this((O) function, timeoutSeconds);
  }
  
  *(O function, double timeoutSeconds) {
    this(timeoutSeconds);
    this.function = function;
  }

  // 0 = no timeout  
  *(double timeoutSeconds) {
    timeout = toMS(timeoutSeconds);
  }
  
  A set(A a) {
    lock lock;
    ++stores;
    value = a;
    set = now();
    ret a;
  }
  
  bool has() {
    lock lock;
    clean();
    if (set != 0) {
      ++hits;
      true;
    }
    ++fails;
    false;
  }
  
  A get() {
    lock lock;
    if (function != null) ret get(function);
    clean();
    if (set != 0) ++hits; else ++fails;
    ret value;
  }
  
  A get(IF0<A> makerFunction) {
    ret get((O) makerFunction);
  }
  
  A get(O makerFunction) {
    lock lock;
    if (keepValueWhileCalculating) {
      if (value == null || shouldClean())
        set((A) callF(makerFunction));
      ret value;
    } else {
      ret this.has() ? getNoClean() : set((A) callF(makerFunction));
    }
  }
  
  A getNoClean() {
    lock lock; // lock to wait for potentially running calculation
    ret value;
  }
  
  // clear if timed out
  void clean() {
    lock lock;
    if (shouldClean()) clear();
  }
  
  bool shouldClean() {
    ret timeout > 0 && now() > set+timeout;
  }
  
  // clear now
  void clear() {
    lock lock;
    set = 0;
    value = null;
  }
  
  S stats() {
    ret "Stores: " + stores + ", hits: " + hits + ", fails: " + fails;
  }
  
  A peek() {
    ret value;
  }
}

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: 773 / 3103
Version history: 10 change(s)
Referenced in: [show references]