Back to Exoplayer

SimpleCache (ExoPlayer library)

docs/doc/reference/com/google/android/exoplayer2/upstream/cache/SimpleCache.html

latest29.0 KB
Original Source

Package com.google.android.exoplayer2.upstream.cache

Class SimpleCache

  • java.lang.Object

    • com.google.android.exoplayer2.upstream.cache.SimpleCache
  • All Implemented Interfaces:Cache


[@Deprecated](https://developer.android.com/reference/java/lang/Deprecated.html "class or interface in java.lang")public final classSimpleCacheextends[Object](https://developer.android.com/reference/java/lang/Object.html "class or interface in java.lang")implements[Cache](Cache.html "interface in com.google.android.exoplayer2.upstream.cache")

Deprecated. com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which contains the same ExoPlayer code). See the migration guide for more details, including a script to help with the migration.

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(File, DatabaseProvider) rather than deleting the directory and its contents directly. This is necessary to ensure that associated index data is also removed.

Nested Class Summary

- 

Nested classes/interfaces inherited from interface com.google.android.exoplayer2.upstream.cache.Cache

Cache.CacheException, Cache.Listener

Field Summary

- 

Fields inherited from interface com.google.android.exoplayer2.upstream.cache.Cache

UID_UNSET

Constructor Summary

Constructors | Constructor | Description | | --- | --- | | SimpleCache​(File cacheDir, CacheEvictor evictor) | Deprecated. Use a constructor that takes a DatabaseProvider for improved performance.

| | SimpleCache​(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider) | Deprecated.

Constructs the cache. | | SimpleCache​(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider, byte[] legacyIndexSecretKey, boolean legacyIndexEncrypt, boolean preferLegacyIndex) | Deprecated.

Constructs the cache. |

Method Summary

All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods | Modifier and Type | Method | Description | | --- | --- | --- | | NavigableSet<CacheSpan> | addListener​(String key, Cache.Listener listener) | Deprecated.

Registers a listener to listen for changes to a given resource. | | void | applyContentMetadataMutations​(String key, ContentMetadataMutations mutations) | Deprecated.

Applies mutations to the ContentMetadata for the given resource. | | void | checkInitialization() | Deprecated.

Checks whether the cache was initialized successfully. | | void | commitFile​(File file, long length) | Deprecated.

Commits a file into the cache. | | static void | delete​(File cacheDir, DatabaseProvider databaseProvider) | Deprecated.

Deletes all content belonging to a cache instance. | | long | getCachedBytes​(String key, long position, long length) | Deprecated.

Returns the total number of cached bytes between position (inclusive) and (position + length) (exclusive) of a resource. | | long | getCachedLength​(String key, long position, long length) | Deprecated.

Returns the length of continuously cached data starting from position, up to a maximum of maxLength, of a resource. | | NavigableSet<CacheSpan> | getCachedSpans​(String key) | Deprecated.

Returns the cached spans for a given resource. | | long | getCacheSpace() | Deprecated.

Returns the total disk space in bytes used by the cache. | | ContentMetadata | getContentMetadata​(String key) | Deprecated.

Returns a ContentMetadata for the given resource. | | Set<String> | getKeys() | Deprecated.

Returns the cache keys of all of the resources that are at least partially cached. | | long | getUid() | Deprecated.

Returns a non-negative unique identifier for the cache, or Cache.UID_UNSET if initialization failed before the unique identifier was determined. | | boolean | isCached​(String key, long position, long length) | Deprecated.

Returns whether the specified range of data in a resource is fully cached. | | static boolean | isCacheFolderLocked​(File cacheFolder) | Deprecated.

Returns whether cacheFolder is locked by a SimpleCache instance. | | void | release() | Deprecated.

Releases the cache. | | void | releaseHoleSpan​(CacheSpan holeSpan) | Deprecated.

Releases a CacheSpan obtained from Cache.startReadWrite(String, long, long) which corresponded to a hole in the cache. | | void | removeListener​(String key, Cache.Listener listener) | Deprecated.

Unregisters a listener. | | void | removeResource​(String key) | Deprecated.

Removes all CacheSpans for a resource, deleting the underlying files. | | void | removeSpan​(CacheSpan span) | Deprecated.

Removes a cached CacheSpan from the cache, deleting the underlying file. | | File | startFile​(String key, long position, long length) | Deprecated.

Obtains a cache file into which data can be written. | | CacheSpan | startReadWrite​(String key, long position, long length) | Deprecated.

A caller should invoke this method when they require data starting from a given position in a given resource. | | CacheSpan | startReadWriteNonBlocking​(String key, long position, long length) | Deprecated.

Same as Cache.startReadWrite(String, long, long). |

- 

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

- 

SimpleCache

[@Deprecated](https://developer.android.com/reference/java/lang/Deprecated.html "class or interface in java.lang")public SimpleCache​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")cacheDir,[CacheEvictor](CacheEvictor.html "interface in com.google.android.exoplayer2.upstream.cache")evictor)

Deprecated. Use a constructor that takes a DatabaseProvider for improved performance.

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 - A dedicated cache directory.evictor - The evictor to be used. For download use cases where cache eviction should not occur, use NoOpCacheEvictor.

- 

SimpleCache

public SimpleCache​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")cacheDir,[CacheEvictor](CacheEvictor.html "interface in com.google.android.exoplayer2.upstream.cache")evictor,[DatabaseProvider](../../database/DatabaseProvider.html "interface in com.google.android.exoplayer2.database")databaseProvider)

Deprecated.

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 - A dedicated cache directory.evictor - The evictor to be used. For download use cases where cache eviction should not occur, use NoOpCacheEvictor.databaseProvider - Provides the database in which the cache index is stored.

- 

SimpleCache

public SimpleCache​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")cacheDir,[CacheEvictor](CacheEvictor.html "interface in com.google.android.exoplayer2.upstream.cache")evictor,
                   @Nullable[DatabaseProvider](../../database/DatabaseProvider.html "interface in com.google.android.exoplayer2.database")databaseProvider,
                   @Nullable
                   byte[] legacyIndexSecretKey,
                   boolean legacyIndexEncrypt,
                   boolean preferLegacyIndex)

Deprecated.

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 - A dedicated cache directory.evictor - The evictor to be used. For download use cases where cache eviction should not occur, use NoOpCacheEvictor.databaseProvider - Provides the database in which the cache index is stored, or null to use a legacy index. Using a database index is highly recommended for performance reasons.legacyIndexSecretKey - 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 - Whether to encrypt when writing to the legacy index. Must be false if legacyIndexSecretKey is null. Not used by the database index.preferLegacyIndex - Whether to use the legacy index even if a databaseProvider is provided. Should be false in nearly all cases. Setting this to true is only useful for downgrading from the database index back to the legacy index.

Method Detail

- 

isCacheFolderLocked

public static boolean isCacheFolderLocked​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")cacheFolder)

Deprecated.

Returns whether cacheFolder is locked by a SimpleCache instance. To unlock the folder the SimpleCache instance should be released.

- 

delete

@WorkerThread
public static void delete​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")cacheDir,
                          @Nullable[DatabaseProvider](../../database/DatabaseProvider.html "interface in com.google.android.exoplayer2.database")databaseProvider)

Deprecated.

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 - The cache directory.databaseProvider - The database in which index data is stored, or null if the cache used a legacy index.

- 

checkInitialization

public void checkInitialization()
                         throws[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Checks whether the cache was initialized successfully. Throws:Cache.CacheException - If an error occurred during initialization.

- 

getUid

public long getUid()

Deprecated.

Description copied from interface: Cache

Returns a non-negative unique identifier for the cache, or Cache.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.

Specified by:getUid in interface Cache

- 

release

public void release()

Deprecated.

Description copied from interface: Cache

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.

Specified by:release in interface Cache

- 

addListener

public[NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html "class or interface in java.util")<[CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")> addListener​([String](https://developer.android.com/reference/java/lang/String.html?is-external=true "class or interface in java.lang")key,[Cache.Listener](Cache.Listener.html "interface in com.google.android.exoplayer2.upstream.cache")listener)

Deprecated.

Description copied from interface: Cache

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.

Specified by:addListener in interface CacheParameters:key - The cache key of the resource.listener - The listener to add.Returns:The current spans for the resource.

- 

removeListener

public void removeListener​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,[Cache.Listener](Cache.Listener.html "interface in com.google.android.exoplayer2.upstream.cache")listener)

Deprecated.

Description copied from interface: Cache

Unregisters a listener. Specified by:removeListener in interface CacheParameters:key - The cache key of the resource.listener - The listener to remove.

- 

getCachedSpans

public[NavigableSet](https://developer.android.com/reference/java/util/NavigableSet.html "class or interface in java.util")<[CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")> getCachedSpans​([String](https://developer.android.com/reference/java/lang/String.html?is-external=true "class or interface in java.lang")key)

Deprecated.

Description copied from interface: Cache

Returns the cached spans for a given resource. Specified by:getCachedSpans in interface CacheParameters:key - The cache key of the resource.Returns:The spans for the key.

- 

getKeys

public[Set](https://developer.android.com/reference/java/util/Set.html "class or interface in java.util")<[String](https://developer.android.com/reference/java/lang/String.html?is-external=true "class or interface in java.lang")> getKeys()

Deprecated.

Description copied from interface: Cache

Returns the cache keys of all of the resources that are at least partially cached. Specified by:getKeys in interface Cache

- 

getCacheSpace

public long getCacheSpace()

Deprecated.

Description copied from interface: Cache

Returns the total disk space in bytes used by the cache. Specified by:getCacheSpace in interface Cache

- 

startReadWrite

public[CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")startReadWrite​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,
                                long position,
                                long length)
                         throws[InterruptedException](https://developer.android.com/reference/java/lang/InterruptedException.html "class or interface in java.lang"),[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Description copied from interface: Cache

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. 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 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 Cache.commitFile(File, long). When the caller has finished writing, it must release the lock by calling Cache.releaseHoleSpan(com.google.android.exoplayer2.upstream.cache.CacheSpan).

This method may be slow and shouldn't normally be called on the main thread.

Specified by:startReadWrite in interface CacheParameters:key - The cache key of the resource.position - The starting position in the resource from which data is required.length - The length of the data being requested, or C.LENGTH_UNSET if unbounded. The length is ignored if there is a cache entry that overlaps the position. Else, it defines the maximum length of the hole CacheSpan that's returned. Cache implementations may support parallel writes into non-overlapping holes, and so passing the actual required length should be preferred to passing C.LENGTH_UNSET when possible.Returns:The CacheSpan.Throws:InterruptedException - If the thread was interrupted.Cache.CacheException - If an error is encountered.

- 

startReadWriteNonBlocking

@Nullable
public[CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")startReadWriteNonBlocking​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,
                                           long position,
                                           long length)
                                    throws[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Description copied from interface: Cache

Same as Cache.startReadWrite(String, long, long). 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.

Specified by:startReadWriteNonBlocking in interface CacheParameters:key - The cache key of the resource.position - The starting position in the resource from which data is required.length - The length of the data being requested, or C.LENGTH_UNSET if unbounded. The length is ignored if there is a cache entry that overlaps the position. Else, it defines the range of data locked by the returned CacheSpan.Returns:The CacheSpan. Or null if the cache entry is locked.Throws:Cache.CacheException - If an error is encountered.

- 

startFile

public[File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")startFile​([String](https://developer.android.com/reference/java/lang/String.html?is-external=true "class or interface in java.lang")key,
                      long position,
                      long length)
               throws[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Description copied from interface: Cache

Obtains a cache file into which data can be written. Must only be called when holding a corresponding hole CacheSpan obtained from Cache.startReadWrite(String, long, long).

This method may be slow and shouldn't normally be called on the main thread.

Specified by:startFile in interface CacheParameters:key - The cache key of the resource being written.position - The starting position in the resource from which data will be written.length - The length of the data being written, or C.LENGTH_UNSET if unknown. Used only to ensure that there is enough space in the cache.Returns:The file into which data should be written.Throws:Cache.CacheException - If an error is encountered.

- 

commitFile

public void commitFile​([File](https://developer.android.com/reference/java/io/File.html "class or interface in java.io")file,
                       long length)
                throws[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Description copied from interface: Cache

Commits a file into the cache. Must only be called when holding a corresponding hole CacheSpan obtained from Cache.startReadWrite(String, long, long).

This method may be slow and shouldn't normally be called on the main thread.

Specified by:commitFile in interface CacheParameters:file - A newly written cache file.length - The length of the newly written cache file in bytes.Throws:Cache.CacheException - If an error is encountered.

- 

releaseHoleSpan

public void releaseHoleSpan​([CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")holeSpan)

Deprecated.

Description copied from interface: Cache

Releases a CacheSpan obtained from Cache.startReadWrite(String, long, long) which corresponded to a hole in the cache. Specified by:releaseHoleSpan in interface CacheParameters:holeSpan - The CacheSpan being released.

- 

removeResource

public void removeResource​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key)

Deprecated.

Description copied from interface: Cache

Removes all CacheSpans for a resource, deleting the underlying files. Specified by:removeResource in interface CacheParameters:key - The cache key of the resource being removed.

- 

removeSpan

public void removeSpan​([CacheSpan](CacheSpan.html "class in com.google.android.exoplayer2.upstream.cache")span)

Deprecated.

Description copied from interface: Cache

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.

Specified by:removeSpan in interface CacheParameters:span - The CacheSpan to remove.

- 

isCached

public boolean isCached​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,
                        long position,
                        long length)

Deprecated.

Description copied from interface: Cache

Returns whether the specified range of data in a resource is fully cached. Specified by:isCached in interface CacheParameters:key - The cache key of the resource.position - The starting position of the data in the resource.length - The length of the data.Returns:true if the data is available in the Cache otherwise false;

- 

getCachedLength

public long getCachedLength​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,
                            long position,
                            long length)

Deprecated.

Description copied from interface: Cache

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. Specified by:getCachedLength in interface CacheParameters:key - The cache key of the resource.position - The starting position of the data in the resource.length - The maximum length of the data or hole to be returned. C.LENGTH_UNSET is permitted, and is equivalent to passing Long.MAX_VALUE.Returns:The length of the continuously cached data, or -holeLength if position isn't cached.

- 

getCachedBytes

public long getCachedBytes​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,
                           long position,
                           long length)

Deprecated.

Description copied from interface: Cache

Returns the total number of cached bytes between position (inclusive) and (position + length) (exclusive) of a resource. Specified by:getCachedBytes in interface CacheParameters:key - The cache key of the resource.position - The starting position of the data in the resource.length - The length of the data to check. C.LENGTH_UNSET is permitted, and is equivalent to passing Long.MAX_VALUE.Returns:The total number of cached bytes.

- 

applyContentMetadataMutations

public void applyContentMetadataMutations​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key,[ContentMetadataMutations](ContentMetadataMutations.html "class in com.google.android.exoplayer2.upstream.cache")mutations)
                                   throws[Cache.CacheException](Cache.CacheException.html "class in com.google.android.exoplayer2.upstream.cache")

Deprecated.

Description copied from interface: Cache

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.

Specified by:applyContentMetadataMutations in interface CacheParameters:key - The cache key of the resource.mutations - Contains mutations to be applied to the metadata.Throws:Cache.CacheException - If an error is encountered.

- 

getContentMetadata

public[ContentMetadata](ContentMetadata.html "interface in com.google.android.exoplayer2.upstream.cache")getContentMetadata​([String](https://developer.android.com/reference/java/lang/String.html "class or interface in java.lang")key)

Deprecated.

Description copied from interface: Cache

Returns a ContentMetadata for the given resource. Specified by:getContentMetadata in interface CacheParameters:key - The cache key of the resource.Returns:The ContentMetadata for the resource.