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

33
LINES

< > BotCompany Repo | #1034193 // SnippetInstaCache - always has latest contents of selected snippets in memory

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

Libraryless. Click here for Pure Java version (19647L/120K).

1  
// only works within a module
2  
// has mild race conditions
3  
sclass SnippetInstaCache is AutoCloseable {
4  
  // snippet ID to text
5  
  SS texts = syncMap();
6  
  AutoCloseable updater;
7  
8  
  *(S... snippetIDs) { this(asList(snippetIDs)); }
9  
  
10  
  *(Iterable<S> snippetIDs) {
11  
    updater = dm_onSnippetEdited(voidfunc(S id) {
12  
      bool contained = texts.containsKey(id);
13  
      //print("SnippetInstaCache: snippet edited " + id + ", contained: " + contained);
14  
      if (contained) {
15  
        S text = loadSnippet(id);
16  
        print(text := shorten(text));
17  
        texts.put(id, text);
18  
      }
19  
    });
20  
    
21  
    fOr (snippetID : map fsI(snippetIDs))
22  
      texts.put(snippetID, loadSnippet(snippetID));
23  
  }
24  
  
25  
  // get snippet text
26  
  S get(S snippetID) {
27  
    ret texts.get(fsI(snippetID));
28  
  }
29  
  
30  
  close { dispose updater; }
31  
  
32  
  Cl<S> cachedSnippetIDs() { ret cloneKeys(texts); }
33  
}

Author comment

Began life as a copy of #1032676

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034193
Snippet name: SnippetInstaCache - always has latest contents of selected snippets in memory
Eternal ID of this version: #1034193/9
Text MD5: 9d0fdea4a2cc20b8333b812e4065427c
Transpilation MD5: c716b293c5d72ab9e193e0c6f6b5e038
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 17:19:33
Source code size: 897 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 108 / 222
Version history: 8 change(s)
Referenced in: [show references]