Module bus.http

Class DiskLruCache

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

public class DiskLruCache extends Object implements Closeable, Flushable
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    Access to read and write files on a hierarchical data store.
    final class 
    Edits the values for an entry.
    final class 
    A snapshot of the values for an entry.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this cache.
    create(DiskLruCache.DiskFile diskFile, File directory, int appVersion, int valueCount, long maxSize)
    Creates a cache that resides in directory.
    void
    Closes the cache and deletes all of its stored values.
    edit(String key)
    Returns an editor for the entry named key, or null if another edit is in progress.
    void
    Deletes all stored values from the cache.
    void
    Force buffered operations to the filesystem.
    get(String key)
    Returns a snapshot of the entry named key, or null if it doesn't exist or is not currently readable.
    Returns the directory where this cache stores its data.
    long
    Returns the maximum number of bytes that this cache should use to store its data.
    void
    Initializes the cache.
    boolean
    Returns true if this cache has been closed.
    boolean
    Drops the entry for key if it exists and can be removed.
    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.
    long
    Returns the number of bytes currently being used to store the values in this cache.
    Returns an iterator over the cache's current items.

    Methods inherited from class java.lang.Object

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

    • create

      public static DiskLruCache create(DiskLruCache.DiskFile diskFile, File directory, int appVersion, int valueCount, long maxSize)
      Creates a cache that resides in directory. 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

      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.
    • get

      public DiskLruCache.Snapshot get(String key) throws IOException
      Returns a snapshot of the entry named key, 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

      public DiskLruCache.Editor edit(String key) throws IOException
      Returns an editor for the entry named key, 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

      public File 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

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

      public boolean remove(String key) throws IOException
      Drops the entry for key if it exists and can be removed. If the entry for key is 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:
      true if the cache is closed.
    • flush

      public void flush() throws IOException
      Force buffered operations to the filesystem.
      Specified by:
      flush in interface Flushable
      Throws:
      IOException - if an I/O error occurs.
    • close

      public void close() throws IOException
      Closes this cache. Stored values will remain on the filesystem.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs.
    • 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 weren't created by the cache.
      Throws:
      IOException - if an I/O error occurs.
    • evictAll

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

      public Iterator<DiskLruCache.Snapshot> snapshots() throws IOException
      Returns an iterator over the cache's current items. This iterator doesn't throw ConcurrentModificationException, but callers must DiskLruCache.Snapshot.close() each snapshot returned by Iterator.next(). Failing to do so will leak open files. The returned iterator supports Iterator.remove().
      Returns:
      an iterator over the cache's current items.
      Throws:
      IOException - if an I/O error occurs.