@Test public void testMultipleAsynchronousWrites() throws ParseException { int max = 100; ParseKeyValueCache.maxKeyValueCacheFiles = max; for (int i = 0; i < max; i++) { ParseKeyValueCache.saveToKeyValueCache("key " + i, "test"); } List<Task<Void>> tasks = new ArrayList<>(); for (int i = 0; i < 1000; i++) { tasks.add(Task.call(new Callable<Void>() { @Override public Void call() { ParseKeyValueCache.saveToKeyValueCache("foo", "test"); return null; } }, Task.BACKGROUND_EXECUTOR)); } ParseTaskUtils.wait(Task.whenAll(tasks)); }
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; } } } }    }