annotate cpack/dml/lib/code_cache.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 :- module(code_cache,
Daniel@0 2 [ clean_code_cache/1
Daniel@0 3 , cache_code/3
Daniel@0 4 , cache_code/2
Daniel@0 5 , call_cached/2
Daniel@0 6 ]).
Daniel@0 7
Daniel@0 8 :- use_module(library(insist)).
Daniel@0 9
Daniel@0 10 :- meta_predicate cache_code(0,-).
Daniel@0 11 :- meta_predicate cache_code(+,0,-).
Daniel@0 12
Daniel@0 13
Daniel@0 14 cache_code(Code,Code1) :- cache_code(false,Code,Code1).
Daniel@0 15 cache_code(MultiUse,Code,code_cache:call_cached(ID,MultiUse)) :-
Daniel@0 16 uuid(ID),
Daniel@0 17 get_time(T),
Daniel@0 18 recorda(ID,code_cache(T,Code)).
Daniel@0 19
Daniel@0 20 call_cached(ID,Keep) :-
Daniel@0 21 insist(recorded(ID,code_cache(_,Code),Ref),code_not_in_cache(ID)),
Daniel@0 22 (Keep=false -> erase(Ref); true),
Daniel@0 23 call(Code).
Daniel@0 24
Daniel@0 25 clean_code_cache(MaxAge) :-
Daniel@0 26 get_time(Now),
Daniel@0 27 Oldest is Now-MaxAge,
Daniel@0 28 forall( ( recorded(_,code_cache(T,Code),Ref), T<Oldest),
Daniel@0 29 ( debug(code_cache,'Removing old cached code: ~q',[Code]),
Daniel@0 30 erase(Ref))).
Daniel@0 31
Daniel@0 32 :- multifile sandbox:safe_meta/2.
Daniel@0 33 sandbox:safe_meta(code_cache:call_cached(ID,_),[Code]) :-
Daniel@0 34 insist(recorded(ID,code_cache(_,Code)),code_not_in_cache(ID)).
Daniel@0 35