/** * Default DiskCache2 strategy: use $user_home/.unidata/cache/, no scouring, alwaysUseCache = false * Mimics default DiskCache static class */ public DiskCache2() { String root = System.getProperty("nj22.cache"); if (root == null) { String home = System.getProperty("user.home"); if (home == null) home = System.getProperty("user.dir"); if (home == null) home = "."; root = home + "/.unidata/cache/"; } setRootDirectory(root); this.alwaysUseCache = false; }
/** * Default DiskCache2 strategy: use $user_home/.unidata/cache/, no scouring, alwaysUseCache = false * Mimics default DiskCache static class */ static public DiskCache2 getDefault() { String root = System.getProperty("nj22.cache"); if (root == null) { String home = System.getProperty("user.home"); if (home == null) home = System.getProperty("user.dir"); if (home == null) home = "."; root = home + "/.unidata/cache/"; } DiskCache2 result = new DiskCache2(); result.setRootDirectory(root); result.alwaysUseCache = false; return result; }
/** * Default DiskCache2 strategy: use $user_home/.unidata/cache/, no scouring, alwaysUseCache = false * Mimics default DiskCache static class */ static public DiskCache2 getDefault() { String root = System.getProperty("nj22.cache"); if (root == null) { String home = System.getProperty("user.home"); if (home == null) home = System.getProperty("user.dir"); if (home == null) home = "."; root = home + "/.unidata/cache/"; } DiskCache2 result = new DiskCache2(); result.setRootDirectory(root); result.alwaysUseCache = false; return result; }
/** * Create a cache on disk. * @param root the root directory of the cache. Must be writeable. * @param reletiveToHome if the root directory is reletive to the cache home directory. * @param persistMinutes a file is deleted if its last modified time is greater than persistMinutes * @param scourEveryMinutes how often to run the scour process. If <= 0, dont scour. */ public DiskCache2(String root, boolean reletiveToHome, int persistMinutes, int scourEveryMinutes) { this.persistMinutes = persistMinutes; this.scourEveryMinutes = scourEveryMinutes; if (reletiveToHome) { String home = System.getProperty("nj22.cachePersistRoot"); if (home == null) home = System.getProperty("user.home"); if (home == null) home = System.getProperty("user.dir"); if (home == null) home = "."; if (!home.endsWith("/")) home = home + "/"; root = home + root; } setRootDirectory(root); if (!fail && scourEveryMinutes > 0) { timer = new Timer("DiskCache-"+root); Calendar c = Calendar.getInstance(); // contains current startup time c.add(Calendar.MINUTE, scourEveryMinutes); timer.scheduleAtFixedRate(new CacheScourTask(), c.getTime(), (long) 1000 * 60 * scourEveryMinutes); } }
/** debug */ static public void main(String[] args) throws IOException { DiskCache2 dc = new DiskCache2("C:/TEMP/test/", false, 0, 0); dc.setRootDirectory("C:/temp/chill/"); make(dc, "C:/junk.txt"); make(dc, "C:/some/enchanted/evening/joots+3478.txt"); make(dc, "http://www.unidata.ucar.edu/some/enc hanted/eve'ning/nowrite.gibberish"); dc.showCache( System.out); StringBuilder sbuff = new StringBuilder(); dc.cleanCache(new File( dc.getRootDirectory()), sbuff, true); System.out.println(sbuff); }