-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
43 lines (36 loc) · 1.22 KB
/
index.ts
File metadata and controls
43 lines (36 loc) · 1.22 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
import type { KeyValueAdapter } from "adminforth";
import { AdapterOptions } from "./types.js";
import { Level } from 'level';
export default class LevelDBKeyValueAdapter implements KeyValueAdapter {
options: AdapterOptions;
private db: Level;
constructor(options: AdapterOptions) {
this.options = options;
this.db = new Level(this.options.dbPath, this.options.dbOptions || {});
}
async set(key, value, expiresInSeconds?) {
let dataToSave: { value: any; expireAt?: number } = { value: value };
if (expiresInSeconds) {
const expireAt = Date.now() + (expiresInSeconds * 1000);
dataToSave.expireAt = expireAt;
}
await this.db.put(key, JSON.stringify(dataToSave));
if (!expiresInSeconds) return;
setTimeout(async () => {
await this.db.del(key).catch(() => null);
}, (expiresInSeconds || 0) * 1000);
}
async get(key) {
const value = await this.db.get(key).catch(() => null);
if (value === undefined) return null;
const parsed = JSON.parse(value);
if (parsed.expireAt && Date.now() > parsed.expireAt) {
await this.db.del(key).catch(() => null);
return null;
}
return parsed.value;
}
async delete(key) {
await this.db.del(key);
}
}