@Test public void testSaveToKeyValueCacheWithoutCacheDir() throws Exception { assertTrue(keyValueCacheDir.exists()); keyValueCacheDir.delete(); assertFalse(keyValueCacheDir.exists()); ParseKeyValueCache.saveToKeyValueCache("key", "value"); assertEquals(1, keyValueCacheDir.listFiles().length); assertArrayEquals( "value".getBytes(), ParseFileUtils.readFileToByteArray(keyValueCacheDir.listFiles()[0])); }
static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } }
ParseKeyValueCache { static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } } }
ParseKeyValueCache { static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } }  }
ParseKeyValueCache { static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } }   }
ParseKeyValueCache { static void saveToKeyValueCache(String key, String value) { synchronized (MUTEX_IO) { File prior = getKeyValueCacheFile(key); if (prior != null) { prior.delete(); } File f = createKeyValueCacheFile(key); try { ParseFileUtils.writeByteArrayToFile(f, value.getBytes("UTF-8")); } catch (IOException e) { } File[] files = getKeyValueCacheDir().listFiles(); if (files == null || files.length == 0) { return; } int numFiles = files.length; int numBytes = 0; for (File file : files) { numBytes += file.length(); } if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { return; } Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { int dateCompare = Long.valueOf(f1.lastModified()).compareTo(f2.lastModified()); if (dateCompare != 0) { return dateCompare; } else { return f1.getName().compareTo(f2.getName()); } } }); for (File file : files) { numFiles--; numBytes -= file.length(); file.delete(); if (numFiles <= maxKeyValueCacheFiles && numBytes <= maxKeyValueCacheBytes) { break; } } } }    }