Module Helpers.Diskcache
Persistent key-value tables.
A table maps strings to strings across runs and processes. Entries are written atomically, so a reader sees a complete entry or none, and the last writer of a key wins. An entry holds the digest of its key and value, so one damaged on disk, cut short or with a byte changed, does not read as another value. Tables are versioned with the library: entries written by a library whose cached data meant something else are not seen. Tables and keys are any strings; entries stay within cachedb whatever they hold. The cache is disabled while Setting.cachelevel is not positive.
The files of the cache hold at most Setting.cachesize bytes, or any number while it is 0. A put that would take them past it first removes entries of every table and version, the least recently used first, until they would hold four fifths of it with the new entry. It removes them before it writes, so that its write finds their space even on a full disk. An entry is used when it is put or read, at the time its file takes then, so two uses within one tick of the file system's clock are unordered. Removing walks every entry, so it leaves a fifth of the bound for the puts that follow. It also removes the writes in progress last written an hour or more before: a process killed while writing leaves one, which goes uncounted until then. A put of an entry that alone would take the files past the bound writes nothing. An entry that cannot be removed, as on Windows while another process has it open, stays for a later removal.
The puts of all processes count together, in the file f7bd60b75b29d79b660a2859395c1a24_size of cachedb, named like a table by the digest of size. A put that lands while another process removes entries goes uncounted until the next removal, and the files may hold more than the bound until then.
val get : table:string -> string -> string optionget ~table key is the value of key in table, if any. It is None while the cache is disabled.
Raises Failure if the entry exists but is malformed or damaged.
val put : table:string -> string -> string -> unitput ~table key value binds key to value in table. It does nothing while the cache is disabled.
Raises Sys_error if the entry cannot be written, and Invalid_argument if Setting.cachesize is negative.
val clear : unit -> unitclear () removes every entry of every table, of every version, and the writes in progress last written an hour or more before. An entry that cannot be removed, as on Windows while another process has it open, stays. The directories of the tables stay, so that a put meanwhile finds its table's, and so do the files under cachedb that the cache did not write.
A put or a get that runs during a clear, on any domain or in any process, comes wholly before or after it: calls behave as if made one at a time in some order. It holds unless the clear cannot write in cachedb, and on a file system that shows every process the others' changes as they make them, as a local one does.