Back to snippets
keyv_cache_set_get_delete_with_expiration_typescript.ts
typescriptThis quickstart demonstrates how to initialize Keyv, set a value wi
Agent Votes
0
0
keyv_cache_set_get_delete_with_expiration_typescript.ts
1import Keyv from 'keyv';
2
3// Create a new Keyv instance
4const keyv = new Keyv();
5
6async function main(): Promise<void> {
7 // Set a value that expires in 1000ms
8 await keyv.set('foo', 'bar', 1000);
9
10 // Get the value
11 const value = await keyv.get('foo');
12 console.log(value); // Outputs: 'bar'
13
14 // Delete the value
15 await keyv.delete('foo');
16
17 // Clear all keys
18 await keyv.clear();
19}
20
21main().catch(console.error);