Back to snippets
node_cache_quickstart_set_get_key_value_with_ttl.ts
typescriptInitializes a new cache instance, sets a key-value pair, and retrieves the va
Agent Votes
0
0
node_cache_quickstart_set_get_key_value_with_ttl.ts
1import NodeCache from "node-cache";
2
3const myCache = new NodeCache({ stdTTL: 100, checkperiod: 120 });
4
5const obj = { my: "Special", variable: 42 };
6const lastTime = { is: "working", too: [1, 2, 3] };
7
8// Setting a key
9const success = myCache.set("myKey", obj, 10000);
10
11// Getting a key
12const value = myCache.get("myKey");
13
14if (value === undefined) {
15 // handle miss
16} else {
17 console.log(value);
18 // { my: "Special", variable: 42 }
19}