forked from velo-org/velo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrr.example.ts
More file actions
40 lines (33 loc) · 1.12 KB
/
rr.example.ts
File metadata and controls
40 lines (33 loc) · 1.12 KB
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
import { RR } from "../mod.ts";
/**
* https://en.wikipedia.org/wiki/Cache_replacement_policies#Random_replacement_(RR)
*
* Randomly selects a candidate item and discards it to make space when necessary.
*/
interface Hello {
hello: string;
}
const rrc = new RR<Hello>({ capacity: 5 }); // init Random Replacement Cache with max 5 key-value pairs
rrc.on("remove", (key, value) => {
console.log(key, value);
});
rrc.on("clear", () => {
console.log("cache cleared");
});
rrc.on("set", (key, value) => {
console.log(key, value);
});
rrc.on("expired", (key, value) => {
console.log(key, value);
});
rrc.set("1", { hello: "asdf" }); // sets 1
rrc.set("2", { hello: "asdf" }); // sets 2
rrc.set("3", { hello: "asdf" }); // sets 3
rrc.set("4", { hello: "asdf" }); // sets 4
rrc.set("5", { hello: "asdf" }); // sets 5
rrc.set("6", { hello: "asdfdd" }); // sets 6 removes random entry
rrc.get("6"); // returns value for key 6
rrc.peek("4"); // returns value for key 4 without changing the queue
rrc.forEach((item, index) => console.log(item, index)); // Array like forEach
rrc.remove("4"); // remove key 4
rrc.clear(); // clear cache