Commit 95bd42d0 by ibaker Committed by Tofunmi Adigun-Hameed

Remove two deprecated `SimpleCache` constructors

Use a non-deprecated constructor that takes a `DatabaseProvider`
instead for better performance.

#minor-release

PiperOrigin-RevId: 532046598
(cherry picked from commit 2dd3c3b31b758f24c9b2ff25479e63a8ddf1dd0d)
parent cc4f10b4
......@@ -136,48 +136,12 @@ public final class SimpleCache implements Cache {
@Deprecated
@SuppressWarnings("deprecation")
public SimpleCache(File cacheDir, CacheEvictor evictor) {
this(cacheDir, evictor, null, false);
}
/**
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
* the directory cannot be used to store other files.
*
* @param cacheDir A dedicated cache directory.
* @param evictor The evictor to be used. For download use cases where cache eviction should not
* occur, use {@link NoOpCacheEvictor}.
* @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
* The key must be 16 bytes long.
* @deprecated Use a constructor that takes a {@link DatabaseProvider} for improved performance.
*/
@Deprecated
@SuppressWarnings("deprecation")
public SimpleCache(File cacheDir, CacheEvictor evictor, @Nullable byte[] secretKey) {
this(cacheDir, evictor, secretKey, secretKey != null);
}
/**
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
* the directory cannot be used to store other files.
*
* @param cacheDir A dedicated cache directory.
* @param evictor The evictor to be used. For download use cases where cache eviction should not
* occur, use {@link NoOpCacheEvictor}.
* @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
* The key must be 16 bytes long.
* @param encrypt Whether the index will be encrypted when written. Must be false if {@code
* secretKey} is null.
* @deprecated Use a constructor that takes a {@link DatabaseProvider} for improved performance.
*/
@Deprecated
public SimpleCache(
File cacheDir, CacheEvictor evictor, @Nullable byte[] secretKey, boolean encrypt) {
this(
cacheDir,
evictor,
/* databaseProvider= */ null,
secretKey,
encrypt,
/* legacyIndexSecretKey= */ null,
/* legacyIndexEncrypt= */ false,
/* preferLegacyIndex= */ true);
}
......
......@@ -44,7 +44,6 @@ import org.mockito.Mockito;
@RunWith(AndroidJUnit4.class)
public class SimpleCacheTest {
private static final byte[] ENCRYPTED_INDEX_KEY = Util.getUtf8Bytes("Bar12345Bar12345");
private static final String KEY_1 = "key1";
private static final String KEY_2 = "key2";
......@@ -205,61 +204,6 @@ public class SimpleCacheTest {
}
@Test
@SuppressWarnings("deprecation") // Encrypted index is deprecated
public void newInstance_withEncryptedIndex() throws Exception {
SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY);
CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET);
addCache(simpleCache, KEY_1, 0, 15);
simpleCache.releaseHoleSpan(holeSpan);
simpleCache.release();
// Create a new instance pointing to the same directory.
simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY);
// Read the cached data back.
CacheSpan fileSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET);
assertCachedDataReadCorrect(fileSpan);
}
@Test
@SuppressWarnings("deprecation") // Encrypted index is deprecated
public void newInstance_withEncryptedIndexAndWrongKey_clearsCache() throws Exception {
SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY);
// Write data.
CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET);
addCache(simpleCache, KEY_1, 0, 15);
simpleCache.releaseHoleSpan(holeSpan);
simpleCache.release();
// Create a new instance pointing to the same directory, with a different key.
simpleCache = getEncryptedSimpleCache(Util.getUtf8Bytes("Foo12345Foo12345"));
// Cache should be cleared.
assertThat(simpleCache.getKeys()).isEmpty();
assertNoCacheFiles(cacheDir);
}
@Test
@SuppressWarnings("deprecation") // Encrypted index is deprecated
public void newInstance_withEncryptedIndexAndNoKey_clearsCache() throws Exception {
SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY);
// Write data.
CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET);
addCache(simpleCache, KEY_1, 0, 15);
simpleCache.releaseHoleSpan(holeSpan);
simpleCache.release();
// Create a new instance pointing to the same directory, with no key.
simpleCache = getSimpleCache();
// Cache should be cleared.
assertThat(simpleCache.getKeys()).isEmpty();
assertNoCacheFiles(cacheDir);
}
@Test
public void write_oneLock_oneFile_thenRead() throws Exception {
SimpleCache simpleCache = getSimpleCache();
......@@ -689,12 +633,6 @@ public class SimpleCacheTest {
return new SimpleCache(cacheDir, new NoOpCacheEvictor(), databaseProvider);
}
@Deprecated
@SuppressWarnings("deprecation") // Testing deprecated behaviour.
private SimpleCache getEncryptedSimpleCache(byte[] secretKey) {
return new SimpleCache(cacheDir, new NoOpCacheEvictor(), secretKey);
}
private static void addCache(SimpleCache simpleCache, String key, int position, int length)
throws IOException {
File file = simpleCache.startFile(key, position, length);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment