This class is a basic redis database adapter.
export type KVType = "raw" | "object";
export type StringOrObject = string | Record<string, any>;
export interface GetConnectionPerfResponse {
isAlive: boolean;
perf: number;
}
export interface ClearExpiredOptions {
banTimeInSecond: number;
}
export type RedisIsKeyExpiredOptions = ClearExpiredOptions & {
key: KeyType;
};
export interface RedisSetValueOptions<T extends StringOrObject = Record<string, any>> {
key: KeyType;
value: Partial<T>;
type: KVType;
expiresIn?: number;
}
export type RedisAdapterOptions = Partial<RedisOptions> & {
attempt?: number;
disconnectionTimeout?: number;
};
const kDefaultAttempt = 4;
const kDefaultTimeout = 500;
import { RedisAdapter } from "@myunisoft/redis";
const redisAdapter = new RedisAdapter();
this method is used to set a key-value pair in redis
const key = "foo";
const value = {
foo: "bar",
};
await redisAdapter.setValue({ key, value });
this method is used to get a value from redis
const key = "foo";
const result = await redisAdapter.getValue(key);
console.log(result); // { foo: "bar" }
this method is used to delete a key-value pair in redis
const key = "foo";
const result = await redisAdapter.deleteValue(key);
console.log(result); // 0 for Failure, 1 for Success