类 DiskLruCache
- 所有已实现的接口:
Closeable,Flushable,AutoCloseable
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
one editor at one time; if a value is not available to be edited then edit(java.lang.String) 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.
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.
-
嵌套类概要
嵌套类修饰符和类型类说明final classEdits the values for an entry.final classA snapshot of the values for an entry. -
方法概要
修饰符和类型方法说明voidclose()Closes this cache.static DiskLruCachecreate(FileSystem fileSystem, File directory, int appVersion, int valueCount, long maxSize) Create a cache which will reside indirectory.voiddelete()Closes the cache and deletes all of its stored values.Returns an editor for the entry namedkey, or null if another edit is in progress.voidevictAll()Deletes all stored values from the cache.voidflush()Force buffered operations to the filesystem.Returns a snapshot of the entry namedkey, or null if it doesn't exist is not currently readable.Returns the directory where this cache stores its data.longReturns the maximum number of bytes that this cache should use to store its data.voidbooleanisClosed()Returns true if this cache has been closed.booleanDrops the entry forkeyif it exists and can be removed.voidsetMaxSize(long maxSize) Changes the maximum number of bytes the cache can store and queues a job to trim the existing store, if necessary.longsize()Returns the number of bytes currently being used to store the values in this cache.Returns an iterator over the cache's current entries.
-
方法详细资料
-
create
public static DiskLruCache create(FileSystem fileSystem, File directory, int appVersion, int valueCount, long maxSize) Create a cache which will reside indirectory. This cache is lazily initialized on first access and will be created if it does not exist.- 参数:
directory- a writable directoryvalueCount- the number of values per cache entry. Must be positive.maxSize- the maximum number of bytes this cache should use to store
-
initialize
- 抛出:
IOException
-
get
Returns a snapshot of the entry namedkey, or null if it doesn't exist is not currently readable. If a value is returned, it is moved to the head of the LRU queue.- 抛出:
IOException
-
edit
Returns an editor for the entry namedkey, or null if another edit is in progress.- 抛出:
IOException
-
getDirectory
Returns the directory where this cache stores its data. -
getMaxSize
public long getMaxSize()Returns the maximum number of bytes that this cache should use to store its data. -
setMaxSize
public void setMaxSize(long maxSize) Changes the maximum number of bytes the cache can store and queues a job to trim the existing store, if necessary. -
size
Returns the number of bytes currently being used to store the values in this cache. This may be greater than the max size if a background deletion is pending.- 抛出:
IOException
-
remove
Drops the entry forkeyif it exists and can be removed. If the entry forkeyis currently being edited, that edit will complete normally but its value will not be stored.- 返回:
- true if an entry was removed.
- 抛出:
IOException
-
isClosed
public boolean isClosed()Returns true if this cache has been closed. -
flush
Force buffered operations to the filesystem.- 指定者:
flush在接口中Flushable- 抛出:
IOException
-
close
Closes this cache. Stored values will remain on the filesystem.- 指定者:
close在接口中AutoCloseable- 指定者:
close在接口中Closeable- 抛出:
IOException
-
delete
Closes the cache and deletes all of its stored values. This will delete all files in the cache directory including files that weren't created by the cache.- 抛出:
IOException
-
evictAll
Deletes all stored values from the cache. In-flight edits will complete normally but their values will not be stored.- 抛出:
IOException
-
snapshots
Returns an iterator over the cache's current entries. This iterator doesn't throwConcurrentModificationException, but if new entries are added while iterating, those new entries will not be returned by the iterator. If existing entries are removed during iteration, they will be absent (unless they were already returned).If there are I/O problems during iteration, this iterator fails silently. For example, if the hosting filesystem becomes unreachable, the iterator will omit elements rather than throwing exceptions.
The caller must
closeeach snapshot returned byIterator.next(). Failing to do so leaks open files!The returned iterator supports
Iterator.remove().- 抛出:
IOException
-