@aurios/jason
Version:
A simple, lightweight, and embeddable JSON document database built on Bun.
50 lines (49 loc) • 1.7 kB
TypeScript
import type { BaseDocument } from "../types/index.js";
export default class Cache<T = BaseDocument> {
#private;
constructor(cacheTimout?: number, maxSize?: number);
/**
* Gets the current cache timeout duration.
* Defaults to 60 seconds (1 minute).
* @returns The duration in milliseconds after which cached items are automatically removed.
*/
get timeout(): number;
/**
* Sets the cache timeout duration.
*
* @param timeout - The duration in milliseconds after which the cached item should be removed.
*/
set timeout(timeout: number);
/**
* Updates the cache with the given id and value.
*
* If the cache has reached its maximum size, it will remove 10% of the items
* in the cache before adding the new item.
*
* @param id - The id of the item to be updated.
* @param value - The value of the item to be updated.
*/
update(id: string, value: T): void;
/**
* Retrieves an item from the cache by its id.
*
* If the item is not found or has exceeded the cache timeout, it returns null.
* Otherwise, it returns the cached value.
*
* @param id - The id of the item to retrieve from the cache.
* @returns The cached value, or null if not found or expired.
*/
get(id: string): T | null;
/**
* Removes the item with the specified id from the cache.
*
* @param id - The id of the item to be removed from the cache.
*/
delete(id: string): void;
/**
* Destroys the cache by stopping the automatic cleanup interval.
*
* Use this method when you are finished with the cache.
*/
destroy(): void;
}