-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.m
81 lines (70 loc) · 2.41 KB
/
cache.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use_cache := true;
prefix := "Dyfj";
//The typical use case of is the functions in this file is the following:
//
//function ComputeX(object,parameters)
// if IsArrayCached(object,"X",parameters) then
// return GetArrayCache(object,"X",parameters);
// else;
// X := <some code that computes X>;
// SetArrayCache(object,"X",parameters,X);
// return X;
// end if;
//end function;
function IsCached(object,attribute0)
if not use_cache then return false; end if;
attribute := prefix cat attribute0;
return attribute in GetAttributes(Type(object)) and
assigned object``attribute;
end function;
function IsArrayCached(object,attribute0,key)
if not IsCached(object,attribute0) then return false; end if;
attribute := prefix cat attribute0;
return Type(object``attribute) eq Assoc and IsDefined(object``attribute,key);
end function;
procedure InitiateCache(object,attribute0)
if use_cache then;
attribute := prefix cat attribute0;
if not attribute in GetAttributes(Type(object)) then
AddAttribute(Type(object), attribute);
end if;
end if;
end procedure;
procedure InitiateArrayCache(object,attribute0)
if use_cache then;
InitiateCache(object,attribute0);
attribute := prefix cat attribute0;
if not assigned object``attribute then
object``attribute := AssociativeArray();
end if;
end if;
end procedure;
procedure SetArrayCache(object, attribute0, key, value : initiate := true)
attribute := prefix cat attribute0;
if use_cache then;
if initiate then InitiateArrayCache(object, attribute0); end if;
object``attribute[key] := value;
end if;
end procedure;
function GetArrayCache(object, attribute0, key);
attribute := prefix cat attribute0;
if use_cache then;
return (object``attribute)[key];
end if;
error "Can only get from cache if use_cache eq true";
end function;
procedure SetCache(object, attribute0, value : initiate := true);
attribute := prefix cat attribute0;
if use_cache then;
if initiate then InitiateCache(object,attribute0); end if;
object``attribute := value;
end if;
return;
end procedure;
function GetCache(object, attribute0);
attribute := prefix cat attribute0;
if use_cache then;
return object``attribute;
end if;
error "Can only get from cache if use_cache eq true";
end function;