Module bus.http

Class Cache

java.lang.Object
org.miaixz.bus.http.cache.Cache
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class Cache extends Object implements Closeable, Flushable
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

    Fields
    Modifier and Type
    Field
    Description
    The internal cache implementation for use by the HTTP client.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cache(File directory, long maxSize)
    Creates a cache in the specified directory with a maximum size of maxSize bytes.
    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 Type
    Method
    Description
    void
    Closes the cache.
    void
    Closes the cache and deletes all of its stored values.
    Returns the directory where the cache is stored.
    void
    Deletes all values stored in the cache.
    void
    Flushes the cache to disk.
    int
    Returns the number of cache hits.
    void
    Initializes the cache.
    boolean
    Returns whether the cache is closed.
    static String
    key(UnoUrl url)
    Generates a unique and safe key for the cache from a URL.
    long
    Returns the maximum size of the cache in bytes.
    int
    Returns the number of network requests made.
    int
    Returns the total number of requests made.
    long
    Returns the current size of the cache in bytes.
    Returns an iterator over the URLs in this cache.
    int
    Returns the number of writes to the cache that were aborted.
    int
    Returns the number of writes to the cache that were successful.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • internalCache

      public final InternalCache internalCache
      The internal cache implementation for use by the HTTP client.
  • Constructor Details

    • Cache

      public Cache(File directory, long maxSize)
      Creates a cache in the specified directory with a maximum size of maxSize bytes.
      Parameters:
      directory - The directory to store the cache in.
      maxSize - The maximum size of the cache in bytes.
    • Cache

      public Cache(File directory, long maxSize, DiskLruCache.DiskFile diskFile)
      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

      public static String key(UnoUrl url)
      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

      public void initialize() throws IOException
      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

      public void delete() throws IOException
      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

      public void evictAll() throws IOException
      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

      public Iterator<String> urls() throws IOException
      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

      public long size() throws IOException
      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

      public void flush() throws IOException
      Flushes the cache to disk.
      Specified by:
      flush in interface Flushable
      Throws:
      IOException - if an I/O error occurs.
    • close

      public void close() throws IOException
      Closes the cache.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs.
    • directory

      public File directory()
      Returns the directory where the cache is stored.
      Returns:
      The cache directory.
    • isClosed

      public boolean isClosed()
      Returns whether the cache is closed.
      Returns:
      true if 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.