api/store: use basic strings instead of hashes for keys
This commit is contained in:
parent
66cb8d360d
commit
693204b799
1 changed files with 18 additions and 22 deletions
|
@ -26,43 +26,39 @@ export default class RedisStore extends Store {
|
||||||
async _get(key) {
|
async _get(key) {
|
||||||
await this.#connected;
|
await this.#connected;
|
||||||
|
|
||||||
const data = await this.#client.hGetAll(
|
const valueType = await this.#client.get(this.#keyOf(key) + '_t');
|
||||||
|
const value = await this.#client.get(
|
||||||
commandOptions({ returnBuffers: true }),
|
commandOptions({ returnBuffers: true }),
|
||||||
this.#keyOf(key)
|
this.#keyOf(key)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!data.d) {
|
if (!value) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = data.t;
|
if (valueType === 'b')
|
||||||
if (type && type[0] === 'b'.charCodeAt())
|
return value;
|
||||||
return data.d;
|
|
||||||
else
|
else
|
||||||
return JSON.parse(data.d);
|
return JSON.parse(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _set(key, val, exp_sec = -1) {
|
async _set(key, val, exp_sec = -1) {
|
||||||
await this.#connected;
|
await this.#connected;
|
||||||
|
|
||||||
const out = { d: val };
|
const options = exp_sec > 0 ? { EX: exp_sec } : undefined;
|
||||||
|
|
||||||
if (val instanceof Buffer) {
|
if (val instanceof Buffer) {
|
||||||
out.t = 'b';
|
await this.#client.set(
|
||||||
} else {
|
this.#keyOf(key) + '_t',
|
||||||
out.d = JSON.stringify(val);
|
'b',
|
||||||
|
options
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.#client.hSet(
|
await this.#client.set(
|
||||||
this.#keyOf(key),
|
this.#keyOf(key),
|
||||||
out
|
val,
|
||||||
);
|
options
|
||||||
|
|
||||||
if (exp_sec > 0) {
|
|
||||||
await this.#client.hExpire(
|
|
||||||
this.#keyOf(key),
|
|
||||||
Object.keys(out),
|
|
||||||
exp_sec
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue