mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Re enable single SimpleCache instance assertion and add helper methods
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=193637830
This commit is contained in:
parent
0666e4f306
commit
80c28f14b3
@ -37,6 +37,8 @@ public final class SimpleCache implements Cache {
|
|||||||
private static final String TAG = "SimpleCache";
|
private static final String TAG = "SimpleCache";
|
||||||
private static final HashSet<File> lockedCacheDirs = new HashSet<>();
|
private static final HashSet<File> lockedCacheDirs = new HashSet<>();
|
||||||
|
|
||||||
|
private static boolean cacheFolderLockingDisabled;
|
||||||
|
|
||||||
private final File cacheDir;
|
private final File cacheDir;
|
||||||
private final CacheEvictor evictor;
|
private final CacheEvictor evictor;
|
||||||
private final CachedContentIndex index;
|
private final CachedContentIndex index;
|
||||||
@ -45,6 +47,31 @@ public final class SimpleCache implements Cache {
|
|||||||
private long totalSpace;
|
private long totalSpace;
|
||||||
private boolean released;
|
private boolean released;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether {@code cacheFolder} is locked by a {@link SimpleCache} instance. To unlock the
|
||||||
|
* folder the {@link SimpleCache} instance should be released.
|
||||||
|
*/
|
||||||
|
public static synchronized boolean isCacheFolderLocked(File cacheFolder) {
|
||||||
|
return lockedCacheDirs.contains(cacheFolder.getAbsoluteFile());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables locking the cache folders which {@link SimpleCache} instances are using and releases
|
||||||
|
* any previous lock.
|
||||||
|
*
|
||||||
|
* <p>The locking prevents multiple {@link SimpleCache} instances from being created for the same
|
||||||
|
* folder. Disabling it may cause the cache data to be corrupted. Use at your own risk.
|
||||||
|
*
|
||||||
|
* @deprecated Don't create multiple {@link SimpleCache} instances for the same cache folder. If
|
||||||
|
* you need to create another instance, make sure you call {@link #release()} on the previous
|
||||||
|
* instance.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static synchronized void disableCacheFolderLocking() {
|
||||||
|
cacheFolderLockingDisabled = true;
|
||||||
|
lockedCacheDirs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
|
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
|
||||||
* the directory cannot be used to store other files.
|
* the directory cannot be used to store other files.
|
||||||
@ -93,6 +120,10 @@ public final class SimpleCache implements Cache {
|
|||||||
* @param index The CachedContentIndex to be used.
|
* @param index The CachedContentIndex to be used.
|
||||||
*/
|
*/
|
||||||
/*package*/ SimpleCache(File cacheDir, CacheEvictor evictor, CachedContentIndex index) {
|
/*package*/ SimpleCache(File cacheDir, CacheEvictor evictor, CachedContentIndex index) {
|
||||||
|
if (!lockFolder(cacheDir)) {
|
||||||
|
throw new IllegalStateException("Another SimpleCache instance uses the folder: " + cacheDir);
|
||||||
|
}
|
||||||
|
|
||||||
this.cacheDir = cacheDir;
|
this.cacheDir = cacheDir;
|
||||||
this.evictor = evictor;
|
this.evictor = evictor;
|
||||||
this.index = index;
|
this.index = index;
|
||||||
@ -122,7 +153,7 @@ public final class SimpleCache implements Cache {
|
|||||||
try {
|
try {
|
||||||
removeStaleSpansAndCachedContents();
|
removeStaleSpansAndCachedContents();
|
||||||
} finally {
|
} finally {
|
||||||
releaseFolder(cacheDir);
|
unlockFolder(cacheDir);
|
||||||
released = true;
|
released = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -461,10 +492,15 @@ public final class SimpleCache implements Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static synchronized boolean lockFolder(File cacheDir) {
|
private static synchronized boolean lockFolder(File cacheDir) {
|
||||||
|
if (cacheFolderLockingDisabled) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return lockedCacheDirs.add(cacheDir.getAbsoluteFile());
|
return lockedCacheDirs.add(cacheDir.getAbsoluteFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static synchronized void releaseFolder(File cacheDir) {
|
private static synchronized void unlockFolder(File cacheDir) {
|
||||||
lockedCacheDirs.remove(cacheDir.getAbsoluteFile());
|
if (!cacheFolderLockingDisabled) {
|
||||||
|
lockedCacheDirs.remove(cacheDir.getAbsoluteFile());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -296,17 +296,17 @@ public class SimpleCacheTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test
|
@Test
|
||||||
// public void testMultipleSimpleCacheWithSameCacheDirThrowsException() throws Exception {
|
public void testMultipleSimpleCacheWithSameCacheDirThrowsException() throws Exception {
|
||||||
// new SimpleCache(cacheDir, new NoOpCacheEvictor());
|
new SimpleCache(cacheDir, new NoOpCacheEvictor());
|
||||||
//
|
|
||||||
// try {
|
try {
|
||||||
// new SimpleCache(cacheDir, new NoOpCacheEvictor());
|
new SimpleCache(cacheDir, new NoOpCacheEvictor());
|
||||||
// assertWithMessage("Exception was expected").fail();
|
assertWithMessage("Exception was expected").fail();
|
||||||
// } catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
// // Expected. Do nothing.
|
// Expected. Do nothing.
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMultipleSimpleCacheWithSameCacheDirDoesNotThrowsExceptionAfterRelease()
|
public void testMultipleSimpleCacheWithSameCacheDirDoesNotThrowsExceptionAfterRelease()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user