-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
85 lines (73 loc) · 2.43 KB
/
index.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
import readline from 'readline';
import Redis from 'ioredis';
import winston from 'winston';
import {
StorageHierarchy,
MemoryStorageProvider,
Logger,
createAgingCache,
} from '@linkedmink/multilevel-aging-cache';
import {
getStringKeyJsonValueOptions,
RedisPubSubStorageProvider,
} from '@linkedmink/multilevel-aging-cache-ioredis';
Logger.options = {
level: 'debug',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console({ format: winston.format.simple() }),
],
};
const redisClient = new Redis(6379, 'localhost');
const redisChannel = new Redis(6379, 'localhost');
const storageHierarchy = new StorageHierarchy<string, Record<string, unknown>>([
new MemoryStorageProvider(),
new RedisPubSubStorageProvider(redisClient, getStringKeyJsonValueOptions(), redisChannel),
]);
const cache = createAgingCache<string, Record<string, unknown>>(storageHierarchy);
const cliReadline = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const PROMPT_COMMAND = 'Enter a command to read/write cache: ';
const PROMPT_KEY = 'Enter the key: ';
const PROMPT_VALUE = 'Enter the value: ';
const promptInput = (prompt: string): Promise<string> => {
return new Promise<string>(resolve =>
cliReadline.question(prompt, ans => {
resolve(ans);
})
);
};
const main = async (): Promise<number> => {
// eslint-disable-next-line no-constant-condition
while (true) {
const command = (await promptInput(PROMPT_COMMAND)).toLowerCase();
if (command === 'get') {
const key = await promptInput(PROMPT_KEY);
const data = await cache.get(key, false);
console.log(data);
} else if (command === 'set') {
const key = await promptInput(PROMPT_KEY);
const value = JSON.parse(await promptInput(PROMPT_VALUE)) as Record<string, unknown>;
const status = await cache.set(key, value, false);
console.log(status);
} else if (command === 'delete') {
const key = await promptInput(PROMPT_KEY);
const status = await cache.delete(key, false);
console.log(status);
} else if (command === 'keys') {
const keys = await cache.keys();
console.log(keys);
} else if (command === 'exit') {
return 0;
} else {
console.error('Invalid Command');
}
}
};
main()
.then(code => process.exit(code))
.catch(error => process.exit(1));