SimpleCache
@UnstableApi
class SimpleCache : Cache
A Cache implementation that maintains an in-memory representation.
Only one instance of SimpleCache is allowed for a given directory at a given time.
To delete a SimpleCache, use delete rather than deleting the directory and its contents directly. This is necessary to ensure that associated index data is also removed.
Summary
Public constructors |
|---|
This function is deprecated. Use a constructor that takes a |
SimpleCache(Constructs the cache. |
SimpleCache(Constructs the cache. |
Public functions |
|
|---|---|
synchronized NavigableSet<CacheSpan!>! |
addListener(key: String!, listener: Cache.Listener!)Registers a listener to listen for changes to a given resource. |
synchronized Unit |
applyContentMetadataMutations(Applies |
synchronized Unit |
Checks whether the cache was initialized successfully. |
synchronized Unit |
commitFile(file: File!, length: Long)Commits a file into the cache. |
java-static Unit |
@WorkerThreadDeletes all content belonging to a cache instance. |
synchronized Long |
Returns the total disk space in bytes used by the cache. |
synchronized Long |
getCachedBytes(key: String!, position: Long, length: Long)Returns the total number of cached bytes between |
synchronized Long |
getCachedLength(key: String!, position: Long, length: Long)Returns the length of continuously cached data starting from |
synchronized NavigableSet<CacheSpan!>! |
getCachedSpans(key: String!)Returns the cached spans for a given resource. |
synchronized ContentMetadata! |
getContentMetadata(key: String!)Returns a |
synchronized (Mutable)Set<String!>! |
getKeys()Returns the cache keys of all of the resources that are at least partially cached. |
synchronized java-static Boolean |
isCacheFolderLocked(cacheFolder: File!)Returns whether |
synchronized Boolean |
Returns whether the specified range of data in a resource is fully cached. |
synchronized Unit |
release()Releases the cache. |
synchronized Unit |
releaseHoleSpan(holeSpan: CacheSpan!)Releases a |
synchronized Unit |
removeListener(key: String!, listener: Cache.Listener!)Unregisters a listener. |
synchronized Unit |
removeResource(key: String!)Removes all |
synchronized Unit |
removeSpan(span: CacheSpan!)Removes a cached |
synchronized File! |
Obtains a cache file into which data can be written. |
synchronized CacheSpan! |
startReadWrite(key: String!, position: Long, length: Long)A caller should invoke this method when they require data starting from a given position in a given resource. |
synchronized CacheSpan? |
startReadWriteNonBlocking(key: String!, position: Long, length: Long)Same as |
Inherited Constants |
|---|
Public constructors
SimpleCache
SimpleCache(cacheDir: File!, evictor: CacheEvictor!)
Constructs the cache. The cache will delete any unrecognized files from the directory. Hence the directory cannot be used to store other files.
| Parameters | |
|---|---|
cacheDir: File! |
A dedicated cache directory. |
evictor: CacheEvictor! |
The evictor to be used. For download use cases where cache eviction should not occur, use |
SimpleCache
SimpleCache(
cacheDir: File!,
evictor: CacheEvictor!,
databaseProvider: DatabaseProvider!
)
Constructs the cache. The cache will delete any unrecognized files from the directory. Hence the directory cannot be used to store other files.
| Parameters | |
|---|---|
cacheDir: File! |
A dedicated cache directory. |
evictor: CacheEvictor! |
The evictor to be used. For download use cases where cache eviction should not occur, use |
databaseProvider: DatabaseProvider! |
Provides the database in which the cache index is stored. |
SimpleCache
SimpleCache(
cacheDir: File!,
evictor: CacheEvictor!,
databaseProvider: DatabaseProvider?,
legacyIndexSecretKey: ByteArray?,
legacyIndexEncrypt: Boolean,
preferLegacyIndex: Boolean
)
Constructs the cache. The cache will delete any unrecognized files from the cache directory. Hence the directory cannot be used to store other files.
| Parameters | |
|---|---|
cacheDir: File! |
A dedicated cache directory. |
evictor: CacheEvictor! |
The evictor to be used. For download use cases where cache eviction should not occur, use |
databaseProvider: DatabaseProvider? |
Provides the database in which the cache index is stored, or |
legacyIndexSecretKey: ByteArray? |
A 16 byte AES key for reading, and optionally writing, the legacy index. Not used by the database index, however should still be provided when using the database index in cases where upgrading from the legacy index may be necessary. |
legacyIndexEncrypt: Boolean |
Whether to encrypt when writing to the legacy index. Must be |
preferLegacyIndex: Boolean |
Whether to use the legacy index even if a |
Public functions
addListener
synchronized fun addListener(key: String!, listener: Cache.Listener!): NavigableSet<CacheSpan!>!
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 | |
|---|---|
key: String! |
The cache key of the resource. |
listener: Cache.Listener! |
The listener to add. |
| Returns | |
|---|---|
NavigableSet<CacheSpan!>! |
The current spans for the resource. |
applyContentMetadataMutations
synchronized fun applyContentMetadataMutations(
key: String!,
mutations: ContentMetadataMutations!
): Unit
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 | |
|---|---|
key: String! |
The cache key of the resource. |
mutations: ContentMetadataMutations! |
Contains mutations to be applied to the metadata. |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
checkInitialization
synchronized fun checkInitialization(): Unit
Checks whether the cache was initialized successfully.
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error occurred during initialization. |
commitFile
synchronized fun commitFile(file: File!, length: Long): Unit
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. |
length: Long |
The length of the newly written cache file in bytes. |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |
delete
@WorkerThread
java-static fun delete(cacheDir: File!, databaseProvider: DatabaseProvider?): Unit
Deletes all content belonging to a cache instance.
This method may be slow and shouldn't normally be called on the main thread.
| Parameters | |
|---|---|
cacheDir: File! |
The cache directory. |
databaseProvider: DatabaseProvider? |
The database in which index data is stored, or |
getCacheSpace
synchronized fun getCacheSpace(): Long
Returns the total disk space in bytes used by the cache.
getCachedBytes
synchronized fun getCachedBytes(key: String!, position: Long, length: Long): Long
Returns the total number of cached bytes between position (inclusive) and
(position + length) (exclusive) of a resource.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource. |
position: Long |
The starting position of the data in the resource. |
length: Long |
The length of the data to check. |
| Returns | |
|---|---|
Long |
The total number of cached bytes. |
getCachedLength
synchronized fun getCachedLength(key: String!, position: Long, length: Long): Long
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 | |
|---|---|
key: String! |
The cache key of the resource. |
position: Long |
The starting position of the data in the resource. |
length: Long |
The maximum length of the data or hole to be returned. |
| Returns | |
|---|---|
Long |
The length of the continuously cached data, or |
getCachedSpans
synchronized fun getCachedSpans(key: String!): NavigableSet<CacheSpan!>!
Returns the cached spans for a given resource.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource. |
| Returns | |
|---|---|
NavigableSet<CacheSpan!>! |
The spans for the key. |
getContentMetadata
synchronized fun getContentMetadata(key: String!): ContentMetadata!
Returns a ContentMetadata for the given resource.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource. |
| Returns | |
|---|---|
ContentMetadata! |
The |
getKeys
synchronized fun getKeys(): (Mutable)Set<String!>!
Returns the cache keys of all of the resources that are at least partially cached.
isCacheFolderLocked
synchronized java-static fun isCacheFolderLocked(cacheFolder: File!): Boolean
Returns whether cacheFolder is locked by a SimpleCache instance. To unlock the folder the SimpleCache instance should be released.
isCached
synchronized fun isCached(key: String!, position: Long, length: Long): Boolean
Returns whether the specified range of data in a resource is fully cached.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource. |
position: Long |
The starting position of the data in the resource. |
length: Long |
The length of the data. |
| Returns | |
|---|---|
Boolean |
true if the data is available in the Cache otherwise false; |
release
synchronized fun release(): Unit
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
synchronized fun releaseHoleSpan(holeSpan: CacheSpan!): Unit
Releases a CacheSpan obtained from startReadWrite which corresponded to a hole in the cache.
removeListener
synchronized fun removeListener(key: String!, listener: Cache.Listener!): Unit
Unregisters a listener.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource. |
listener: Cache.Listener! |
The listener to remove. |
removeResource
synchronized fun removeResource(key: String!): Unit
Removes all CacheSpans for a resource, deleting the underlying files.
| Parameters | |
|---|---|
key: String! |
The cache key of the resource being removed. |
removeSpan
synchronized fun removeSpan(span: CacheSpan!): Unit
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
synchronized fun startFile(key: String!, position: Long, length: Long): File!
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 | |
|---|---|
key: String! |
The cache key of the resource being written. |
position: Long |
The starting position in the resource from which data will be written. |
length: Long |
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
synchronized fun startReadWrite(key: String!, position: Long, length: Long): CacheSpan!
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 | |
|---|---|
key: String! |
The cache key of the resource. |
position: Long |
The starting position in the resource from which data is required. |
length: Long |
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
synchronized fun startReadWriteNonBlocking(key: String!, position: Long, length: Long): CacheSpan?
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 | |
|---|---|
key: String! |
The cache key of the resource. |
position: Long |
The starting position in the resource from which data is required. |
length: Long |
The length of the data being requested, or |
| Throws | |
|---|---|
androidx.media3.datasource.cache.Cache.CacheException |
If an error is encountered. |