Disk LRU Cache

repository·master·Indexed 26 days ago

https://github.com/jakewharton/disklrucache

A Java library providing a bounded, filesystem-based Least Recently Used (LRU) cache designed for Android compatibility. It manages byte sequences using an atomic edit/commit pattern and supports version 2.0.2.

Tokens
715
Snippets
2
Records
4
Agent score
41%

What's inside disklrucache

  1. Overview of Disk LRU Cache functionality

    master

    Disk LRU Cache is a filesystem-based cache that uses a bounded amount of space.

    Key Constraints:

    • Keys: Must be strings matching the regex [a-z0-9_-]{1,120}.
    • Values: Byte sequences (accessible as streams or files) with lengths between 0 and Integer.MAX_VALUE bytes.
    • Directory: The cache uses a specific directory on the filesystem. This directory must be exclusive to the cache; multiple processes must not use the same directory simultaneously.
    • Space Management: The cache limits the total bytes stored. When the limit is exceeded, entries are removed in the background. Note that the limit is not strict and does not include filesystem overhead or the cache journal; set a conservative limit for space-sensitive applications.
    • Android Compatibility: This implementation specifically targets Android.
  2. Use edit and get to manage cache entries

    master

    The cache uses an edit and get pattern for managing data.

    Writing Data (edit)

    Clients call edit to create or update values.

    • Concurrency: Only one editor can work on an entry at a time. If an entry is unavailable, edit returns null.
    • Creation: When creating a new entry, you must supply a full set of values (use an empty value as a placeholder if necessary).
    • Updating: When editing an existing entry, you do not need to supply every value; missing values default to their previous state.
    • Atomicity: Every edit call must be followed by either Editor.commit() or Editor.abort(). Committing is atomic: readers will see either the full old set of values or the full new set, never a partial mix.

    Reading Data (get)

    Clients call get to read a snapshot of an entry. The read observes the values as they existed at the moment get was called. Subsequent updates or removals do not affect ongoing reads.

    Error Handling

    • The cache is tolerant of missing files; if a file is missing, the entry is dropped.
    • If a write error occurs during an edit, the edit fails silently.
    • For other issues, callers should catch IOException and handle them appropriately.
  3. Install Disk LRU Cache via Maven or Gradle

    master

    You can add Disk LRU Cache to your project using Maven or Gradle dependency management. The current stable version is 2.0.2.

    <!-- Maven -->
    <dependency>
      <groupId>com.jakewharton</groupId>
      <artifactId>disklrucache</artifactId>
      <version>2.0.2</version>
    </dependency>
    
    <!-- Gradle -->
    compile 'com.jakewharton:disklrucache:2.0.2'