Commit 92d34cd8 by cblay Committed by Oliver Woodman

Add flag to CacheDataSource to disable caching unset length requests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139377417
parent d6eb9cb7
...@@ -49,7 +49,8 @@ public final class CacheDataSource implements DataSource { ...@@ -49,7 +49,8 @@ public final class CacheDataSource implements DataSource {
* Flags controlling the cache's behavior. * Flags controlling the cache's behavior.
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef(flag = true, value = {FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR}) @IntDef(flag = true, value = {FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR,
FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS})
public @interface Flags {} public @interface Flags {}
/** /**
* A flag indicating whether we will block reads if the cache key is locked. If this flag is * A flag indicating whether we will block reads if the cache key is locked. If this flag is
...@@ -65,6 +66,11 @@ public final class CacheDataSource implements DataSource { ...@@ -65,6 +66,11 @@ public final class CacheDataSource implements DataSource {
public static final int FLAG_IGNORE_CACHE_ON_ERROR = 1 << 1; public static final int FLAG_IGNORE_CACHE_ON_ERROR = 1 << 1;
/** /**
* A flag indicating that the cache should be bypassed for requests whose lengths are unset.
*/
public static final int FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS = 1 << 2;
/**
* Listener of {@link CacheDataSource} events. * Listener of {@link CacheDataSource} events.
*/ */
public interface EventListener { public interface EventListener {
...@@ -87,6 +93,7 @@ public final class CacheDataSource implements DataSource { ...@@ -87,6 +93,7 @@ public final class CacheDataSource implements DataSource {
private final boolean blockOnCache; private final boolean blockOnCache;
private final boolean ignoreCacheOnError; private final boolean ignoreCacheOnError;
private final boolean ignoreCacheForUnsetLengthRequests;
private DataSource currentDataSource; private DataSource currentDataSource;
private boolean currentRequestUnbounded; private boolean currentRequestUnbounded;
...@@ -146,6 +153,8 @@ public final class CacheDataSource implements DataSource { ...@@ -146,6 +153,8 @@ public final class CacheDataSource implements DataSource {
this.cacheReadDataSource = cacheReadDataSource; this.cacheReadDataSource = cacheReadDataSource;
this.blockOnCache = (flags & FLAG_BLOCK_ON_CACHE) != 0; this.blockOnCache = (flags & FLAG_BLOCK_ON_CACHE) != 0;
this.ignoreCacheOnError = (flags & FLAG_IGNORE_CACHE_ON_ERROR) != 0; this.ignoreCacheOnError = (flags & FLAG_IGNORE_CACHE_ON_ERROR) != 0;
this.ignoreCacheForUnsetLengthRequests =
(flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0;
this.upstreamDataSource = upstream; this.upstreamDataSource = upstream;
if (cacheWriteDataSink != null) { if (cacheWriteDataSink != null) {
this.cacheWriteDataSource = new TeeDataSource(upstream, cacheWriteDataSink); this.cacheWriteDataSource = new TeeDataSource(upstream, cacheWriteDataSink);
...@@ -162,7 +171,8 @@ public final class CacheDataSource implements DataSource { ...@@ -162,7 +171,8 @@ public final class CacheDataSource implements DataSource {
flags = dataSpec.flags; flags = dataSpec.flags;
key = dataSpec.key != null ? dataSpec.key : uri.toString(); key = dataSpec.key != null ? dataSpec.key : uri.toString();
readPosition = dataSpec.position; readPosition = dataSpec.position;
currentRequestIgnoresCache = ignoreCacheOnError && seenCacheError; currentRequestIgnoresCache = (ignoreCacheOnError && seenCacheError)
|| (dataSpec.length == C.LENGTH_UNSET && ignoreCacheForUnsetLengthRequests);
if (dataSpec.length != C.LENGTH_UNSET || currentRequestIgnoresCache) { if (dataSpec.length != C.LENGTH_UNSET || currentRequestIgnoresCache) {
bytesRemaining = dataSpec.length; bytesRemaining = dataSpec.length;
} else { } else {
......
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