-
Notifications
You must be signed in to change notification settings - Fork 40
/
sample.html
143 lines (120 loc) · 4.45 KB
/
sample.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<html>
<head>
<title>JavaScript LRU Cache: Sample</title>
<script src="cache.js"></script>
<script src="test.js"></script>
<script>
var cachetest;
function exampleCreateCache() {
var size = document.getElementById("createSize").value;
cachetest = new Cache(size, true);
updateStatus("Created cache with size " + size);
}
function exampleAddItem() {
if (!isCacheInitialized()) {
updateStatus("Cache is not initialized");
return;
}
var k = document.getElementById("addKey").value;
var v = document.getElementById("addValue").value;
var ea = document.getElementById("addEA").value;
var es = document.getElementById("addES").value;
var p = document.getElementById("addPriority").value;
var c = document.getElementById("addCallback").value;
var options = {};
if (ea != '') {
options.expirationAbsolute = new Date(Date.parse(ea));
}
if (es != '') {
options.expirationSliding = parseInt(es);
}
if (p != '') {
options.priority = parseInt(p);
}
if (c != '') {
options.callback = new Function("key", "value", c);
}
cachetest.setItem(k, v, options);
updateStatus("Added item '" + k + "' to cache");
}
function exampleGetItem() {
if (!isCacheInitialized()) {
updateStatus("Cache is not initialized");
return;
}
var k = document.getElementById("getKey").value;
var v = cachetest.getItem(k);
updateStatus("Key '" + k + "' has value '" + v + "'");
}
function exampleClearCache() {
if (!isCacheInitialized()) {
updateStatus("Cache is not initialized");
return;
}
cachetest.clear();
updateStatus("Cleared cache");
}
function isCacheInitialized() {
return (cachetest != null);
}
function updateStatus(msg) {
document.getElementById("status").innerHTML = msg;
printCache();
}
function printCache() {
document.getElementById("cachetostring").innerHTML = cachetest.toHtmlString();
}
</script>
</head>
<body>
<form>
<table border="0" cellpadding="8">
<tr><td valign="top">
<table border="1" cellpadding="8">
<tr><td valign="top">
Create a new cache
</td><td valign="top">
<table border="0" cellpadding="2">
<tr><td valign="top" align="right">Size:</td><td valign="top"><input type="text" id="createSize" value="-1" size="3" maxLength="4" /></td></tr>
</table>
</td><td>
<input type="button" id="create" value="GO" onclick="exampleCreateCache(); return false;" />
</td></tr>
<tr><td valign="top">
Add an item to the cache
</td><td valign="top">
<table border="0" cellpadding="2">
<tr><td valign="top" align="right">Key:</td><td valign="top"><input type="text" id="addKey" value="" size="10" maxLength="50" /></td></tr>
<tr><td valign="top" align="right">Value:</td><td valign="top"><input type="text" id="addValue" value="" size="10" maxLength="50" /></td></tr>
<tr><td valign="top" align="right">Absolute Expiration:</td><td valign="top"><input type="text" id="addEA" value="" size="10" maxLength="50" /></td></tr>
<tr><td valign="top" align="right">Sliding Expiration:</td><td valign="top"><input type="text" id="addES" value="" size="10" maxLength="50" /> (s)</td></tr>
<tr><td valign="top" align="right">Priority:</td><td valign="top"><input type="text" id="addPriority" value="" size="10" maxLength="50" /></td></tr>
<tr><td valign="top" align="right">Callback:</td><td valign="top"><input type="text" id="addCallback" value="" size="10" maxLength="50" /></td></tr>
</table>
</td><td>
<input type="button" id="add" value="GO" onclick="exampleAddItem(); return false;" />
</td></tr>
<tr><td valign="top">
Get item from cache
</td><td valign="top">
<table border="0" cellpadding="2">
<tr><td valign="top" align="right">Key:</td><td valign="top"><input type="text" id="getKey" value="" size="10" maxLength="50" /></td></tr>
</table>
</td><td>
<input type="button" id="get" value="GO" onclick="exampleGetItem(); return false;" />
</td></tr>
<tr><td valign="top">
Clear the cache
</td><td valign="top">
</td><td>
<input type="button" id="clear" value="GO" onclick="exampleClearCache(); return false;" />
</td></tr>
</table>
</td><td valign="top">
<div id="status" style="color: #18a760;"></div><br />
<div id="cachetostring"></div>
</td></tr></table>
</form>
</body>
</html>