You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/util/cache/Cache.ts

78 lines
2.0 KiB

import {Awaitable} from '../support/types'
import {Safe} from '../support/Safe'
/**
* Abstract interface class for a cached object.
*/
export abstract class Cache {
/**
* Fetch a value from the cache by its key.
* @param {string} key
* @return Promise<any|undefined>
*/
public abstract fetch(key: string): Awaitable<string|undefined>;
/**
* Fetch a value from the cache by its key as a Safe value.
* @param key
*/
public async safe(key: string): Promise<Safe> {
return new Safe(await this.fetch(key))
}
/**
* Store the given value in the cache by key.
* @param {string} key
* @param {string} value
* @param expires
*/
public abstract put(key: string, value: string, expires?: Date): Awaitable<void>;
/**
* Check if the cache has the given key.
* @param {string} key
* @return Promise<boolean>
*/
public abstract has(key: string): Awaitable<boolean>;
/**
* Drop the given key from the cache.
* @param {string} key
*/
public abstract drop(key: string): Awaitable<void>;
/**
* Fetch an item from the cache by key, and then remove it.
* @param key
*/
public abstract pop(key: string): Awaitable<string|undefined>;
/**
* Increment a key in the cache by a given amount.
* @param key
* @param amount
*/
public abstract increment(key: string, amount?: number): Awaitable<number|undefined>;
/**
* Decrement a key in the cache by a given amount.
* @param key
* @param amount
*/
public abstract decrement(key: string, amount?: number): Awaitable<number|undefined>;
/**
* Push an item onto the end an array-like key.
* @param key
* @param value
*/
public abstract arrayPush(key: string, value: string): Awaitable<void>;
/**
* Remove and return an item from the beginning of an array-like key.
* @param key
* @param value
*/
public abstract arrayPop(key: string): Awaitable<string|undefined>;
}