refactoring redis for multiple value classes
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
43cf782511
commit
d5c136790e
34
lib/Redis.ts
34
lib/Redis.ts
@ -11,6 +11,9 @@ import { LabelledKey } from './Key';
|
|||||||
|
|
||||||
const logger = debug ('redis');
|
const logger = debug ('redis');
|
||||||
|
|
||||||
|
export type SyncClass = 'blacklist' | 'keystore'
|
||||||
|
export type SyncValue = LabelledKey | string;
|
||||||
|
|
||||||
export class Redis {
|
export class Redis {
|
||||||
private _redis: IORedis | null = null;
|
private _redis: IORedis | null = null;
|
||||||
|
|
||||||
@ -52,20 +55,31 @@ export class Redis {
|
|||||||
log ('done');
|
log ('done');
|
||||||
}
|
}
|
||||||
|
|
||||||
public async set_key (key: LabelledKey): Promise<void> {
|
public async set (
|
||||||
const log = logger.extend ('set_key');
|
sync_class: SyncClass,
|
||||||
log ('trying to set key %s to redis', key.index);
|
key: string,
|
||||||
|
value: SyncValue
|
||||||
|
): Promise<void> {
|
||||||
|
const log = logger.extend ('set');
|
||||||
|
log ('trying to set %s value %s to redis', sync_class, key);
|
||||||
if (this._redis === null) {
|
if (this._redis === null) {
|
||||||
log ('redis is inactive, skipping');
|
log ('redis is inactive, skipping');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const valid_for = Math.floor (
|
let valid_for = null;
|
||||||
(key.valid_until - (new Date)
|
if (sync_class === 'keystore') {
|
||||||
.getTime ()) / 1000
|
valid_for = Math.floor (
|
||||||
);
|
(key.valid_until - (new Date)
|
||||||
log ('key is valid for %d seconds', valid_for);
|
.getTime ()) / 1000
|
||||||
await this._redis.setex (key.index, valid_for, JSON.stringify (key));
|
);
|
||||||
log ('saved key');
|
log ('key is valid for %d seconds', valid_for);
|
||||||
|
}
|
||||||
|
if (valid_for === null)
|
||||||
|
await this._redis.set (key, JSON.stringify (value));
|
||||||
|
else
|
||||||
|
await this._redis.setex (key, valid_for, JSON.stringify (value));
|
||||||
|
|
||||||
|
log ('saved value');
|
||||||
}
|
}
|
||||||
|
|
||||||
public async get_key (index: string): Promise<LabelledKey | null> {
|
public async get_key (index: string): Promise<LabelledKey | null> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user