Module Value.Cache

Data cache.

Store and retrieve data from cache. The cache can seen as a persistent weak key-value storage. Data stored here can disappear at any time, but can survive for a long time (outliving the program). The library by itself doesn't provide a caching service for any type, only the interface. The caching service can be added externally (e.g., via a plugin).

The caching infrastructure provides only facilities for storing and loading data. In fact this is just a weak key-value storage. A weak, because storage is allowed to to loose data.

As a key, we use digest that is underneath the hood is and md5 sum of arguments used to build the cached data.

Let's take for example a function that builds some control flow graphs. It has three parameters, one is an optional debug, that doesn't affect the algorithm, and the rest two has type string and int correspondingly. The following code will try to load result from a cache, using a digest of the arguments, and if is not available, the the graph will be computed and stored in the cache. This function will work even if there is no caching service. Of course, there will be no benefits, since the save function will just immediately forget its argument.

let compute_graph ?(debug=false) x y : Graphs.Cfg.t =
  let digest =
    Data.Cache.digest ~namespace:"example" "%s%d" x y in
  match Graphs.Cfg.Cache.load digest with
  | Some g -> g
  | None ->
    let g = build_graph ?debug x y in
    Graphs.Cfg.Cache.save digest g;
    g

Note: it is only reasonable to use caching for data types, that take significant amount of time to create.

val load : Regular.Std.digest -> t option

load id load data previously stored under give id

val save : Regular.Std.digest -> t -> unit

save id data store data under given id. If something is already stored, then it will be overwritten.