-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-usage-typescript.ts
More file actions
36 lines (29 loc) · 930 Bytes
/
example-usage-typescript.ts
File metadata and controls
36 lines (29 loc) · 930 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const cache = require('./index')
.ttl(60)
.storeUndefinedObjects(false)
.cleanup(60)
.create({ id: 'snacks' })
function standardGetAndSetExample() {
const key = 'the_key1';
let result = cache.get(key)
if (!result) {
cache.set(key, { snack: 'crisps'});
result = cache.get(key)
}
return { value: result, expiry: cache.getExpiry(key) };
}
async function getData() {
return { snack: 'chocolate' };
}
async function getAndSetWithDataRetrievalExample() {
const key = 'the_key2';
let result = await cache.getAndSet(key, getData);
return { value: result, expiry: cache.getExpiry(key) };
}
async function testImplementation() {
console.log(`Result 1: ${JSON.stringify(standardGetAndSetExample())}`);
console.log(`Result 2: ${JSON.stringify(await getAndSetWithDataRetrievalExample())}`);
}
testImplementation()
.then(() => process.exit(0))