Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 1.53 KB

TimedKVPeer.md

File metadata and controls

79 lines (58 loc) · 1.53 KB

TimedKVPeer

Represents an abstraction design to store time-lifed key-value peer.

Interface

export interface TimedKVPeerOptions 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;
}

interface TimedSetValueOptions<T extends object> extends Omit<
  RedisSetValueOptions<T>,
  "expiresIn" | "key" | "type"
> {
  key?: string | Buffer;
}

📚 Usage

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
});

📜 API

constructor< T extends object >(options: TimedKVPeerOptions)

setValue(options: TimedSetValueOptions): Promise< Result< KeyType, Error > >

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);

deleteValue(key: KeyType): Promise< number >;

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