java.lang.Object
org.miaixz.bus.http.cache.Cache
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
Caches HTTP and HTTPS responses to the filesystem so they can be reused, saving time and bandwidth.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal InternalCacheThe internal cache implementation for use by the HTTP client. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a cache in the specifieddirectorywith a maximum size ofmaxSizebytes.Cache(File directory, long maxSize, DiskLruCache.DiskFile diskFile) Creates a cache with the specified directory, max size, and disk file system. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the cache.voiddelete()Closes the cache and deletes all of its stored values.Returns the directory where the cache is stored.voidevictAll()Deletes all values stored in the cache.voidflush()Flushes the cache to disk.inthitCount()Returns the number of cache hits.voidInitializes the cache.booleanisClosed()Returns whether the cache is closed.static StringGenerates a unique and safe key for the cache from a URL.longmaxSize()Returns the maximum size of the cache in bytes.intReturns the number of network requests made.intReturns the total number of requests made.longsize()Returns the current size of the cache in bytes.urls()Returns an iterator over the URLs in this cache.intReturns the number of writes to the cache that were aborted.intReturns the number of writes to the cache that were successful.
-
Field Details
-
internalCache
The internal cache implementation for use by the HTTP client.
-
-
Constructor Details
-
Cache
Creates a cache in the specifieddirectorywith a maximum size ofmaxSizebytes.- Parameters:
directory- The directory to store the cache in.maxSize- The maximum size of the cache in bytes.
-
Cache
Creates a cache with the specified directory, max size, and disk file system.- Parameters:
directory- The directory to store the cache in.maxSize- The maximum size of the cache in bytes.diskFile- The disk file system to use.
-
-
Method Details
-
key
Generates a unique and safe key for the cache from a URL.- Parameters:
url- The URL to generate a key for.- Returns:
- The cache key.
-
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.
-
delete
Closes the cache and deletes all of its stored values. This will delete all files in the cache directory, including files that were not created by the cache.- Throws:
IOException- if an I/O error occurs during deletion.
-
evictAll
Deletes all values stored in the cache. Writes to the cache will still complete normally, but the corresponding responses will not be stored.- Throws:
IOException- if an I/O error occurs during eviction.
-
urls
Returns an iterator over the URLs in this cache. This iterator supports Iterator.remove(). Removing a URL from the iterator will remove the corresponding response from the cache. Use this to clear selected responses.- Returns:
- An iterator over the URLs in the cache.
- Throws:
IOException- if an I/O error occurs.
-
writeAbortCount
public int writeAbortCount()Returns the number of writes to the cache that were aborted.- Returns:
- The number of aborted writes.
-
writeSuccessCount
public int writeSuccessCount()Returns the number of writes to the cache that were successful.- Returns:
- The number of successful writes.
-
size
Returns the current size of the cache in bytes.- Returns:
- The current size of the cache.
- Throws:
IOException- if an I/O error occurs.
-
maxSize
public long maxSize()Returns the maximum size of the cache in bytes.- Returns:
- The maximum size of the cache.
-
flush
Flushes the cache to disk.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if an I/O error occurs.
-
close
Closes the cache.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.
-
directory
Returns the directory where the cache is stored.- Returns:
- The cache directory.
-
isClosed
public boolean isClosed()Returns whether the cache is closed.- Returns:
trueif the cache is closed.
-
networkCount
public int networkCount()Returns the number of network requests made.- Returns:
- The network request count.
-
hitCount
public int hitCount()Returns the number of cache hits.- Returns:
- The cache hit count.
-
requestCount
public int requestCount()Returns the total number of requests made.- Returns:
- The total request count.
-