question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Goals

  • Read while writing.
  • Avoid I/O in synchronized blocks.
  • Only one file per entry.

API sketch

class DiskLruCache2 {
  Entry get(String key);
  Entry put(String key, ByteString metadata, Source upstream);
  // ... plus methods for sizing, trimming, iterating, etc.

  static class Entry {
    ByteString metadata();
    Source newSource();
  }
}

Implementation sketch

Each entry that is being written has these sources and sinks:

  • The upstream source passed into put(). This has a logical cursor starting at 0, representing the next byte to read.
  • The file sink. Data read from upstream is written to the file sink. Its cursor starts at 0 and is incremented when a flush completes successfully. Its cursor represents that last byte that is safe to read from the file.
  • The sources created by calls to newSource(). On each attempt to read it will read from the file (if its cursor is lower than the file cursor) or from the network otherwise. If it reads from the network it is responsible for writing to the file.

Readers cooperate with synchronization, with only one reading from upstream at a time. We should also add a buffer to contain up to N bytes preceding the upstream source’s cursor. Readers may prefer to read from this buffer instead of from the file for better performance. Also this makes it possible to reduce flushes to the file system.

Notes

  • What happens when a FileInputStream reads the last byte of a file? Does it eagerly EOF? Or does it continue to read bytes if further bytes are later written to the file?
  • The current DiskLruCache has CLEAN and DIRTY entries in its journal. We should do likewise. An entry can be made clean after the upstream source on a put reaches EOF. This requires a last flush to the file.
  • We want to be able to put() a new entry when an existing entry is already in place. This is necessary when a conditional GET misses.
  • We may also want to be able to put() new metadata without updating the rest of the cache. This might motivate putting the metadata ByteString at the end of the file.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:14
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
ScottPiercecommented, May 13, 2017

@swankjesse Is there any intention to make an OkHttp Cache that is only in-memory or make the persisted data encrypted so as to better handle using the cache with secure data?

0reactions
ScottPiercecommented, Sep 9, 2021

What a fantastic benefit of the FakeFileSystem. Thanks for the suggestion!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JakeWharton/DiskLruCache: Java implementation of a Disk ...
Disk LRU Cache. A cache that uses a bounded amount of space on a filesystem. Each cache entry has a string key and...
Read more >
A deep dive into Jake Wharton's DiskLruCache | by Rahul Raja
This post is about the implementation details of Jake Wharton's famous DiskLruCache. The reason that this cache version is so popular is ...
Read more >
DiskLruCache.java - Google Git
* Used to fetch an instance of DiskLruCache. *. * @param context. * @param cacheDir. * @ ...
Read more >
Disk LRU Cache - The Android Arsenal
Disk LRU (least recently used) cache with persisted journal. This cache has specific capacity and location. Rarely requested files are evicted ...
Read more >
Example of JakeWhartons DiskLruCache - Stack Overflow
I want to use the DiskLruCache from Jake Wharton in my Android app based on API Level 7+. I would use it in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found