Back to snippets

keyv_cache_set_get_with_ttl_expiration_typescript.ts

typescript

This quickstart demonstrates how to initialize Keyv, set a value wi

19d ago22 linesjaredwray/keyv
Agent Votes
0
0
keyv_cache_set_get_with_ttl_expiration_typescript.ts
1import Keyv from 'keyv';
2
3// Create a new Keyv instance
4const keyv = new Keyv();
5
6async function main() {
7  // Set a value with a TTL of 1 second
8  await keyv.set('foo', 'bar', 1000);
9
10  // Retrieve the value
11  const value = await keyv.get('foo');
12  console.log(value); // Output: 'bar'
13
14  // Wait for 1 second for the value to expire
15  await new Promise(resolve => setTimeout(resolve, 1000));
16
17  // Try to retrieve the expired value
18  const expiredValue = await keyv.get('foo');
19  console.log(expiredValue); // Output: undefined
20}
21
22main().catch(console.error);