UNPKG

@nasriya/cachify

Version:

A lightweight, extensible in-memory caching library for storing anything, with built-in TTL and customizable cache types.

78 lines (77 loc) 3.27 kB
import constants from "../../core/consts/consts.js"; import helpers from "../../core/persistence/helpers/helpers.js"; class ExtPersistenceManager { #_cache; #_persistence; constructor(persistence, client) { this.#_persistence = persistence; this.#_cache = client; } /** * Initiates a backup process for the specified cache flavor and storage service. * * This function dispatches the backup operation to the appropriate cache manager * based on the provided cache flavor. The `kvs` flavor targets the key-value cache, * while the `files` flavor targets the file cache. * * @template F - The cache flavor type, indicating the cache source. * @template S - The type of storage service to back up to. * @param {S} to - The target storage service for the backup. * @param {...BackupParameters<S>} args - Additional parameters for the backup operation. * @returns {Promise<void>} Resolves when the backup operation completes. * @throws {Error} If the specified cache flavor is unsupported. */ async backup(to, ...args) { for (const flavor of constants.CACHE_FLAVORS) { const manager = this.#_cache[flavor]; if (manager && typeof manager.backup === 'function') { if (manager.size === 0) { continue; } await manager.backup(to, ...args); } } } /** * Initiates a restore process from the specified storage service for all cache flavors. * * This function dispatches the restore operation to the appropriate cache manager * based on the provided cache flavor. The `kvs` flavor targets the key-value cache, * while the `files` flavor targets the file cache. * * @template S - The type of storage service to restore from. * @param {S} service - The target storage service for the restore. * @param {...RestoreParameters<S>} args - Additional parameters for the restore operation. * @returns {Promise<void>} Resolves when the restore operation completes. * @throws {Error} If the specified cache flavor is unsupported. */ async restore(service, ...args) { for (const flavor of constants.CACHE_FLAVORS) { const manager = this.#_cache[flavor]; if (manager && typeof manager.backup === 'function') { await manager.restore(service, ...args); } } } /** * Schedules a persistence operation for the specified cache flavor and storage service. * * This function is a no-op until the persistence manager is fully implemented. * */ schedule() { } /** * Registers a persistence service with the external persistence manager. * * @template S - The type of storage service. * @param {S} service - The type of storage service to be added. * @param {PersistanceStorageServices[S]['configs']} configs - The configuration settings for the service. * @since v1.0.0 */ use(service, configs) { helpers.validateService(service, configs); this.#_persistence.defineAdapter(service, configs); } } export default ExtPersistenceManager;