add callbacks in CCCache.with_cache{,_rec} (closes #140)

This commit is contained in:
Simon Cruanes 2017-07-04 13:16:38 +02:00
parent aab19f6a50
commit debf586db5
2 changed files with 24 additions and 7 deletions

View file

@ -26,18 +26,25 @@ type ('a,'b) t = {
clear : unit -> unit; clear : unit -> unit;
} }
type ('a, 'b) callback = in_cache:bool -> 'a -> 'b -> unit
let clear c = c.clear () let clear c = c.clear ()
let with_cache c f x = let default_callback_ ~in_cache:_ _ _ = ()
let with_cache ?(cb=default_callback_) c f x =
try try
c.get x let y = c.get x in
cb ~in_cache:true x y;
y
with Not_found -> with Not_found ->
let y = f x in let y = f x in
c.set x y; c.set x y;
cb ~in_cache:false x y;
y y
let with_cache_rec c f = let with_cache_rec ?(cb=default_callback_) c f =
let rec f' x = with_cache c (f f') x in let rec f' x = with_cache ~cb c (f f') x in
f' f'
(*$R (*$R

View file

@ -31,13 +31,22 @@ type ('a, 'b) t
val clear : (_,_) t -> unit val clear : (_,_) t -> unit
(** Clear the content of the cache *) (** Clear the content of the cache *)
val with_cache : ('a, 'b) t -> ('a -> 'b) -> 'a -> 'b type ('a, 'b) callback = in_cache:bool -> 'a -> 'b -> unit
(** Type of the callback that is called once a cached value is found
or not.
Should never raise.
@param in_cache is [true] if the value was in cache, [false]
if the value was just produced.
@since NEXT_RELEASE *)
val with_cache : ?cb:('a, 'b) callback -> ('a, 'b) t -> ('a -> 'b) -> 'a -> 'b
(** [with_cache c f] behaves like [f], but caches calls to [f] in the (** [with_cache c f] behaves like [f], but caches calls to [f] in the
cache [c]. It always returns the same value as cache [c]. It always returns the same value as
[f x], if [f x] returns, or raise the same exception. [f x], if [f x] returns, or raise the same exception.
However, [f] may not be called if [x] is in the cache. *) However, [f] may not be called if [x] is in the cache.
@param cb called after the value is generated or retrieved *)
val with_cache_rec : ('a,'b) t -> (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b val with_cache_rec : ?cb:('a, 'b) callback -> ('a,'b) t -> (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b
(** [with_cache_rec c f] is a function that first, applies [f] to (** [with_cache_rec c f] is a function that first, applies [f] to
some [f' = fix f], such that recursive calls to [f'] are cached in [c]. some [f' = fix f], such that recursive calls to [f'] are cached in [c].
It is similar to {!with_cache} but with a function that takes as It is similar to {!with_cache} but with a function that takes as
@ -52,6 +61,7 @@ val with_cache_rec : ('a,'b) t -> (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b
fib 70;; fib 70;;
]} ]}
@param cb called after the value is generated or retrieved
*) *)
val size : (_,_) t -> int val size : (_,_) t -> int