-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.js
More file actions
58 lines (48 loc) · 1.75 KB
/
Copy pathredis.js
File metadata and controls
58 lines (48 loc) · 1.75 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const { CacheManager } = require('../lib');
async function example1() {
const client = new CacheManager({
store: 'redis',
host: 'localhost',
port: 6379,
namespace: 'example-1'
});
await client.set('foo', 'bar');
console.log(await client.get('foo')); // bar
console.log(await client.has('foo')); // true
await client.delete('foo');
console.log(await client.get('foo')); // undefined
console.log(await client.has('foo')); // false
await client.set('example-1', 'example');
await client.set('example-2', 'example');
await client.set('lorem', 'ipsum');
await client.set('foo', 'bar');
console.log(await client.getKeys()); // ['foo', 'lorem', 'example-2', 'example-1']
console.log(await client.getKeys('*')); // ['foo', 'lorem', 'example-2', 'example-1']
console.log(await client.getKeys('example-*')); // ['example-2', 'example-1']
}
async function example2() {
const client = new CacheManager({
store: 'redis',
host: 'localhost',
port: 6379,
ttl: 5, // time to live in seconds,
namespace: 'example-2'
});
await client.set('foo', 'bar');
console.log(await client.get('foo')); // bar
await sleep(6000); // sleep for 6000ms = 6s
console.log(await client.get('foo')); // undefined
const NO_EXPIRATION_TTL = 0;
await client.set('foo', 'bar', NO_EXPIRATION_TTL);
console.log(await client.get('foo')); // bar
await sleep(5000); // sleep for 5000ms = 5s
console.log(await client.get('foo')); // bar
await client.set('foo', 'bar', 2);
console.log(await client.get('foo')); // bar
await sleep(2000); // sleep for 2000ms = 2s
console.log(await client.get('foo')); // undefined
}
example1();
example2();
const sleep = time => new Promise(resolve => setTimeout(() => resolve(), time));