lemon-core
Version:
Lemon Serverless Micro-Service Platform
184 lines (183 loc) • 4.8 kB
TypeScript
/**
* `redis-storage-services.ts`
* - storage implementation on redis backend
*
* @author Tim Hong <tim@lemoncloud.io>
* @date 2020-12-08 initial version
*
* @copyright (C) lemoncloud.io 2020 - All Rights Reserved.
*/
import { Redis } from 'ioredis';
import { StorageService, StorageModel } from './storage-service';
/** ********************************************************************************************************************
* Exported Types
** ********************************************************************************************************************/
/**
* type `RedisOptions`
*/
export interface RedisOptions {
/**
* (optional) cache server endpoint
*/
endpoint?: string;
/**
* (optional) virtual table name used as key prefix
*/
tableName: string | number;
/**
* (optional) default time to live of a model in seconds. 0 for unlimited (default: 0)
*/
defTimeout?: number;
}
/** ********************************************************************************************************************
* Exported Class
** ********************************************************************************************************************/
/**
* class `RedisStorageService`
*/
export declare class RedisStorageService<T extends StorageModel> implements StorageService<T> {
/**
* Environment variable name for redis server endpoint
* @static
*/
static readonly ENV_REDIS_ENDPOINT = "MY_REDIS_ENDPOINT";
/**
* Maximum retry count of check-and-save behavior
* @static
*/
static readonly CAS_MAX_RETRIES = 5;
/**
* ioredis client
* @protected
*/
protected readonly redis: Redis;
/**
* Virtual table name
* @private
*/
readonly tableName: string;
/**
* Default time to live of a model
* @private
*/
readonly ttl: number;
/**
* Public constructor
*
* @options redis options
*/
constructor(options: RedisOptions);
/**
* Say hello
*/
hello(): string;
/**
* Disconnect from redis
*/
quit(): Promise<void>;
/**
* Read model by id
*
* @param id
*/
read(id: string): Promise<T>;
/**
* Read model or create if id does not exist
*
* @param id
* @param model
*/
readOrCreate(id: string, model: T): Promise<T>;
/**
* Create model and overwrite if id exists
*
* @param id
* @param model
*/
save(id: string, model: T): Promise<T>;
/**
* Update existing model and create if id does not exist
* @param id
* @param update model to update
* @param increment (optional) model to increment
*/
update(id: string, update: T, increment?: T): Promise<T>;
/**
* Increment the integer value of a key
*
* @param id
* @param increment model to increment
* @param update (optional) model to update
*/
increment(id: string, increment: T, update?: T): Promise<T>;
/**
* Delete a key
*
* @param id
* @return true on success
*/
delete(id: string): Promise<T>;
/**
* Get redis key from id
* @param id
* @protected
*/
asKey(id: string): string;
/**
* Serialize model into internal data
* @param model
* @protected
*/
serialize(model: T): Record<string, string>;
/**
* Deserialize internal data into model
* @param data
* @protected
*/
deserialize(data: Record<string, string>): T;
/**
* Update key w/ check-and-save behavior and retries
* @param id
* @param update (optional) model to update
* @param increment (optional) model to increment
* @private
*/
private updateCAS;
/**
* Prepare new model - original model + update + increment
* @param orig
* @param update
* @param increment
* @private
*/
private prepareUpdatedModel;
/**
* Check transaction results and throw if error occurred
* @param results transaction pipeline execution results
* @private
*/
private static throwIfTransactionError;
}
/**
* class `DummyRedisStorageService`
* - Use local redis server and non-default logical database
*/
export declare class DummyRedisStorageService<T extends StorageModel> extends RedisStorageService<T> {
/**
* Database index. Each DummyRedisStorageService uses different logical database.
* @private
*/
private static dbIndex;
/**
* Public constructor
*/
constructor();
/**
* Say hello
*/
hello(): string;
/**
* Delete all data in database
*/
truncate(): Promise<void>;
}