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).

static boolean preferCached = false;
static boolean loadSnippet_debug = false;
static new ThreadLocal<Bool> loadSnippet_silent;
static new ThreadLocal<Bool> loadSnippet_publicOnly;
static int loadSnippet_timeout = 30000;

ifclass Snippet
static S loadSnippet(Snippet s) {
  ret loadSnippet(s.id);
}
endif

static S loadSnippet(S snippetID) ctex {
  if (snippetID == null) null;
  ret loadSnippet(parseSnippetID(snippetID), preferCached);
}

static S loadSnippet(S snippetID, bool preferCached) throws IOException {
  return loadSnippet(parseSnippetID(snippetID), preferCached);
}

swappable static S loadSnippet(long snippetID) ctex {
  ret loadSnippet(snippetID, preferCached);
}

static S loadSnippet(long snippetID, boolean preferCached) throws IOException {
  if (isLocalSnippetID(snippetID))
    ret loadLocalSnippet(snippetID);
    
  ifndef NoResourceLoader
  IResourceLoader rl = vm_getResourceLoader();
  if (rl != null)
    ret rl.loadSnippet(fsI(snippetID));
  endifndef
  
  ret loadSnippet_noResourceLoader(snippetID, preferCached);
}

static S loadSnippet_noResourceLoader(long snippetID, boolean preferCached) throws IOException {
  S text;
  
  // boss bot (old concept)
  /*text = getSnippetFromBossBot(snippetID);
  if (text != null) return text;*/
  
  initSnippetCache();
  text = DiskSnippetCache_get(snippetID);
  
  if (preferCached && text != null)
    return text;
  
  try {
    if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text));
    S url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1";
    if (nempty(text)) url += "&md5=" + md5(text);
    if (!isTrue(loadSnippet_publicOnly!))
      url += standardCredentials();
    
    S text2 = loadSnippet_loadFromServer(url);
    
    bool same = eq(text2, "==*#*==");
    if (loadSnippet_debug) print("loadSnippet: same=" + same);
    if (!same) text = text2;
  } catch (RuntimeException e) {
    e.printStackTrace();
    throw new IOException("Snippet #" + snippetID + " not found or not public");
  }

  try {
    initSnippetCache();
    DiskSnippetCache_put(snippetID, text);
  } catch (IOException e) {
    System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
  }

  return text;
}

static File DiskSnippetCache_dir;

public static void initDiskSnippetCache(File dir) {
  DiskSnippetCache_dir = dir;
  dir.mkdirs();
}

public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
  return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
}

private static File DiskSnippetCache_getFile(long snippetID) {
  return new File(DiskSnippetCache_dir, "" + snippetID);
}

public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
  saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
}

public static File DiskSnippetCache_getDir() {
  return DiskSnippetCache_dir;
}

public static void initSnippetCache() {
  if (DiskSnippetCache_dir == null)
    initDiskSnippetCache(getGlobalCache());
}

static S loadSnippet_loadFromServer(S url) {
  Int oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout);
  try {
    ret isTrue(loadSnippet_silent!) ? loadPageSilently(url) : loadPage(url);
  } finally {
    loadPage_forcedTimeout_byThread.set(oldTimeout);
  }
}

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: 938 / 5306
Version history: 17 change(s)
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)