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

114
LINES

< > BotCompany Repo | #1000997 // loadSnippet and dependencies (with (hopefully) better caching, current)

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

Libraryless. Click here for Pure Java version (4578L/31K).

1  
static boolean preferCached = false;
2  
static boolean loadSnippet_debug = false;
3  
static new ThreadLocal<Bool> loadSnippet_silent;
4  
static new ThreadLocal<Bool> loadSnippet_publicOnly;
5  
static int loadSnippet_timeout = 30000;
6  
7  
ifclass Snippet
8  
static S loadSnippet(Snippet s) {
9  
  ret loadSnippet(s.id);
10  
}
11  
endif
12  
13  
static S loadSnippet(S snippetID) ctex {
14  
  if (snippetID == null) null;
15  
  ret loadSnippet(parseSnippetID(snippetID), preferCached);
16  
}
17  
18  
static S loadSnippet(S snippetID, bool preferCached) throws IOException {
19  
  return loadSnippet(parseSnippetID(snippetID), preferCached);
20  
}
21  
22  
swappable static S loadSnippet(long snippetID) ctex {
23  
  ret loadSnippet(snippetID, preferCached);
24  
}
25  
26  
static S loadSnippet(long snippetID, boolean preferCached) throws IOException {
27  
  if (isLocalSnippetID(snippetID))
28  
    ret loadLocalSnippet(snippetID);
29  
    
30  
  ifndef NoResourceLoader
31  
  IResourceLoader rl = vm_getResourceLoader();
32  
  if (rl != null)
33  
    ret rl.loadSnippet(fsI(snippetID));
34  
  endifndef
35  
  
36  
  ret loadSnippet_noResourceLoader(snippetID, preferCached);
37  
}
38  
39  
static S loadSnippet_noResourceLoader(long snippetID, boolean preferCached) throws IOException {
40  
  S text;
41  
  
42  
  // boss bot (old concept)
43  
  /*text = getSnippetFromBossBot(snippetID);
44  
  if (text != null) return text;*/
45  
  
46  
  initSnippetCache();
47  
  text = DiskSnippetCache_get(snippetID);
48  
  
49  
  if (preferCached && text != null)
50  
    return text;
51  
  
52  
  try {
53  
    if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text));
54  
    S url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1";
55  
    if (nempty(text)) url += "&md5=" + md5(text);
56  
    if (!isTrue(loadSnippet_publicOnly!))
57  
      url += standardCredentials();
58  
    
59  
    S text2 = loadSnippet_loadFromServer(url);
60  
    
61  
    bool same = eq(text2, "==*#*==");
62  
    if (loadSnippet_debug) print("loadSnippet: same=" + same);
63  
    if (!same) text = text2;
64  
  } catch (RuntimeException e) {
65  
    e.printStackTrace();
66  
    throw new IOException("Snippet #" + snippetID + " not found or not public");
67  
  }
68  
69  
  try {
70  
    initSnippetCache();
71  
    DiskSnippetCache_put(snippetID, text);
72  
  } catch (IOException e) {
73  
    System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
74  
  }
75  
76  
  return text;
77  
}
78  
79  
static File DiskSnippetCache_dir;
80  
81  
public static void initDiskSnippetCache(File dir) {
82  
  DiskSnippetCache_dir = dir;
83  
  dir.mkdirs();
84  
}
85  
86  
public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
87  
  return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
88  
}
89  
90  
private static File DiskSnippetCache_getFile(long snippetID) {
91  
  return new File(DiskSnippetCache_dir, "" + snippetID);
92  
}
93  
94  
public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
95  
  saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
96  
}
97  
98  
public static File DiskSnippetCache_getDir() {
99  
  return DiskSnippetCache_dir;
100  
}
101  
102  
public static void initSnippetCache() {
103  
  if (DiskSnippetCache_dir == null)
104  
    initDiskSnippetCache(getGlobalCache());
105  
}
106  
107  
static S loadSnippet_loadFromServer(S url) {
108  
  Int oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout);
109  
  try {
110  
    ret isTrue(loadSnippet_silent!) ? loadPageSilently(url) : loadPage(url);
111  
  } finally {
112  
    loadPage_forcedTimeout_byThread.set(oldTimeout);
113  
  }
114  
}

Author comment

Began life as a copy of #2000512

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000997
Snippet name: loadSnippet and dependencies (with (hopefully) better caching, current)
Eternal ID of this version: #1000997/18
Text MD5: 375017d60e7132b057515a88f6ab2d14
Transpilation MD5: 12d7a5cc2bb48fe9614e2085ba96d538
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-10-14 14:27:28
Source code size: 3474 bytes / 114 lines
Pitched / IR pitched: No / No
Views / Downloads: 848 / 5202
Version history: 17 change(s)
Referenced in: [show references]