comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 :- module(code_cache,
2 [ clean_code_cache/1
3 , cache_code/3
4 , cache_code/2
5 , call_cached/2
6 ]).
7
8 :- use_module(library(insist)).
9
10 :- meta_predicate cache_code(0,-).
11 :- meta_predicate cache_code(+,0,-).
12
13
14 cache_code(Code,Code1) :- cache_code(false,Code,Code1).
15 cache_code(MultiUse,Code,code_cache:call_cached(ID,MultiUse)) :-
16 uuid(ID),
17 get_time(T),
18 recorda(ID,code_cache(T,Code)).
19
20 call_cached(ID,Keep) :-
21 insist(recorded(ID,code_cache(_,Code),Ref),code_not_in_cache(ID)),
22 (Keep=false -> erase(Ref); true),
23 call(Code).
24
25 clean_code_cache(MaxAge) :-
26 get_time(Now),
27 Oldest is Now-MaxAge,
28 forall( ( recorded(_,code_cache(T,Code),Ref), T<Oldest),
29 ( debug(code_cache,'Removing old cached code: ~q',[Code]),
30 erase(Ref))).
31
32 :- multifile sandbox:safe_meta/2.
33 sandbox:safe_meta(code_cache:call_cached(ID,_),[Code]) :-
34 insist(recorded(ID,code_cache(_,Code)),code_not_in_cache(ID)).
35