-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public final class DiskLruCache implements Closeable
A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and a fixed number of values. Each key must match the regex [a-z0-9_-]{1,120}. Values are byte sequences, accessible as streams or files. Each value must be between
{@code 0}and{@code Integer.MAX_VALUE}bytes in length.The cache stores its data in a directory on the filesystem. This directory must be exclusive to the cache; the cache may delete or overwrite files from its directory. It is an error for multiple processes to use the same cache directory at the same time.
This cache limits the number of bytes that it will store on the filesystem. When the number of stored bytes exceeds the limit, the cache will remove entries in the background until the limit is satisfied. The limit is not strict: the cache may temporarily exceed it while waiting for files to be deleted. The limit does not include filesystem overhead or the cache journal so space-sensitive applications should set a conservative limit.
Clients call edit to create or update the values of an entry. An entry may have only one editor at one time; if a value is not available to be edited then edit will return null.
- When an entry is being created it is necessary to supply a full set of values; the empty value should be used as a placeholder if necessary.
- When an entry is being edited, it is not necessary to supply data for every value; values default to their previous value.
Clients call get to read a snapshot of an entry. The read will observe the value at the time that get was called. Updates and removals after the call do not impact ongoing reads.
This class is tolerant of some I/O errors. If files are missing from the filesystem, the corresponding entries will be dropped from the cache. If an error occurs while writing a cache value, the edit will fail silently. Callers should handle other problems by catching
{@code IOException}and responding appropriately.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classDiskLruCache.SnapshotA snapshot of the values for an entry.
public final classDiskLruCache.EditorEdits the values for an entry.
public final classDiskLruCache.UtilJunk drawer of utility methods.
-
Method Summary
Modifier and Type Method Description FilegetDirectory()Returns the directory where this cache stores its data. synchronized longgetMaxSize()Returns the maximum number of bytes that this cache should use to storeits data. synchronized voidsetMaxSize(long maxSize)Changes the maximum number of bytes the cache can store and queues a jobto trim the existing store, if necessary. static DiskLruCacheopen(File directory, int appVersion, int valueCount, long maxSize, Map<Integer, DiskLruCache> mapKeyToCache, boolean gzip)Opens the cache in {@code directory}, creating a cache if none existsthere.booleanisGzip()synchronized DiskLruCache.Snapshotget(String key)Returns a snapshot of the entry named {@code key}, or null if it doesn'texist is not currently readable.DiskLruCache.Editoredit(String key)Returns an editor for the entry named {@code key}, or null if anotheredit is in progress.synchronized longsize()Returns the number of bytes currently being used to store the values inthis cache. synchronized booleanremove(String key)Drops the entry for {@code key}if it exists and can be removed.synchronized booleanisClosed()Returns true if this cache has been closed. synchronized voidflush()Force buffered operations to the filesystem. synchronized voidclose()Closes this cache. voiddelete()Closes the cache and deletes all of its stored values. voidclear()Deletes all of its stored values.This will deleteall files in the cache directory including files that weren't created bythe cache. -
-
Method Detail
-
getDirectory
File getDirectory()
Returns the directory where this cache stores its data.
-
getMaxSize
synchronized long getMaxSize()
Returns the maximum number of bytes that this cache should use to storeits data.
-
setMaxSize
synchronized void setMaxSize(long maxSize)
Changes the maximum number of bytes the cache can store and queues a jobto trim the existing store, if necessary.
-
open
static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize, Map<Integer, DiskLruCache> mapKeyToCache, boolean gzip)
Opens the cache in
{@code directory}, creating a cache if none existsthere.- Parameters:
directory- a writable directoryvalueCount- the number of values per cache entry.maxSize- the maximum number of bytes this cache should use to store
-
isGzip
boolean isGzip()
-
get
synchronized DiskLruCache.Snapshot get(String key)
Returns a snapshot of the entry named
{@code key}, or null if it doesn'texist is not currently readable. If a value is returned, it is moved tothe head of the LRU queue.
-
edit
DiskLruCache.Editor edit(String key)
Returns an editor for the entry named
{@code key}, or null if anotheredit is in progress.
-
size
synchronized long size()
Returns the number of bytes currently being used to store the values inthis cache. This may be greater than the max size if a backgrounddeletion is pending.
-
remove
synchronized boolean remove(String key)
Drops the entry for
{@code key}if it exists and can be removed. Entriesactively being edited cannot be removed.
-
isClosed
synchronized boolean isClosed()
Returns true if this cache has been closed.
-
flush
synchronized void flush()
Force buffered operations to the filesystem.
-
close
synchronized void close()
Closes this cache. Stored values will remain on the filesystem.
-
delete
void delete()
Closes the cache and deletes all of its stored values. This will deleteall files in the cache directory including files that weren't created bythe cache.
-
clear
void clear()
Deletes all of its stored values.This will deleteall files in the cache directory including files that weren't created bythe cache.Make the cache empty as newly created
-
-
-
-