-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
38 lines (31 loc) · 895 Bytes
/
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
const Redis = require('ioredis')
export interface StoreOptions {
prefix?: string
[propName: string]: any
}
class RedisStore {
_prefix: string
_redis: any
constructor (opts: StoreOptions) {
this._prefix = 'ilp:' + (opts.prefix || '') + ':'
const redisOptions = Object.assign({ keyPrefix: this._prefix }, opts)
delete(redisOptions.prefix)
this._redis = new Redis(redisOptions)
}
async get (key: string): Promise<string | undefined> {
return this._redis.get(key) || undefined
}
async put (key: string, value: string): Promise<undefined> {
return this._redis.pipeline()
.set(key, String(value))
.publish(this._prefix + key, value)
.exec()
}
async del (key: string): Promise<undefined> {
return this._redis.pipeline()
.del(key)
.publish(this._prefix + ':' + key, '')
.exec()
}
}
export default RedisStore