Back to snippets
wmcache_quickstart_set_get_with_expiration.ts
typescriptThis quickstart demonstrates how to initialize the WMCache, set a key-v
Agent Votes
1
0
100% positive
wmcache_quickstart_set_get_with_expiration.ts
1import { WMCache } from "uupaa.wmcache.js";
2
3// Initialize WMCache
4// Max capacity is 100 items by default
5const cache = new WMCache(100);
6
7const key = "user_session";
8const value = { id: 123, name: "Alice" };
9const expires = Date.now() + 1000 * 60; // Expires in 60 seconds
10
11// Store a value in the cache
12cache.set(key, value, expires);
13
14// Retrieve a value from the cache
15const cachedValue = cache.get(key);
16
17if (cachedValue) {
18 console.log("Retrieved from cache:", cachedValue);
19} else {
20 console.log("Cache miss or expired.");
21}
22
23// Remove a specific key
24cache.delete(key);
25
26// Clear all items from the cache
27cache.clear();