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

60
LINES

< > BotCompany Repo | #1003946 // cleanJavaXCache

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

Libraryless. Click here for Pure Java version (2478L/16K).

static int cleanJavaXCache_msgInterval = 5000;
sbool cleanJavaXCache_verbose;
static int cleanJavaXCache_emptyDirs_retentionMinutes = 10;

static void cleanJavaXCache(int retentionHours, bool verbose) {
  pcall {
    retentionHours = max(1, retentionHours);
    long now = now(), msgPrinted = now();
    if (verbose)
      System.out.println("Cleaning JavaX Cache (" + retentionHours + " hours retention time; cutoff time: " + (now-retentionHours*1000*60*60) + ")");
    int fileDeletions = 0;
    for (File javax : ll(new File(userHome(), ".javax"), javaxTempDir())) {
      File[] files = javax.listFiles();
      int scanned = 0, deleted = 0;
      if (files != null) for (File dir : files) {
        ++scanned;
        if (now() >= msgPrinted+cleanJavaXCache_msgInterval) {
          print("Cleaning cache (" + scanned + "/" + n(files, "dir") + " scanned, " + deleted + " deleted)...");
          msgPrinted = now();
        }
        if (dir.isDirectory() && isInteger(dir.getName())) pcall {
          long time = Long.parseLong(dir.getName());
          long seconds = (now - time) / 1000;
          long minutes = seconds / 60;
          int retentionMinutes = empty(listFiles(dir)) ? cleanJavaXCache_emptyDirs_retentionMinutes : retentionHours*60;
          if (cleanJavaXCache_verbose)
            print("Dir " + dir.getName() + " minutes: " + minutes + ", deleting? " + (minutes >= retentionMinutes)); 
          if (minutes >= retentionMinutes) {
            //System.out.println("Can delete " + dir.getAbsolutePath() + ", age: " + hours + " h");
            fileDeletions += cleanJavaXCache_removeDir(dir, verbose);
            ++deleted;
          }
        }
      }
    }
    if (verbose && fileDeletions != 0)
      print("Cleaned cache. File deletions: " + fileDeletions);
  }
}

static int cleanJavaXCache_removeDir(File dir, bool verbose) {
  int fileDeletions = 0;
  if (!containsOneOf(dir.getAbsolutePath(), ".javax", "JavaX-Caches"))  // security check!
    fail("WHAT ARE YOU DOING!? >> " + dir.getAbsolutePath());
  File[] files = listFiles(dir);
  if (cleanJavaXCache_verbose)
    print("Have " + n(files, "file") + " in " + dir);
  for ping (File f : files) {
    if (f.isDirectory())
      cleanJavaXCache_removeDir(f, verbose);
    else {
      if (verbose)
        print("Deleting " + f.getAbsolutePath());
      f.delete();
      ++fileDeletions;
    }
  }
  dir.delete();
  ret fileDeletions;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003946
Snippet name: cleanJavaXCache
Eternal ID of this version: #1003946/17
Text MD5: a3b2748fab9fc41f619f88fc7d9d39e9
Transpilation MD5: 2d3cbe3fe4652ac4cac30981bb432f9f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-16 17:55:03
Source code size: 2487 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 698 / 750
Version history: 16 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)