public class

DiskStorageCache

extends Object
implements FileCache DiskTrimmable
java.lang.Object
   ↳ com.facebook.cache.disk.DiskStorageCache

Class Overview

Cache that manages disk storage.

Summary

Nested Classes
class DiskStorageCache.Params  
Constants
int START_OF_VERSIONING
Public Constructors
DiskStorageCache(DiskStorage diskStorage, EntryEvictionComparatorSupplier entryEvictionComparatorSupplier, DiskStorageCache.Params params, CacheEventListener cacheEventListener, CacheErrorLogger cacheErrorLogger, DiskTrimmableRegistry diskTrimmableRegistry, Executor executorForBackgrountInit, boolean indexPopulateAtStartupEnabled)
Public Methods
void clearAll()
long clearOldEntries(long cacheExpirationMs)
Deletes old cache files.
long getCount()
DiskStorage.DiskDumpInfo getDumpInfo()
BinaryResource getResource(CacheKey key)
Retrieves the file corresponding to the mKey, if it is in the cache.
long getSize()
boolean hasKey(CacheKey key)
boolean hasKeySync(CacheKey key)
Returns true if the key is in the in-memory key index.
BinaryResource insert(CacheKey key, WriterCallback callback)
Inserts resource into file with key
boolean isEnabled()
Tells if this cache is enabled.
boolean isIndexReady()
Tells if memory index is completed in initialization.
boolean probe(CacheKey key)
Probes whether the object corresponding to the mKey is in the cache.
void remove(CacheKey key)
Removes a resource by key from cache.
void trimToMinimum()
Called when there is very little disk space left.
void trimToNothing()
Called when there is almost no disk space left and the app is likely to crash soon
Protected Methods
void awaitIndex()
Blocks current thread until having finished initialization in Memory Index.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.facebook.cache.disk.FileCache
From interface com.facebook.common.disk.DiskTrimmable

Constants

public static final int START_OF_VERSIONING

Constant Value: 1 (0x00000001)

Public Constructors

public DiskStorageCache (DiskStorage diskStorage, EntryEvictionComparatorSupplier entryEvictionComparatorSupplier, DiskStorageCache.Params params, CacheEventListener cacheEventListener, CacheErrorLogger cacheErrorLogger, DiskTrimmableRegistry diskTrimmableRegistry, Executor executorForBackgrountInit, boolean indexPopulateAtStartupEnabled)

Public Methods

public void clearAll ()

public long clearOldEntries (long cacheExpirationMs)

Deletes old cache files.

Parameters
cacheExpirationMs files older than this will be deleted.
Returns
  • the age in ms of the oldest file remaining in the cache.

public long getCount ()

public DiskStorage.DiskDumpInfo getDumpInfo ()

Throws
IOException

public BinaryResource getResource (CacheKey key)

Retrieves the file corresponding to the mKey, if it is in the cache. Also touches the item, thus changing its LRU timestamp. If the file is not present in the file cache, returns null.

This should NOT be called on the UI thread.

Parameters
key the mKey to check
Returns
  • The resource if present in cache, otherwise null

public long getSize ()

public boolean hasKey (CacheKey key)

public boolean hasKeySync (CacheKey key)

Returns true if the key is in the in-memory key index.

Not guaranteed to be correct. The cache may yet have this key even if this returns false. But if it returns true, it definitely has it.

Avoids a disk read.

public BinaryResource insert (CacheKey key, WriterCallback callback)

Inserts resource into file with key

Parameters
key cache key
callback Callback that writes to an output stream
Returns
  • a sequence of bytes
Throws
IOException

public boolean isEnabled ()

Tells if this cache is enabled. It's important for some caches that can be disabled without further notice (like in removable/unmountable storage). Anyway a disabled cache should just ignore calls, not fail.

Returns
  • true if this cache is usable, false otherwise.

public boolean isIndexReady ()

Tells if memory index is completed in initialization. Only call it when you need to know if memory index is completed in cold start.

public boolean probe (CacheKey key)

Probes whether the object corresponding to the mKey is in the cache. Note that the act of probing touches the item (if present in cache), thus changing its LRU timestamp.

This will be faster than retrieving the object, but it still has file system accesses and should NOT be called on the UI thread.

Parameters
key the mKey to check
Returns
  • whether the keyed mValue is in the cache

public void remove (CacheKey key)

Removes a resource by key from cache.

Parameters
key cache key

public void trimToMinimum ()

Called when there is very little disk space left.

public void trimToNothing ()

Called when there is almost no disk space left and the app is likely to crash soon

Protected Methods

protected void awaitIndex ()

Blocks current thread until having finished initialization in Memory Index. Call only when you need memory index in cold start.