Cache
@UnstableApi
public interface Cache
SimpleCache |
A |
A cache that supports partial caching of resources.
Terminology
- A resource is a complete piece of logical data, for example a complete media file.
- A cache key uniquely identifies a resource. URIs are often suitable for use as cache keys, however this is not always the case. URIs are not suitable when caching resources obtained from a service that generates multiple URIs for the same underlying resource, for example because the service uses expiring URIs as a form of access control.
- A cache span is a byte range within a resource, which may or may not be cached. A cache span that's not cached is called a hole span. A cache span that is cached corresponds to a single underlying file in the cache.
Summary
Nested types |
|---|
public class Cache.CacheException extends IOExceptionThrown when an error is encountered when writing data. |
public interface Cache.ListenerListener of |
Constants |
|
|---|---|
default static final long |
UID_UNSET = -1Returned by |
Public methods |
|
|---|---|
abstract NavigableSet<CacheSpan> |
addListener(String key, Cache.Listener listener)Registers a listener to listen for changes to a given resource. |
abstract void |
@WorkerThreadApplies |
abstract void |
@WorkerThreadCommits a file into the cache. |
abstract long |
Returns the total disk space in bytes used by the cache. |
abstract long |
getCachedBytes(String key, long position, long length)Returns the total number of cached bytes between |
abstract long |
getCachedLength(String key, long position, long length)Returns the length of continuously cached data starting from |
abstract NavigableSet<CacheSpan> |
getCachedSpans(String key)Returns the cached spans for a given resource. |
abstract ContentMetadata |
getContentMetadata(String key)Returns a |
abstract Set<String> |
getKeys()Returns the cache keys of all of the resources that are at least partially cached. |
abstract long |
getUid()Returns a non-negative unique identifier for the cache, or |
abstract boolean |
Returns whether the specified range of data in a resource is fully cached. |
abstract void |
Releases the cache. |
abstract void |
releaseHoleSpan(CacheSpan holeSpan)Releases a |
abstract void |
removeListener(String key, Cache.Listener listener)Unregisters a listener. |
abstract void |
Removes all |
abstract void |
@WorkerThreadRemoves a cached |
abstract File |
@WorkerThreadObtains a cache file into which data can be written. |
abstract CacheSpan |
@WorkerThreadA caller should invoke this method when they require data starting from a given position in a given resource. |
abstract @Nullable CacheSpan |
@WorkerThreadSame as |
Constants
Public methods
addListener
abstract NavigableSet<CacheSpan> addListener(String key, Cache.Listener listener)
Registers a listener to listen for changes to a given resource.
No 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.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
Cache.Listener listener |
The listener to add. |
| Returns | |
|---|---|
NavigableSet<CacheSpan> |
The current spans for the resource. |
applyContentMetadataMutations
@WorkerThread
abstract void applyContentMetadataMutations(
String key,
ContentMetadataMutations mutations
)
Applies mutations to the ContentMetadata for the given resource. A new CachedContent is added if there isn't one already for the resource.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
ContentMetadataMutations mutations |
Contains mutations to be applied to the metadata. |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
commitFile
@WorkerThread
abstract void commitFile(File file, long length)
Commits a file into the cache. Must only be called when holding a corresponding hole CacheSpan obtained from startReadWrite.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
File file |
A newly written cache file. |
long length |
The length of the newly written cache file in bytes. |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
getCacheSpace
abstract long getCacheSpace()
Returns the total disk space in bytes used by the cache.
getCachedBytes
abstract long getCachedBytes(String key, long position, long length)
Returns the total number of cached bytes between position (inclusive) and
(position + length) (exclusive) of a resource.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
long position |
The starting position of the data in the resource. |
long length |
The length of the data to check. |
| Returns | |
|---|---|
long |
The total number of cached bytes. |
getCachedLength
abstract long getCachedLength(String key, long position, long length)
Returns the length of continuously cached data starting from position, up to a maximum of maxLength, of a resource. If position isn't cached then -holeLength is returned, where holeLength is the length of continuously uncached data starting from position, up to a maximum of maxLength.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
long position |
The starting position of the data in the resource. |
long length |
The maximum length of the data or hole to be returned. |
| Returns | |
|---|---|
long |
The length of the continuously cached data, or |
getCachedSpans
abstract NavigableSet<CacheSpan> getCachedSpans(String key)
Returns the cached spans for a given resource.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
| Returns | |
|---|---|
NavigableSet<CacheSpan> |
The spans for the key. |
getContentMetadata
abstract ContentMetadata getContentMetadata(String key)
Returns a ContentMetadata for the given resource.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
| Returns | |
|---|---|
ContentMetadata |
The |
getKeys
abstract Set<String> getKeys()
Returns the cache keys of all of the resources that are at least partially cached.
getUid
abstract long getUid()
Returns a non-negative unique identifier for the cache, or UID_UNSET if initialization failed before the unique identifier was determined.
Implementations are expected to generate and store the unique identifier alongside the cached content. If the location of the cache is deleted or swapped, it is expected that a new unique identifier will be generated when the cache is recreated.
isCached
abstract boolean isCached(String key, long position, long length)
Returns whether the specified range of data in a resource is fully cached.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
long position |
The starting position of the data in the resource. |
long length |
The length of the data. |
| Returns | |
|---|---|
boolean |
true if the data is available in the Cache otherwise false; |
release
@WorkerThread
abstract void release()
Releases the cache. This method must be called when the cache is no longer required. The cache must not be used after calling this method.
This method may be slow and shouldn't normally be called on the main thread.
releaseHoleSpan
abstract void releaseHoleSpan(CacheSpan holeSpan)
Releases a CacheSpan obtained from startReadWrite which corresponded to a hole in the cache.
removeListener
abstract void removeListener(String key, Cache.Listener listener)
Unregisters a listener.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
Cache.Listener listener |
The listener to remove. |
removeResource
@WorkerThread
abstract void removeResource(String key)
Removes all CacheSpans for a resource, deleting the underlying files.
| Parameters | |
|---|---|
String key |
The cache key of the resource being removed. |
removeSpan
@WorkerThread
abstract void removeSpan(CacheSpan span)
Removes a cached CacheSpan from the cache, deleting the underlying file.
This method may be slow and shouldn't normally be called on the main thread.
startFile
@WorkerThread
abstract File startFile(String key, long position, long length)
Obtains a cache file into which data can be written. Must only be called when holding a corresponding hole CacheSpan obtained from startReadWrite.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
String key |
The cache key of the resource being written. |
long position |
The starting position in the resource from which data will be written. |
long length |
The length of the data being written, or |
| Returns | |
|---|---|
File |
The file into which data should be written. |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
startReadWrite
@WorkerThread
abstract CacheSpan startReadWrite(String key, long position, long length)
A caller should invoke this method when they require data starting from a given position in a given resource.
If there is a cache entry that overlaps the position, then the returned CacheSpan defines the file in which the data is stored. isCached is true. The caller may read from the cache file, but does not acquire any locks.
If there is no cache entry overlapping position, 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. When the caller has finished writing, it must release the lock by calling releaseHoleSpan.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
long position |
The starting position in the resource from which data is required. |
long length |
The length of the data being requested, or |
| Throws | |
|---|---|
java.lang.InterruptedException |
If the thread was interrupted. |
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
startReadWriteNonBlocking
@WorkerThread
abstract @Nullable CacheSpan startReadWriteNonBlocking(String key, long position, long length)
Same as startReadWrite. However, if the cache entry is locked, then instead of blocking, this method will return null as the CacheSpan.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
String key |
The cache key of the resource. |
long position |
The starting position in the resource from which data is required. |
long length |
The length of the data being requested, or |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |