Represents an abstraction design to store time-lifed key-value peer.
export interface TimedKVPeerOptions<T extends object, K extends Record<string, any> | null = null> extends Omit<KVOptions<T, K>, "type"> {
/** How long the keys are kept, by default set to 10 minutes **/
ttl?: number;
/** A random key callback generator for setValue() method **/
randomKeyCallback?: () => string;
}
import { TimedKVPeer, MemoryAdapter } from "@myunisoft/redis";
interface MyCustomObject {
foo: string;
life: number;
isReal: boolean;
}
function randomKeyCallback() {
return randomBytes(128).toString("hex");
}
const memoryAdapter = new MemoryAdapter();
const store = new TimedKVPeer<MyCustomObject>({
adapter: memoryAdapter,
sessionDuration: 3600,
randomKeyCallback
});
this method is used to set a key-value peer
const value: MyCustomObject = {
foo: "bar",
life: 0,
isReal: true
};
const finalKey = await store.setValue(value);
console.log(finalKey);
this method is used to delete a key-value peer
const res = await store.deleteValue(finalKey);
console.log(res) // 0 for Failure, 1 for Success