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

1  
static int cleanJavaXCache_msgInterval = 5000;
2  
sbool cleanJavaXCache_verbose;
3  
static int cleanJavaXCache_emptyDirs_retentionMinutes = 10;
4  
5  
static void cleanJavaXCache(int retentionHours, bool verbose) {
6  
  pcall {
7  
    retentionHours = max(1, retentionHours);
8  
    long now = now(), msgPrinted = now();
9  
    if (verbose)
10  
      System.out.println("Cleaning JavaX Cache (" + retentionHours + " hours retention time; cutoff time: " + (now-retentionHours*1000*60*60) + ")");
11  
    int fileDeletions = 0;
12  
    for (File javax : ll(new File(userHome(), ".javax"), javaxTempDir())) {
13  
      File[] files = javax.listFiles();
14  
      int scanned = 0, deleted = 0;
15  
      if (files != null) for (File dir : files) {
16  
        ++scanned;
17  
        if (now() >= msgPrinted+cleanJavaXCache_msgInterval) {
18  
          print("Cleaning cache (" + scanned + "/" + n(files, "dir") + " scanned, " + deleted + " deleted)...");
19  
          msgPrinted = now();
20  
        }
21  
        if (dir.isDirectory() && isInteger(dir.getName())) pcall {
22  
          long time = Long.parseLong(dir.getName());
23  
          long seconds = (now - time) / 1000;
24  
          long minutes = seconds / 60;
25  
          int retentionMinutes = empty(listFiles(dir)) ? cleanJavaXCache_emptyDirs_retentionMinutes : retentionHours*60;
26  
          if (cleanJavaXCache_verbose)
27  
            print("Dir " + dir.getName() + " minutes: " + minutes + ", deleting? " + (minutes >= retentionMinutes)); 
28  
          if (minutes >= retentionMinutes) {
29  
            //System.out.println("Can delete " + dir.getAbsolutePath() + ", age: " + hours + " h");
30  
            fileDeletions += cleanJavaXCache_removeDir(dir, verbose);
31  
            ++deleted;
32  
          }
33  
        }
34  
      }
35  
    }
36  
    if (verbose && fileDeletions != 0)
37  
      print("Cleaned cache. File deletions: " + fileDeletions);
38  
  }
39  
}
40  
41  
static int cleanJavaXCache_removeDir(File dir, bool verbose) {
42  
  int fileDeletions = 0;
43  
  if (!containsOneOf(dir.getAbsolutePath(), ".javax", "JavaX-Caches"))  // security check!
44  
    fail("WHAT ARE YOU DOING!? >> " + dir.getAbsolutePath());
45  
  File[] files = listFiles(dir);
46  
  if (cleanJavaXCache_verbose)
47  
    print("Have " + n(files, "file") + " in " + dir);
48  
  for ping (File f : files) {
49  
    if (f.isDirectory())
50  
      cleanJavaXCache_removeDir(f, verbose);
51  
    else {
52  
      if (verbose)
53  
        print("Deleting " + f.getAbsolutePath());
54  
      f.delete();
55  
      ++fileDeletions;
56  
    }
57  
  }
58  
  dir.delete();
59  
  ret fileDeletions;
60  
}

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: 697 / 750
Version history: 16 change(s)
Referenced in: [show references]