java.lang.Object
org.miaixz.bus.http.cache.DiskLruCache
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
A cache that uses a limited 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,64}. Values are byte sequences, accessible as streams or
files.
Each value must be between 0 and Integer.MAX_VALUE bytes in length.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAccess to read and write files on a hierarchical data store.final classEdits the values for an entry.final classA snapshot of the values for an entry. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this cache.static DiskLruCachecreate(DiskLruCache.DiskFile diskFile, File directory, int appVersion, int valueCount, long maxSize) Creates a cache that resides 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 or 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.voidInitializes the cache.booleanisClosed()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 items.
-
Method Details
-
create
public static DiskLruCache create(DiskLruCache.DiskFile diskFile, File directory, int appVersion, int valueCount, long maxSize) Creates a cache that resides indirectory. This cache is lazily initialized on first access and will be created if it does not exist.- Parameters:
diskFile- the file system to use.directory- a writable directory.appVersion- the version of the application using the cache.valueCount- the number of values per cache entry.maxSize- the maximum number of bytes this cache should use to store its data.- Returns:
- the disk cache.
-
initialize
Initializes the cache. This will include reading the journal file from storage and building up the necessary in-memory cache information. Note that if the application chooses not to call this method to initialize the cache, it will be initialized lazily on the first use of the cache.- Throws:
IOException- if an I/O error occurs during initialization.
-
get
Returns a snapshot of the entry namedkey, or null if it doesn't exist or is not currently readable. If a value is returned, it will be moved to the head of the LRU queue.- Parameters:
key- the cache key.- Returns:
- the snapshot.
- Throws:
IOException- if an I/O error occurs.
-
edit
Returns an editor for the entry namedkey, or null if another edit is in progress.- Parameters:
key- the file key.- Returns:
- the editor.
- Throws:
IOException- if an I/O error occurs.
-
getDirectory
Returns the directory where this cache stores its data.- Returns:
- the cache directory.
-
getMaxSize
public long getMaxSize()Returns the maximum number of bytes that this cache should use to store its data.- Returns:
- the maximum size in bytes.
-
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.- Parameters:
maxSize- the new maximum size in bytes.
-
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.- Returns:
- the current size in bytes.
- Throws:
IOException- if an I/O error occurs.
-
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.- Parameters:
key- the key of the entry to remove.- Returns:
- true if an entry was removed.
- Throws:
IOException- if an I/O error occurs.
-
isClosed
public boolean isClosed()Returns true if this cache has been closed.- Returns:
trueif the cache is closed.
-
flush
Force buffered operations to the filesystem.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if an I/O error occurs.
-
close
Closes this cache. Stored values will remain on the filesystem.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.
-
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.- Throws:
IOException- if an I/O error occurs.
-
evictAll
Deletes all stored values from the cache. In-flight edits will complete normally but their values will not be stored.- Throws:
IOException- if an I/O error occurs.
-
snapshots
Returns an iterator over the cache's current items. This iterator doesn't throwConcurrentModificationException, but callers mustDiskLruCache.Snapshot.close()each snapshot returned byIterator.next(). Failing to do so will leak open files. The returned iterator supportsIterator.remove().- Returns:
- an iterator over the cache's current items.
- Throws:
IOException- if an I/O error occurs.
-