@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
60 lines (59 loc) • 2.24 kB
TypeScript
/**
* @module Cache
*/
import { type ICacheAdapter } from "../../../../cache/contracts/_module-exports.js";
import type { ISerde } from "../../../../serde/contracts/_module-exports.js";
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
import { type Redis, type Result } from "ioredis";
declare module "ioredis" {
interface RedisCommander<Context> {
daiso_cache_increment(key: string, number: string): Result<number, Context>;
}
}
/**
*
* IMPORT_PATH: `"@daiso-tech/core/cache/adapters"`
* @group Adapters
*/
export type RedisCacheAdapterSettings = {
database: Redis;
serde: ISerde<string>;
};
/**
* To utilize the `RedisCacheAdapter`, you must install the `"ioredis"` package and supply a {@link ISerde | `ISerde<string>`}, with adapter like {@link SuperJsonSerdeAdapter | `SuperJsonSerdeAdapter `}.
*
* IMPORT_PATH: `"@daiso-tech/core/cache/adapters"`
* @group Adapters
*/
export declare class RedisCacheAdapter<TType> implements ICacheAdapter<TType> {
private static isRedisTypeError;
private readonly serde;
private readonly database;
/**
* @example
* ```ts
* import { RedisCacheAdapter } from "@daiso-tech/core/cache/adapters";
* import { Serde } from "@daiso-tech/core/serde";
* import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
* import Redis from "ioredis";
*
* const database = new Redis("YOUR_REDIS_CONNECTION_STRING");
* const serde = new Serde(new SuperJsonSerdeAdapter());
* const cacheAdapter = new RedisCacheAdapter({
* database,
* serde,
* });
* ```
*/
constructor(settings: RedisCacheAdapterSettings);
private initIncrementCommand;
get(key: string): Promise<TType | null>;
getAndRemove(key: string): Promise<TType | null>;
add(key: string, value: TType, ttl: TimeSpan | null): Promise<boolean>;
put(key: string, value: TType, ttl: TimeSpan | null): Promise<boolean>;
update(key: string, value: TType): Promise<boolean>;
increment(key: string, value: number): Promise<boolean>;
removeMany(keys: string[]): Promise<boolean>;
removeAll(): Promise<void>;
removeByKeyPrefix(prefix: string): Promise<void>;
}