public final class DiskLruCache extends Object implements Closeable, Flushable
0
and 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(java.lang.String) to create or update the values of an entry. An entry may have only
first editor at first time; if a value is not available to be edited then edit(java.lang.String) will return
null.
Every edit(java.lang.String) call must be matched by a call to DiskLruCache.Editor.commit() or DiskLruCache.Editor.abort(). Committing is atomic: a read observes the full set of values as they were before
or after the commit, but never a mix of values.
Clients call get(java.lang.String) to read a snapshot of an entry. The read will observe the value at
the time that get(java.lang.String) 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 IOException and responding appropriately.
| Modifier and Type | Class and Description |
|---|---|
class |
DiskLruCache.Editor
Edits the values for an entry.
|
class |
DiskLruCache.Snapshot |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
static DiskLruCache |
create(FileSystem fileSystem,
File directory,
int appVersion,
int valueCount,
long maxSize) |
void |
delete() |
DiskLruCache.Editor |
edit(String key) |
void |
evictAll() |
void |
flush() |
DiskLruCache.Snapshot |
get(String key) |
File |
getDirectory() |
long |
getMaxSize() |
void |
initialize() |
boolean |
isClosed() |
boolean |
remove(String key) |
void |
setMaxSize(long maxSize) |
long |
size() |
Iterator<DiskLruCache.Snapshot> |
snapshots() |
public static DiskLruCache create(FileSystem fileSystem, File directory, int appVersion, int valueCount, long maxSize)
public void initialize()
throws IOException
IOExceptionpublic DiskLruCache.Snapshot get(String key) throws IOException
IOExceptionpublic DiskLruCache.Editor edit(String key) throws IOException
IOExceptionpublic File getDirectory()
public long getMaxSize()
public void setMaxSize(long maxSize)
public long size()
throws IOException
IOExceptionpublic boolean remove(String key) throws IOException
IOExceptionpublic boolean isClosed()
public void flush()
throws IOException
flush in interface FlushableIOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionpublic void delete()
throws IOException
IOExceptionpublic void evictAll()
throws IOException
IOExceptionpublic Iterator<DiskLruCache.Snapshot> snapshots() throws IOException
IOExceptionCopyright © 2019. All rights reserved.