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
使用文件系统上有限空间的缓存。每个缓存条目都有一个字符串键和固定数量的值 每个键必须匹配regex [a-z0-9_-]{1,64}。值是字节序列,可以作为流或文件访问 每个值必须在0Integer之间。MAX_VALUE字节的长度
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 
    快照信息
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this cache.
    create(DiskLruCache.DiskFile diskFile, File directory, int appVersion, int valueCount, long maxSize)
    创建一个驻留在directory中的缓存。此缓存在第一次访问时惰性初始化,如果它不存在,将创建它.
    void
    Closes the cache and deletes all of its stored values.
    edit(String key)
    返回名为key的条目的编辑器,如果另一个编辑正在进行,则返回null
    void
    Deletes all stored values from the cache.
    void
    Force buffered operations to the filesystem.
    get(String key)
    返回名为key的条目的快照,如果条目不存在,则返回null, 否则当前无法读取。如果返回一个值,它将被移动到LRU队列的头部
    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
     
    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.
    返回缓存当前项的迭代器。这个迭代器不会抛出ConcurrentModificationException, 调用者必须DiskLruCache.Snapshot.close()每个由Iterator.next()返回的快照 如果做不到这一点,就会泄漏打开的文件,返回的迭代器支持 Iterator.remove().

    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)
      创建一个驻留在directory中的缓存。此缓存在第一次访问时惰性初始化,如果它不存在,将创建它.
      Parameters:
      diskFile - 文件系统
      directory - 一个可写目录
      appVersion - 版本信息
      valueCount - 每个缓存条目的值数目.
      maxSize - 此缓存应用于存储的最大字节数
      Returns:
      the disk cache
    • initialize

      public void initialize() throws IOException
      Throws:
      IOException
    • get

      public DiskLruCache.Snapshot get(String key) throws IOException
      返回名为key的条目的快照,如果条目不存在,则返回null, 否则当前无法读取。如果返回一个值,它将被移动到LRU队列的头部
      Parameters:
      key - 缓存key
      Returns:
      the 快照信息
      Throws:
      IOException - 异常
    • edit

      public DiskLruCache.Editor edit(String key) throws IOException
      返回名为key的条目的编辑器,如果另一个编辑正在进行,则返回null
      Parameters:
      key - 文件key
      Returns:
      编辑器
      Throws:
      IOException - 异常
    • getDirectory

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

      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.
      Throws:
      IOException
    • 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.
      Returns:
      true if an entry was removed.
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
      Returns true if this cache has been closed.
    • flush

      public void flush() throws IOException
      Force buffered operations to the filesystem.
      Specified by:
      flush in interface Flushable
      Throws:
      IOException
    • 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
    • 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
    • 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
    • snapshots

      public Iterator<DiskLruCache.Snapshot> snapshots() throws IOException
      返回缓存当前项的迭代器。这个迭代器不会抛出ConcurrentModificationException, 调用者必须DiskLruCache.Snapshot.close()每个由Iterator.next()返回的快照 如果做不到这一点,就会泄漏打开的文件,返回的迭代器支持 Iterator.remove().
      Returns:
      返回迭代器
      Throws:
      IOException - 异常