Cache.CacheException, Cache.Listener| Constructor and Description |
|---|
SimpleCache(java.io.File cacheDir,
CacheEvictor evictor)
Constructs the cache.
|
SimpleCache(java.io.File cacheDir,
CacheEvictor evictor,
byte[] secretKey)
Constructs the cache.
|
SimpleCache(java.io.File cacheDir,
CacheEvictor evictor,
byte[] secretKey,
boolean encrypt)
Constructs the cache.
|
| Modifier and Type | Method and Description |
|---|---|
java.util.NavigableSet<CacheSpan> |
addListener(java.lang.String key,
Cache.Listener listener)
Registers a listener to listen for changes to a given key.
|
void |
applyContentMetadataMutations(java.lang.String key,
ContentMetadataMutations mutations)
Applies
mutations to the ContentMetadata for the given key. |
void |
commitFile(java.io.File file)
Commits a file into the cache.
|
static void |
disableCacheFolderLocking()
Deprecated.
Don't create multiple
SimpleCache instances for the same cache folder. If
you need to create another instance, make sure you call release() on the previous
instance. |
long |
getCachedLength(java.lang.String key,
long position,
long length)
Returns the length of the cached data block starting from the
position to the block end
up to length bytes. |
java.util.NavigableSet<CacheSpan> |
getCachedSpans(java.lang.String key)
Returns the cached spans for a given cache key.
|
long |
getCacheSpace()
Returns the total disk space in bytes used by the cache.
|
long |
getContentLength(java.lang.String key)
Returns the content length for the given key if one set, or
C.LENGTH_UNSET otherwise. |
ContentMetadata |
getContentMetadata(java.lang.String key)
Returns a
ContentMetadata for the given key. |
java.util.Set<java.lang.String> |
getKeys()
Returns all keys in the cache.
|
boolean |
isCached(java.lang.String key,
long position,
long length)
Queries if a range is entirely available in the cache.
|
static boolean |
isCacheFolderLocked(java.io.File cacheFolder)
Returns whether
cacheFolder is locked by a SimpleCache instance. |
void |
release()
Releases the cache.
|
void |
releaseHoleSpan(CacheSpan holeSpan)
Releases a
CacheSpan obtained from Cache.startReadWrite(String, long) which
corresponded to a hole in the cache. |
void |
removeListener(java.lang.String key,
Cache.Listener listener)
Unregisters a listener.
|
void |
removeSpan(CacheSpan span)
Removes a cached
CacheSpan from the cache, deleting the underlying file. |
void |
setContentLength(java.lang.String key,
long length)
Sets the content length for the given key.
|
java.io.File |
startFile(java.lang.String key,
long position,
long maxLength)
Obtains a cache file into which data can be written.
|
com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan |
startReadWrite(java.lang.String key,
long position)
A caller should invoke this method when they require data from a given position for a given
key.
|
com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan |
startReadWriteNonBlocking(java.lang.String key,
long position)
Same as
Cache.startReadWrite(String, long). |
public SimpleCache(java.io.File cacheDir,
CacheEvictor evictor)
cacheDir - A dedicated cache directory.evictor - The evictor to be used.public SimpleCache(java.io.File cacheDir,
CacheEvictor evictor,
byte[] secretKey)
cacheDir - A dedicated cache directory.evictor - The evictor to be used.secretKey - If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
The key must be 16 bytes long.public SimpleCache(java.io.File cacheDir,
CacheEvictor evictor,
byte[] secretKey,
boolean encrypt)
cacheDir - A dedicated cache directory.evictor - The evictor to be used.secretKey - If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
The key must be 16 bytes long.encrypt - Whether the index will be encrypted when written. Must be false if secretKey is null.public static boolean isCacheFolderLocked(java.io.File cacheFolder)
cacheFolder is locked by a SimpleCache instance. To unlock the
folder the SimpleCache instance should be released.@Deprecated public static void disableCacheFolderLocking()
SimpleCache instances for the same cache folder. If
you need to create another instance, make sure you call release() on the previous
instance.SimpleCache instances are using and releases
any previous lock.
The locking prevents multiple SimpleCache instances from being created for the same
folder. Disabling it may cause the cache data to be corrupted. Use at your own risk.
public void release()
throws Cache.CacheException
Cacherelease in interface CacheCache.CacheExceptionpublic java.util.NavigableSet<CacheSpan> addListener(java.lang.String key, Cache.Listener listener)
CacheNo guarantees are made about the thread or threads on which the listener is called, but it is guaranteed that listener methods will be called in a serial fashion (i.e. one at a time) and in the same order as events occurred.
addListener in interface Cachekey - The key to listen to.listener - The listener to add.public void removeListener(java.lang.String key,
Cache.Listener listener)
CacheremoveListener in interface Cachekey - The key to stop listening to.listener - The listener to remove.@NonNull public java.util.NavigableSet<CacheSpan> getCachedSpans(java.lang.String key)
CachegetCachedSpans in interface Cachekey - The key for which spans should be returned.public java.util.Set<java.lang.String> getKeys()
Cachepublic long getCacheSpace()
CachegetCacheSpace in interface Cachepublic com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan startReadWrite(java.lang.String key,
long position)
throws java.lang.InterruptedException,
Cache.CacheException
CacheIf there is a cache entry that overlaps the position, then the returned CacheSpan
defines the file in which the data is stored. CacheSpan.isCached is true. The caller
may read from the cache file, but does not acquire any locks.
If there is no cache entry overlapping offset, then the returned CacheSpan
defines a hole in the cache starting at position into which the caller may write as it
obtains the data from some other source. The returned CacheSpan serves as a lock.
Whilst the caller holds the lock it may write data into the hole. It may split data into
multiple files. When the caller has finished writing a file it should commit it to the cache by
calling Cache.commitFile(File). When the caller has finished writing, it must release the
lock by calling Cache.releaseHoleSpan(com.google.android.exoplayer2.upstream.cache.CacheSpan).
startReadWrite in interface Cachekey - The key of the data being requested.position - The position of the data being requested.CacheSpan.java.lang.InterruptedException - If the thread was interrupted.Cache.CacheException - If an error is encountered.public com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan startReadWriteNonBlocking(java.lang.String key,
long position)
throws Cache.CacheException
CacheCache.startReadWrite(String, long). However, if the cache entry is locked, then
instead of blocking, this method will return null as the CacheSpan.startReadWriteNonBlocking in interface Cachekey - The key of the data being requested.position - The position of the data being requested.CacheSpan. Or null if the cache entry is locked.Cache.CacheException - If an error is encountered.public java.io.File startFile(java.lang.String key,
long position,
long maxLength)
throws Cache.CacheException
CacheCacheSpan obtained from Cache.startReadWrite(String, long).startFile in interface Cachekey - The cache key for the data.position - The starting position of the data.maxLength - The maximum length of the data to be written. Used only to ensure that there
is enough space in the cache.Cache.CacheException - If an error is encountered.public void commitFile(java.io.File file)
throws Cache.CacheException
CacheCacheSpan obtained from Cache.startReadWrite(String, long)commitFile in interface Cachefile - A newly written cache file.Cache.CacheException - If an error is encountered.public void releaseHoleSpan(CacheSpan holeSpan)
CacheCacheSpan obtained from Cache.startReadWrite(String, long) which
corresponded to a hole in the cache.releaseHoleSpan in interface CacheholeSpan - The CacheSpan being released.public void removeSpan(CacheSpan span) throws Cache.CacheException
CacheCacheSpan from the cache, deleting the underlying file.removeSpan in interface Cachespan - The CacheSpan to remove.Cache.CacheException - If an error is encountered.public boolean isCached(java.lang.String key,
long position,
long length)
Cachepublic long getCachedLength(java.lang.String key,
long position,
long length)
Cacheposition to the block end
up to length bytes. If the position isn't cached then -(the length of the gap
to the next cached data up to length bytes) is returned.getCachedLength in interface Cachekey - The cache key for the data.position - The starting position of the data.length - The maximum length of the data to be returned.public void setContentLength(java.lang.String key,
long length)
throws Cache.CacheException
CachesetContentLength in interface Cachekey - The cache key for the data.length - The length of the data.Cache.CacheException - If an error is encountered.public long getContentLength(java.lang.String key)
CacheC.LENGTH_UNSET otherwise.getContentLength in interface Cachekey - The cache key for the data.C.LENGTH_UNSET otherwise.public void applyContentMetadataMutations(java.lang.String key,
ContentMetadataMutations mutations)
throws Cache.CacheException
Cachemutations to the ContentMetadata for the given key. A new CachedContent is added if there isn't one already with the given key.applyContentMetadataMutations in interface Cachekey - The cache key for the data.mutations - Contains mutations to be applied to the metadata.Cache.CacheException - If an error is encountered.public ContentMetadata getContentMetadata(java.lang.String key)
CacheContentMetadata for the given key.getContentMetadata in interface Cachekey - The cache key for the data.ContentMetadata for the given key.