Move default CacheKeyFactory to CacheKeyFactory.DEFAULT

PiperOrigin-RevId: 310940572
This commit is contained in:
olly 2020-05-11 18:24:57 +01:00 committed by Oliver Woodman
parent 8ae8bf7b21
commit dbff731d1f
6 changed files with 57 additions and 51 deletions

View file

@ -64,7 +64,7 @@ public final class CacheDataSource implements DataSource {
public Factory() {
cacheReadDataSourceFactory = new FileDataSource.Factory();
cacheKeyFactory = CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
cacheKeyFactory = CacheKeyFactory.DEFAULT;
}
/**
@ -123,7 +123,7 @@ public final class CacheDataSource implements DataSource {
/**
* Sets the {@link CacheKeyFactory}.
*
* <p>The default is {@link CacheUtil#DEFAULT_CACHE_KEY_FACTORY}.
* <p>The default is {@link CacheKeyFactory#DEFAULT}.
*
* @param cacheKeyFactory The {@link CacheKeyFactory}.
* @return This factory.
@ -508,8 +508,7 @@ public final class CacheDataSource implements DataSource {
@Nullable EventListener eventListener) {
this.cache = cache;
this.cacheReadDataSource = cacheReadDataSource;
this.cacheKeyFactory =
cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY;
this.cacheKeyFactory = cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT;
this.blockOnCache = (flags & FLAG_BLOCK_ON_CACHE) != 0;
this.ignoreCacheOnError = (flags & FLAG_IGNORE_CACHE_ON_ERROR) != 0;
this.ignoreCacheForUnsetLengthRequests =

View file

@ -20,6 +20,10 @@ import com.google.android.exoplayer2.upstream.DataSpec;
/** Factory for cache keys. */
public interface CacheKeyFactory {
/** Default {@link CacheKeyFactory}. */
CacheKeyFactory DEFAULT =
(dataSpec) -> dataSpec.key != null ? dataSpec.key : dataSpec.uri.toString();
/**
* Returns a cache key for the given {@link DataSpec}.
*

View file

@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.upstream.cache;
import android.net.Uri;
import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
@ -55,18 +54,9 @@ public final class CacheUtil {
/** Default buffer size to be used while caching. */
public static final int DEFAULT_BUFFER_SIZE_BYTES = 128 * 1024;
/** Default {@link CacheKeyFactory}. */
public static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY =
(dataSpec) -> dataSpec.key != null ? dataSpec.key : generateKey(dataSpec.uri);
/**
* Generates a cache key out of the given {@link Uri}.
*
* @param uri Uri of a content which the requested key is for.
*/
public static String generateKey(Uri uri) {
return uri.toString();
}
/** @deprecated Use {@link CacheKeyFactory#DEFAULT}. */
@Deprecated
public static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY = CacheKeyFactory.DEFAULT;
/**
* Queries the cache to obtain the request length and the number of bytes already cached for a
@ -375,7 +365,7 @@ public final class CacheUtil {
private static String buildCacheKey(
DataSpec dataSpec, @Nullable CacheKeyFactory cacheKeyFactory) {
return (cacheKeyFactory != null ? cacheKeyFactory : DEFAULT_CACHE_KEY_FACTORY)
return (cacheKeyFactory != null ? cacheKeyFactory : CacheKeyFactory.DEFAULT)
.buildCacheKey(dataSpec);
}

View file

@ -75,7 +75,7 @@ public final class CacheDataSourceTest {
boundedDataSpec = buildDataSpec(/* unbounded= */ false, /* key= */ null);
unboundedDataSpecWithKey = buildDataSpec(/* unbounded= */ true, DATASPEC_KEY);
boundedDataSpecWithKey = buildDataSpec(/* unbounded= */ false, DATASPEC_KEY);
defaultCacheKey = CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(unboundedDataSpec);
defaultCacheKey = CacheKeyFactory.DEFAULT.buildCacheKey(unboundedDataSpec);
customCacheKey = "customKey." + defaultCacheKey;
cacheKeyFactory = dataSpec -> customCacheKey;

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.upstream.cache;
import static com.google.android.exoplayer2.upstream.cache.CacheKeyFactory.DEFAULT;
import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.upstream.DataSpec;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests {@link CacheKeyFactoryTest}. */
@RunWith(AndroidJUnit4.class)
public class CacheKeyFactoryTest {
@Test
public void default_dataSpecWithKey_returnsKey() {
Uri testUri = Uri.parse("test");
String key = "key";
DataSpec dataSpec = new DataSpec.Builder().setUri(testUri).setKey(key).build();
assertThat(DEFAULT.buildCacheKey(dataSpec)).isEqualTo(key);
}
@Test
public void default_dataSpecWithoutKey_returnsUri() {
Uri testUri = Uri.parse("test");
DataSpec dataSpec = new DataSpec.Builder().setUri(testUri).build();
assertThat(DEFAULT.buildCacheKey(dataSpec)).isEqualTo(testUri.toString());
}
}

View file

@ -15,7 +15,6 @@
*/
package com.google.android.exoplayer2.upstream.cache;
import static com.google.android.exoplayer2.C.LENGTH_UNSET;
import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCacheEmpty;
import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCachedData;
import static com.google.common.truth.Truth.assertThat;
@ -104,37 +103,6 @@ public final class CacheUtilTest {
Util.recursiveDelete(tempFolder);
}
@Test
public void generateKey() {
assertThat(CacheUtil.generateKey(Uri.EMPTY)).isNotNull();
Uri testUri = Uri.parse("test");
String key = CacheUtil.generateKey(testUri);
assertThat(key).isNotNull();
// Should generate the same key for the same input.
assertThat(CacheUtil.generateKey(testUri)).isEqualTo(key);
// Should generate different key for different input.
assertThat(key.equals(CacheUtil.generateKey(Uri.parse("test2")))).isFalse();
}
@Test
public void defaultCacheKeyFactory_buildCacheKey() {
Uri testUri = Uri.parse("test");
String key = "key";
// If DataSpec.key is present, returns it.
assertThat(
CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(
new DataSpec.Builder().setUri(testUri).setKey(key).build()))
.isEqualTo(key);
// If not generates a new one using DataSpec.uri.
assertThat(
CacheUtil.DEFAULT_CACHE_KEY_FACTORY.buildCacheKey(
new DataSpec(testUri, /* position= */ 0, /* length= */ LENGTH_UNSET)))
.isEqualTo(testUri.toString());
}
@Test
public void getCachedNoData() {
Pair<Long, Long> contentLengthAndBytesCached =