/** debug */ static void make(DiskCache2 dc, String filename) throws IOException { File want = dc.getCacheFile(filename); System.out.println("make=" + want.getPath() + "; exists = " + want.exists()); if (!want.exists()) want.createNewFile(); System.out.println(" canRead= " + want.canRead() + " canWrite = " + canWrite(want) + " lastMod = " + new Date(want.lastModified())); System.out.println(" original=" + EscapeStrings.urlDecode(filename)); }
/** 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); }
/** * Get a File in the cache, corresponding to the fileLocation. * File may or may not exist. * If fileLocation has "/" in it, and cachePathPolicy == NestedDirectory, the * nested directories will be created. * * @param fileLocation logical file location * @return physical File in the cache. */ public File getCacheFile(String fileLocation) { if (!alwaysUseCache) { File f = new File(fileLocation); if (canWrite(f)) return f; } File f = new File(makeCachePath(fileLocation)); //if (f.exists()) // f.setLastModified( System.currentTimeMillis()); if (cachePathPolicy == CachePathPolicy.NestedDirectory) { File dir = f.getParentFile(); dir.mkdirs(); } return f; }
/** * 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; }
@Test public void testDiskCache() throws Exception { String cacheDirName = tempFolder.newFolder().getAbsolutePath() + "/"; DiskCache2 cache = new DiskCache2(cacheDirName, false, 0, 0); cache.setAlwaysUseCache(true); Assert.assertEquals(cache.getRootDirectory(), cacheDirName); assert new File(cache.getRootDirectory()).exists(); GribIndexCache.setDiskCache2(cache); if (data.getName().endsWith(".grib1") || data.getName().endsWith(".grib2")) { String index = data.getPath()+".ncx4"; File indexFile = cache.getCacheFile(index); assert indexFile != null; assert indexFile.exists() : indexFile.getPath() +" does not exist";
File file = diskCache.getCacheFile(uriString); if (file != null) { HttpURLConnection conn = null;
@Test public void testCacheIsUsed() throws IOException, InvalidRangeException { String filename = "file:TestAggExistingCache.xml"; System.out.printf("%s%n", filename); String cacheDirName = tempFolder.newFolder().getAbsolutePath() + "/"; System.out.printf("cacheDir=%s%n", cacheDirName); File cacheDir = new File(cacheDirName); FileUtils.deleteDirectory(cacheDir); // from commons-io assert !cacheDir.exists(); DiskCache2 cache = new DiskCache2(cacheDirName, false, 0, 0); cache.setAlwaysUseCache(true); Assert.assertEquals(cache.getRootDirectory(), cacheDirName); assert new File(cache.getRootDirectory()).exists(); Aggregation.setPersistenceCache(cache); AggregationExisting.countCacheUse = 0; try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), filename, null)) { System.out.println(" TestNcmlAggExisting.open " + filename); Array ATssta = ncfile.readSection("ATssta(:,0,0,0)"); Assert.assertEquals(4, ATssta.getSize()); } Assert.assertEquals(0, AggregationExisting.countCacheUse); AggregationExisting.countCacheUse = 0; try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), filename, null)) { System.out.println(" TestNcmlAggExisting.open " + filename); Array ATssta = ncfile.readSection("ATssta(:,0,0,0)"); Assert.assertEquals(4, ATssta.getSize()); } Assert.assertEquals(8, AggregationExisting.countCacheUse); }
/** * Create a new, uniquely named file in the root directory. * Mimics File.createTempFile() * * @param prefix The prefix string to be used in generating the file's * name; must be at least three characters long * @param suffix The suffix string to be used in generating the file's * name; may be <code>null</code>, in which case the * suffix <code>".tmp"</code> will be used * @return uniquely named file */ public synchronized File createUniqueFile(String prefix, String suffix) { if (suffix == null) suffix = ".tmp"; Random random = new Random(System.currentTimeMillis()); File result = new File(getRootDirectory(), prefix + Integer.toString(random.nextInt()) + suffix); while (result.exists()) result = new File(getRootDirectory(), prefix + Integer.toString(random.nextInt()) + suffix); return result; }
@Test public void canWriteFile() throws IOException { File file = Files.createTempFile(nearest, "temp", null).toFile(); try { Assert.assertTrue(file.exists()); Assert.assertTrue(DiskCache2.canWrite(file)); } finally { file.delete(); } }
static public DiskCache2 getNoop() { DiskCache2 noop = new DiskCache2(); noop.neverUseCache = true; return noop; }
@Override public void run() { Formatter sbuff = new Formatter(); sbuff.format("DiskCache2 scour on directory= %s%n", root); cleanCache(new File(root), sbuff, true); if (cacheLog.isDebugEnabled()) cacheLog.debug(sbuff.toString()); } }
public void setPolicy(String policy) { if (policy == null) return; if (policy.equalsIgnoreCase("oneDirectory")) setCachePathPolicy(CachePathPolicy.OneDirectory, null); else if (policy.equalsIgnoreCase("nestedDirectory")) setCachePathPolicy(CachePathPolicy.NestedDirectory, null); }
/** * 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; }
@Test public void testNotExist() throws Exception { DiskCache2 cache = DiskCache2.getDefault(); File file = cache.getFile("gfs.t00z.master.grbf00.10m.uv.grib2"); // not exist System.out.printf("canWrite= %s%n", file.canWrite()); assert !file.canWrite(); }
static public void setPersistenceCache(DiskCache2 dc) { diskCache2 = dc; if (diskCache2 != null) diskCache2.setAlwaysUseCache(true); // the persistence cache file has same name as the ncml - must put it into the cache else clobber ncml 7/31/2014 }
/** * Looking for an existing file, in cache or no * @param fileLocation the original name * @return existing file if you can find it */ public File getExistingFileOrCache(String fileLocation) { File f = new File(fileLocation); if (f.exists()) return f; File fc = new File(makeCachePath(fileLocation)); if (fc.exists()) return fc; return null; }
@BeforeClass static public void before() { GribIosp.debugIndexOnlyCount = 0; GribCollectionImmutable.countGC = 0; PartitionCollectionImmutable.countPC = 0; RandomAccessFile.enableDefaultGlobalFileCache(); RandomAccessFile.setDebugLeaks(true); // Grib.setDebugFlags(new DebugFlagsImpl("Grib/indexOnly")); GribCdmIndex.setGribCollectionCache(new ucar.nc2.util.cache.FileCacheGuava("GribCollectionCacheGuava", 100)); GribCdmIndex.gribCollectionCache.resetTracking(); // make sure that the indexes are created with the data files DiskCache2 diskCache = GribIndexCache.getDiskCache2(); diskCache.setNeverUseCache(true); diskCache.setAlwaysUseCache(false); }
static synchronized public DiskCache2 getDiskCache2() { if (diskCache == null) diskCache = DiskCache2.getDefault(); return diskCache; }
File file = diskCache.getCacheFile(uriString); if (file != null) { HttpURLConnection conn = null;