package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=1fd7bddce07cf5d244fc9427f7b5e4d4
sha512=c0f2a0fdc8253e0ea999d8d4c58bfbf32b18d251a2e1d9656bf279de5f01a33e9aabac3af4d95f465f8b671e7711ebd37218043face233340a0c11b08fa62f78
doc/batteries.unthreaded/BatCache/index.html
Module BatCache
Source
type ('a, 'b) manual_cache = {
get : 'a -> 'b;
del : 'a -> unit;
enum : unit -> ('a * 'b) BatEnum.t;
}
The data structure for a manual cache with keys 'a
and values 'b
.
This cache gives access to some internals of a memoized function f
, called the generating function. This function is usually pure; always returning the same output for a given input. This module allows the results of complex functions to be cached so that the function does not need to be called again to get that result.
When c.get k
is called, an internal structure is first consulted to see if f k
has been stored in that structure. If it has, then that previous result is returned, otherwise, f k
is evaluated, the result stored in the caching structure and also returned to the user.
The user is allowed to remove a value from the caching structure with c.del k
. This allows the user to prevent unbounded growth of the cache by removing unneeded entries. If the user prefers an automatically managed cache, this module provides !auto_cache
.
Last, c.enum ()
will enumerate all the currently memorized bindings as pairs. This allows inspection of what is currently cached.
Make a manual cache backed by a hashtable. The generating function is passed with ~gen
and the initial size of the hashtable is ~init_size
. The hashtable uses the polymorphic hash
and (=)
.
Make a manual cache for function ~gen
backed by a Set.t. This set uses the polymorphic (<)
for comparison, so 'a
should be properly comparable by it.
Automatically managed caches
This type of cache is more transparent than the !manual_cache
above. It does not provide inspection of the caching structure, but guarantees bounded memory usage through some policy of discarding some entries.
Each auto-cache can have a different policy to decide which entry to discard.