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

111
LINES

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

JavaX fragment (include)

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);
}

public static String loadSnippet(long snippetID) ctex {
  ret loadSnippet(snippetID, preferCached);
}

public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
  S text;
  
  if (isLocalSnippetID(snippetID))
    ret loadLocalSnippet(snippetID);
    
  ifndef NoResourceLoader
  IResourceLoader rl = vm_getResourceLoader();
  if (rl != null)
    ret rl.loadSnippet(fsI(snippetID));
  endifndef
  
  // boss bot disabled for now for shorter transpilations
  
  /*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   

Travelled to 12 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1000997
Snippet name: loadSnippet and dependencies (with (hopefully) better caching, current)
Eternal ID of this version: #1000997/15
Text MD5: eabb7b55fc59440dd5d27d479c284e20
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-08 14:19:08
Source code size: 3357 bytes / 111 lines
Pitched / IR pitched: No / No
Views / Downloads: 496 / 4678
Version history: 14 change(s)
Referenced in: [show references]

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