axiodb
Version:
A blazing-fast, lightweight, and scalable nodejs package based DBMS for modern application. Supports schemas, encryption, and advanced query capabilities.
65 lines (64 loc) • 2.59 kB
TypeScript
/**
* @description This class is responsible for caching data in memory
* @class InMemoryCache
* @export InMemoryCache
* @version 1.0.1
* @since 23 April 2025
**/
export declare class InMemoryCache {
private readonly ttl;
private cacheObject;
private tempSearchQuery;
private readonly autoResetCacheInterval;
private readonly threshold;
/**
* Creates a new instance of the cache operation class
* @param TTL - Time to live in seconds for cache entries. Defaults to 86400 seconds (24 hours)
*/
constructor(TTL?: string | number);
/**
* Sets a value in the cache with the specified key.
* The cached item will expire after the TTL (Time To Live) duration set for the cache.
*
* @param key - The unique identifier for the cached item
* @param value - The value to be stored in the cache
* @returns A Promise that resolves when the value has been cached
*
* @example
* ```typescript
* await cache.setCache('user-123', { name: 'John', age: 30 });
* ```
*/
setCache(key: string, value: any): Promise<boolean>;
setTempSearchQuery(queryString: any): Promise<boolean>;
/**
* Retrieves a value from the cache using the specified key
* @param key - The unique identifier to lookup in the cache
* @returns A Promise that resolves to the cached value if found and not expired, null otherwise
*/
getCache(key: string): Promise<any | boolean>;
/**
* Clears all cached data stored in memory.
* Resets the cache object and temporary search query array to their initial empty states.
*
* @returns A Promise that resolves to true when the cache has been successfully cleared.
*/
clearAllCache(): Promise<boolean>;
/**
* Sets up an automatic cache reset mechanism.
* This method creates an interval timer that periodically checks and removes expired items from the cache.
*
* The method performs the following operations:
* 1. Checks if the cache is empty, and returns early if it is.
* 2. Iterates through all cache items and removes those that have expired based on the `autoResetCacheInterval`.
* 3. Filters out expired temporary search queries based on the same interval.
*
* The interval for this automatic reset is determined by the `ttl` (time-to-live) property.
*
* @private
* @returns {Promise<void>} A promise that resolves when the interval is set up.
*/
private autoResetCache;
}
declare const _default: InMemoryCache;
export default _default;