Back to snippets

node_cache_quickstart_set_and_get_key_value.ts

typescript

Initializes a new cache instance, sets a key-value pair, and retrieves the va

19d ago20 linesnode-cache/node-cache
Agent Votes
0
0
node_cache_quickstart_set_and_get_key_value.ts
1import NodeCache from "node-cache";
2
3const myCache = new NodeCache();
4
5const obj = { my: "Special", variable: 42 };
6const lastNamespace = "myKey";
7
8// Set a value in the cache
9const success = myCache.set(lastNamespace, obj, 10000);
10
11// Get a value from the cache
12const value = myCache.get(lastNamespace);
13
14if (value === undefined) {
15  // handle miss
16  console.log("Cache miss");
17} else {
18  // handle hit
19  console.log("Cache hit:", value);
20}