Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/api/src/store/redis-store.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (51 sloc)
1.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { commandOptions, createClient } from "redis"; | |
import { env } from "../config.js"; | |
import { Store } from "./base-store.js"; | |
export default class RedisStore extends Store { | |
#client = createClient({ | |
url: env.redisURL, | |
}); | |
#connected; | |
constructor(name) { | |
super(name); | |
this.#connected = this.#client.connect(); | |
} | |
#keyOf(key) { | |
return this.id + '_' + key; | |
} | |
async _has(key) { | |
await this.#connected; | |
return this.#client.hExists(key); | |
} | |
async _get(key) { | |
await this.#connected; | |
const valueType = await this.#client.get(this.#keyOf(key) + '_t'); | |
const value = await this.#client.get( | |
commandOptions({ returnBuffers: true }), | |
this.#keyOf(key) | |
); | |
if (!value) { | |
return null; | |
} | |
if (valueType === 'b') | |
return value; | |
else | |
return JSON.parse(value); | |
} | |
async _set(key, val, exp_sec = -1) { | |
await this.#connected; | |
const options = exp_sec > 0 ? { EX: exp_sec } : undefined; | |
if (val instanceof Buffer) { | |
await this.#client.set( | |
this.#keyOf(key) + '_t', | |
'b', | |
options | |
); | |
} | |
await this.#client.set( | |
this.#keyOf(key), | |
val, | |
options | |
); | |
} | |
} |