public interface Cache
Modifier and Type | Interface and Description |
---|---|
static interface |
Cache.Listener
Interface definition for a callback to be notified of
Cache events. |
Modifier and Type | Method and Description |
---|---|
NavigableSet<CacheSpan> |
addListener(String key,
Cache.Listener listener)
Registers a listener to listen for changes to a given key.
|
void |
commitFile(File file)
Commits a file into the cache.
|
NavigableSet<CacheSpan> |
getCachedSpans(String key)
Returns the cached spans for a given cache key.
|
long |
getCacheSpace()
Returns the total disk space in bytes used by the cache.
|
Set<String> |
getKeys()
Returns all keys in the cache.
|
boolean |
isCached(String key,
long position,
long length)
Queries if a range is entirely available in the cache.
|
void |
releaseHoleSpan(CacheSpan holeSpan)
Releases a
CacheSpan obtained from startReadWrite(String, long) which
corresponded to a hole in the cache. |
void |
removeListener(String key,
Cache.Listener listener)
Unregisters a listener.
|
void |
removeSpan(CacheSpan span)
Removes a cached
CacheSpan from the cache, deleting the underlying file. |
File |
startFile(String key,
long position,
long length)
Obtains a cache file into which data can be written.
|
CacheSpan |
startReadWrite(String key,
long position)
A caller should invoke this method when they require data from a given position for a given
key.
|
CacheSpan |
startReadWriteNonBlocking(String key,
long position)
Same as
startReadWrite(String, long) . |
NavigableSet<CacheSpan> addListener(String key, Cache.Listener listener)
No guarantees are made about the thread or threads on which the listener is invoked, but it is guaranteed that listener methods will be invoked in a serial fashion (i.e. one at a time) and in the same order as events occurred.
key
- The key to listen to.listener
- The listener to add.void removeListener(String key, Cache.Listener listener)
key
- The key to stop listening to.listener
- The listener to remove.NavigableSet<CacheSpan> getCachedSpans(String key)
key
- The key for which spans should be returned.long getCacheSpace()
CacheSpan startReadWrite(String key, long position) throws InterruptedException
If 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 commitFile(File)
. When the caller has finished writing, it must release
the lock by calling releaseHoleSpan(com.google.android.exoplayer.upstream.cache.CacheSpan)
.
key
- The key of the data being requested.position
- The position of the data being requested.CacheSpan
.InterruptedException
CacheSpan startReadWriteNonBlocking(String key, long position)
startReadWrite(String, long)
. However, if the cache entry is locked, then
instead of blocking, this method will return null as the CacheSpan
.key
- The key of the data being requested.position
- The position of the data being requested.CacheSpan
. Or null if the cache entry is locked.File startFile(String key, long position, long length)
CacheSpan
obtained from startReadWrite(String, long)
.key
- The cache key for the data.position
- The starting position of the data.length
- The length of the data to be written. Used only to ensure that there is enough
space in the cache.void commitFile(File file)
CacheSpan
obtained from startReadWrite(String, long)
file
- A newly written cache file.void releaseHoleSpan(CacheSpan holeSpan)
CacheSpan
obtained from startReadWrite(String, long)
which
corresponded to a hole in the cache.holeSpan
- The CacheSpan
being released.void removeSpan(CacheSpan span)
CacheSpan
from the cache, deleting the underlying file.span
- The CacheSpan
to remove.boolean isCached(String key, long position, long length)
key
- The cache key for the data.position
- The starting position of the data.length
- The length of the data.