UNPKG

@nasriya/cachify

Version:

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

104 lines (103 loc) 4.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const atomix_1 = __importDefault(require("@nasriya/atomix")); class EvictConfig { #_updateStatus; #_data = { enabled: true, /** Default max items before oldest gets evicted */ maxRecords: 500, mode: 'lru' }; constructor(updateStatus, options) { this.#_updateStatus = updateStatus; if (options && atomix_1.default.valueIs.record(options)) { if (atomix_1.default.dataTypes.record.hasOwnProperty(options, 'enabled')) { this.enabled = options.enabled; } if (atomix_1.default.dataTypes.record.hasOwnProperty(options, 'maxRecords')) { this.maxRecords = options.maxRecords; } if (atomix_1.default.dataTypes.record.hasOwnProperty(options, 'mode')) { this.mode = options.mode; } } } /** * Retrieves whether or not the eviction policy is enabled for the cache. * When disabled, the cache will not evict items, and the cache will grow indefinitely. * @returns {boolean} Whether or not the eviction policy is enabled for the cache. */ get enabled() { return this.#_data.enabled; } /** * Sets whether or not the eviction policy is enabled for the cache. * When disabled, the cache will not evict items, and the cache will grow indefinitely. * @param {boolean} enabled Whether or not the eviction policy is enabled for the cache. * @throws {TypeError} If the provided enabled value is not a boolean. */ set enabled(value) { if (typeof value !== 'boolean') { throw new TypeError(`The provided enabled value (${value}) is not a boolean.`); } if (this.#_data.enabled !== value) { this.#_data.enabled = value; this.#_updateStatus('eviction', value ? 'enabled' : 'disabled'); } } /** * Retrieves the maximum number of records that the cache can store before * the oldest record is evicted. * @returns {number} The maximum number of records in the cache. */ get maxRecords() { return this.#_data.maxRecords; } /** * Sets the maximum number of records that the cache can store before * the oldest record is evicted. * @param {number} maxRecords The maximum number of records in the cache. Must be a number greater than 0. * @throws {TypeError} If the provided maxRecords value is not a number. * @throws {TypeError} If the provided maxRecords value is not an integer. * @throws {RangeError} If the provided maxRecords value is not greater than 0. */ set maxRecords(maxRecords) { if (!atomix_1.default.valueIs.number(maxRecords)) { throw new TypeError(`The provided maxRecords value (${maxRecords}) is not a number.`); } if (maxRecords !== Infinity) { if (!atomix_1.default.valueIs.integer(maxRecords)) { throw new TypeError(`The provided maxRecords value (${maxRecords}) is not an integer.`); } if (maxRecords <= 0) { throw new RangeError(`The provided maxRecords value (${maxRecords}) must be greater than 0.`); } } this.#_data.maxRecords = maxRecords; } /** * Retrieves the eviction mode of the cache. * The eviction mode determines which record is evicted from the cache when the cache is full and a new record is added. * The two valid values for this property are 'lru' and 'fifo'. * @returns {EvictionMode} The eviction mode of the cache. */ get mode() { return this.#_data.mode; } /** * Sets the eviction mode of the cache. * The eviction mode determines which record is evicted from the cache when the cache is full and a new record is added. * @param {EvictionMode} mode The eviction mode of the cache. Must be a string. * @throws {TypeError} If the provided mode value is not a string. * @throws {TypeError} If the provided mode value is not a valid eviction mode. */ set mode(mode) { if (typeof mode !== 'string') { throw new TypeError(`The provided mode value (${mode}) is not a string.`); } mode = mode.toLowerCase(); if (!['lru', 'fifo', 'lfu'].includes(mode)) { throw new TypeError(`The provided mode value (${mode}) is not a valid eviction mode.`); } this.#_data.mode = mode; } } exports.default = EvictConfig;