flat-cache
Advanced tools
+256
| import { CacheableMemory } from "cacheable"; | ||
| import { Hookified } from "hookified"; | ||
| //#region src/index.d.ts | ||
| type FlatCacheOptions = { | ||
| ttl?: number | string; | ||
| useClone?: boolean; | ||
| lruSize?: number; | ||
| expirationInterval?: number; | ||
| persistInterval?: number; | ||
| cacheDir?: string; | ||
| cacheId?: string; | ||
| deserialize?: (data: string) => any; | ||
| serialize?: (data: any) => string; | ||
| }; | ||
| declare enum FlatCacheEvents { | ||
| SAVE = "save", | ||
| LOAD = "load", | ||
| DELETE = "delete", | ||
| CLEAR = "clear", | ||
| DESTROY = "destroy", | ||
| ERROR = "error", | ||
| EXPIRED = "expired" | ||
| } | ||
| declare class FlatCache extends Hookified { | ||
| private readonly _cache; | ||
| private _cacheDir; | ||
| private _cacheId; | ||
| private _persistInterval; | ||
| private _persistTimer; | ||
| private _changesSinceLastSave; | ||
| private readonly _parse; | ||
| private readonly _stringify; | ||
| constructor(options?: FlatCacheOptions); | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache(): CacheableMemory; | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir(): string; | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value: string); | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId(): string; | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value: string); | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave(): boolean; | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval(): number; | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value: number); | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId?: string, cacheDir?: string): void; | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile: string): void; | ||
| loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void; | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all(): Record<string, any>; | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items(): Array<{ | ||
| key: string; | ||
| value: any; | ||
| expires?: number; | ||
| }>; | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath(): string; | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath(): string; | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys(): string[]; | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key: string): void; | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key: string): void; | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey<T>(key: string): T; | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get<T>(key: string): T; | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear(): void; | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force?: boolean): void; | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile(): boolean; | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory?: boolean): void; | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist(): void; | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist(): void; | ||
| } | ||
| declare class FlatCacheDefault { | ||
| static create: typeof create; | ||
| static createFromFile: typeof createFromFile; | ||
| static clearCacheById: typeof clearCacheById; | ||
| static clearAll: typeof clearAll; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. | ||
| * | ||
| * @method create | ||
| * @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| declare function create(options?: FlatCacheOptions): FlatCache; | ||
| /** | ||
| * Load a cache from the provided file | ||
| * @method createFromFile | ||
| * @param {String} filePath the path to the file containing the info for the cache | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache; | ||
| /** | ||
| * Clear the cache identified by the given Id. This will only remove the cache from disk. | ||
| * @method clearCacheById | ||
| * @param cacheId {String} the id of the cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| */ | ||
| declare function clearCacheById(cacheId: string, cacheDirectory?: string): void; | ||
| /** | ||
| * Clear the cache directory | ||
| * @method clearAll | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| declare function clearAll(cacheDirectory?: string): void; | ||
| //#endregion | ||
| export { FlatCache, FlatCacheEvents, FlatCacheOptions, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default }; |
+451
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { CacheableMemory } from "cacheable"; | ||
| import { parse, stringify } from "flatted"; | ||
| import { Hookified } from "hookified"; | ||
| //#region src/index.ts | ||
| let FlatCacheEvents = /* @__PURE__ */ function(FlatCacheEvents) { | ||
| FlatCacheEvents["SAVE"] = "save"; | ||
| FlatCacheEvents["LOAD"] = "load"; | ||
| FlatCacheEvents["DELETE"] = "delete"; | ||
| FlatCacheEvents["CLEAR"] = "clear"; | ||
| FlatCacheEvents["DESTROY"] = "destroy"; | ||
| FlatCacheEvents["ERROR"] = "error"; | ||
| FlatCacheEvents["EXPIRED"] = "expired"; | ||
| return FlatCacheEvents; | ||
| }({}); | ||
| var FlatCache = class extends Hookified { | ||
| _cache = new CacheableMemory(); | ||
| _cacheDir = ".cache"; | ||
| _cacheId = "cache1"; | ||
| _persistInterval = 0; | ||
| _persistTimer; | ||
| _changesSinceLastSave = false; | ||
| _parse = parse; | ||
| _stringify = stringify; | ||
| constructor(options) { | ||
| super(); | ||
| if (options) this._cache = new CacheableMemory({ | ||
| ttl: options.ttl, | ||
| useClone: options.useClone, | ||
| lruSize: options.lruSize, | ||
| checkInterval: options.expirationInterval | ||
| }); | ||
| if (options?.cacheDir) this._cacheDir = options.cacheDir; | ||
| if (options?.cacheId) this._cacheId = options.cacheId; | ||
| if (options?.persistInterval) { | ||
| this._persistInterval = options.persistInterval; | ||
| this.startAutoPersist(); | ||
| } | ||
| if (options?.deserialize) this._parse = options.deserialize; | ||
| if (options?.serialize) this._stringify = options.serialize; | ||
| } | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache() { | ||
| return this._cache; | ||
| } | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir() { | ||
| return this._cacheDir; | ||
| } | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value) { | ||
| this._cacheDir = value; | ||
| } | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId() { | ||
| return this._cacheId; | ||
| } | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value) { | ||
| this._cacheId = value; | ||
| } | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave() { | ||
| return this._changesSinceLastSave; | ||
| } | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval() { | ||
| return this._persistInterval; | ||
| } | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value) { | ||
| this._persistInterval = value; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId, cacheDir) { | ||
| try { | ||
| const filePath = path.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`); | ||
| this.loadFile(filePath); | ||
| this.emit("load"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile) { | ||
| if (fs.existsSync(pathToFile)) { | ||
| const data = fs.readFileSync(pathToFile, "utf8"); | ||
| const items = this._parse(data); | ||
| if (Array.isArray(items)) { | ||
| for (const item of items) if (item && typeof item === "object" && "key" in item) if (item.expires) this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| else if (item.timestamp) | ||
| /* v8 ignore next -- @preserve */ | ||
| this._cache.set(item.key, item.value, { expire: item.timestamp }); | ||
| else this._cache.set(item.key, item.value); | ||
| } else for (const key of Object.keys(items)) { | ||
| const item = items[key]; | ||
| /* v8 ignore next -- @preserve */ | ||
| if (item && typeof item === "object" && "key" in item) this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| else if (item && typeof item === "object" && item.timestamp) | ||
| /* v8 ignore next -- @preserve */ | ||
| this._cache.set(key, item, { expire: item.timestamp }); | ||
| else this._cache.set(key, item); | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| } | ||
| loadFileStream(pathToFile, onProgress, onEnd, onError) { | ||
| if (fs.existsSync(pathToFile)) { | ||
| const total = fs.statSync(pathToFile).size; | ||
| let loaded = 0; | ||
| let streamData = ""; | ||
| const readStream = fs.createReadStream(pathToFile, { encoding: "utf8" }); | ||
| readStream.on("data", (chunk) => { | ||
| loaded += chunk.length; | ||
| streamData += chunk; | ||
| onProgress(loaded, total); | ||
| }); | ||
| readStream.on("end", () => { | ||
| const items = this._parse(streamData); | ||
| for (const key of Object.keys(items)) this._cache.set(items[key].key, items[key].value, { expire: items[key].expires }); | ||
| this._changesSinceLastSave = true; | ||
| onEnd(); | ||
| }); | ||
| /* v8 ignore next -- @preserve */ | ||
| readStream.on("error", (error) => { | ||
| this.emit("error", error); | ||
| if (onError) onError(error); | ||
| }); | ||
| } else { | ||
| const error = /* @__PURE__ */ new Error(`Cache file ${pathToFile} does not exist`); | ||
| this.emit("error", error); | ||
| /* v8 ignore next -- @preserve */ | ||
| if (onError) onError(error); | ||
| } | ||
| } | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all() { | ||
| const result = {}; | ||
| const items = [...this._cache.items]; | ||
| for (const item of items) result[item.key] = item.value; | ||
| return result; | ||
| } | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items() { | ||
| return [...this._cache.items]; | ||
| } | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath() { | ||
| return path.resolve(`${this._cacheDir}/${this._cacheId}`); | ||
| } | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath() { | ||
| return path.resolve(this._cacheDir); | ||
| } | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys() { | ||
| return [...this._cache.keys]; | ||
| } | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key, value, ttl) { | ||
| this.set(key, value, ttl); | ||
| } | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key, value, ttl) { | ||
| this._cache.set(key, value, ttl); | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key) { | ||
| this.delete(key); | ||
| } | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key) { | ||
| this._cache.delete(key); | ||
| this._changesSinceLastSave = true; | ||
| this.emit("delete", key); | ||
| } | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey(key) { | ||
| return this.get(key); | ||
| } | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get(key) { | ||
| return this._cache.get(key); | ||
| } | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear() { | ||
| try { | ||
| this._cache.clear(); | ||
| this._changesSinceLastSave = true; | ||
| this.save(); | ||
| this.emit("clear"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force = false) { | ||
| try { | ||
| /* v8 ignore next -- @preserve */ | ||
| if (this._changesSinceLastSave || force) { | ||
| const filePath = this.cacheFilePath; | ||
| const items = [...this._cache.items]; | ||
| const data = this._stringify(items); | ||
| if (!fs.existsSync(this._cacheDir)) fs.mkdirSync(this._cacheDir, { recursive: true }); | ||
| fs.writeFileSync(filePath, data); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("save"); | ||
| } | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile() { | ||
| try { | ||
| if (fs.existsSync(this.cacheFilePath)) { | ||
| fs.rmSync(this.cacheFilePath); | ||
| return true; | ||
| } | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| return false; | ||
| } | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory = false) { | ||
| try { | ||
| this._cache.clear(); | ||
| this.stopAutoPersist(); | ||
| if (includeCacheDirectory) fs.rmSync(this.cacheDirPath, { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| else fs.rmSync(this.cacheFilePath, { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("destroy"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist() { | ||
| /* v8 ignore next -- @preserve */ | ||
| if (this._persistInterval > 0) { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| this._persistTimer = setInterval(() => { | ||
| this.save(); | ||
| }, this._persistInterval); | ||
| } | ||
| } | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist() { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| } | ||
| }; | ||
| var FlatCacheDefault = class { | ||
| static create = create; | ||
| static createFromFile = createFromFile; | ||
| static clearCacheById = clearCacheById; | ||
| static clearAll = clearAll; | ||
| }; | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. | ||
| * | ||
| * @method create | ||
| * @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| function create(options) { | ||
| const cache = new FlatCache(options); | ||
| cache.load(); | ||
| return cache; | ||
| } | ||
| /** | ||
| * Load a cache from the provided file | ||
| * @method createFromFile | ||
| * @param {String} filePath the path to the file containing the info for the cache | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| function createFromFile(filePath, options) { | ||
| const cache = new FlatCache(options); | ||
| cache.loadFile(filePath); | ||
| return cache; | ||
| } | ||
| /** | ||
| * Clear the cache identified by the given Id. This will only remove the cache from disk. | ||
| * @method clearCacheById | ||
| * @param cacheId {String} the id of the cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| */ | ||
| function clearCacheById(cacheId, cacheDirectory) { | ||
| new FlatCache({ | ||
| cacheId, | ||
| cacheDir: cacheDirectory | ||
| }).destroy(); | ||
| } | ||
| /** | ||
| * Clear the cache directory | ||
| * @method clearAll | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| function clearAll(cacheDirectory) { | ||
| fs.rmSync(cacheDirectory ?? ".cache", { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| } | ||
| //#endregion | ||
| export { FlatCache, FlatCacheEvents, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default }; |
+466
-476
@@ -1,2 +0,6 @@ | ||
| "use strict"; | ||
| Object.defineProperties(exports, { | ||
| __esModule: { value: true }, | ||
| [Symbol.toStringTag]: { value: "Module" } | ||
| }); | ||
| //#region \0rolldown/runtime.js | ||
| var __create = Object.create; | ||
@@ -8,489 +12,475 @@ var __defProp = Object.defineProperty; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { | ||
| key = keys[i]; | ||
| if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { | ||
| get: ((k) => from[k]).bind(null, key), | ||
| enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable | ||
| }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
| // If the importer is in node compatibility mode or this is not an ESM | ||
| // file that has been converted to a CommonJS file using a Babel- | ||
| // compatible transform (i.e. "__esModule" has not been set), then set | ||
| // "default" to the CommonJS "module.exports" for node compatibility. | ||
| isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
| mod | ||
| )); | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/index.ts | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| FlatCache: () => FlatCache, | ||
| FlatCacheEvents: () => FlatCacheEvents, | ||
| clearAll: () => clearAll, | ||
| clearCacheById: () => clearCacheById, | ||
| create: () => create, | ||
| createFromFile: () => createFromFile, | ||
| default: () => FlatCacheDefault | ||
| }); | ||
| module.exports = __toCommonJS(index_exports); | ||
| var import_node_fs = __toESM(require("fs"), 1); | ||
| var import_node_path = __toESM(require("path"), 1); | ||
| var import_cacheable = require("cacheable"); | ||
| var import_flatted = require("flatted"); | ||
| var import_hookified = require("hookified"); | ||
| var FlatCacheEvents = /* @__PURE__ */ ((FlatCacheEvents2) => { | ||
| FlatCacheEvents2["SAVE"] = "save"; | ||
| FlatCacheEvents2["LOAD"] = "load"; | ||
| FlatCacheEvents2["DELETE"] = "delete"; | ||
| FlatCacheEvents2["CLEAR"] = "clear"; | ||
| FlatCacheEvents2["DESTROY"] = "destroy"; | ||
| FlatCacheEvents2["ERROR"] = "error"; | ||
| FlatCacheEvents2["EXPIRED"] = "expired"; | ||
| return FlatCacheEvents2; | ||
| })(FlatCacheEvents || {}); | ||
| var FlatCache = class extends import_hookified.Hookified { | ||
| _cache = new import_cacheable.CacheableMemory(); | ||
| _cacheDir = ".cache"; | ||
| _cacheId = "cache1"; | ||
| _persistInterval = 0; | ||
| _persistTimer; | ||
| _changesSinceLastSave = false; | ||
| _parse = import_flatted.parse; | ||
| _stringify = import_flatted.stringify; | ||
| constructor(options) { | ||
| super(); | ||
| if (options) { | ||
| this._cache = new import_cacheable.CacheableMemory({ | ||
| ttl: options.ttl, | ||
| useClone: options.useClone, | ||
| lruSize: options.lruSize, | ||
| checkInterval: options.expirationInterval | ||
| }); | ||
| } | ||
| if (options?.cacheDir) { | ||
| this._cacheDir = options.cacheDir; | ||
| } | ||
| if (options?.cacheId) { | ||
| this._cacheId = options.cacheId; | ||
| } | ||
| if (options?.persistInterval) { | ||
| this._persistInterval = options.persistInterval; | ||
| this.startAutoPersist(); | ||
| } | ||
| if (options?.deserialize) { | ||
| this._parse = options.deserialize; | ||
| } | ||
| if (options?.serialize) { | ||
| this._stringify = options.serialize; | ||
| } | ||
| } | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache() { | ||
| return this._cache; | ||
| } | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir() { | ||
| return this._cacheDir; | ||
| } | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value) { | ||
| this._cacheDir = value; | ||
| } | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId() { | ||
| return this._cacheId; | ||
| } | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value) { | ||
| this._cacheId = value; | ||
| } | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave() { | ||
| return this._changesSinceLastSave; | ||
| } | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval() { | ||
| return this._persistInterval; | ||
| } | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value) { | ||
| this._persistInterval = value; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId, cacheDir) { | ||
| try { | ||
| const filePath = import_node_path.default.resolve( | ||
| `${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}` | ||
| ); | ||
| this.loadFile(filePath); | ||
| this.emit("load" /* LOAD */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile) { | ||
| if (import_node_fs.default.existsSync(pathToFile)) { | ||
| const data = import_node_fs.default.readFileSync(pathToFile, "utf8"); | ||
| const items = this._parse(data); | ||
| if (Array.isArray(items)) { | ||
| for (const item of items) { | ||
| if (item && typeof item === "object" && "key" in item) { | ||
| if (item.expires) { | ||
| this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| } else if (item.timestamp) { | ||
| this._cache.set(item.key, item.value, { expire: item.timestamp }); | ||
| } else { | ||
| this._cache.set(item.key, item.value); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| for (const key of Object.keys(items)) { | ||
| const item = items[key]; | ||
| if (item && typeof item === "object" && "key" in item) { | ||
| this._cache.set(item.key, item.value, { | ||
| expire: item.expires | ||
| }); | ||
| } else { | ||
| if (item && typeof item === "object" && item.timestamp) { | ||
| this._cache.set(key, item, { expire: item.timestamp }); | ||
| } else { | ||
| this._cache.set(key, item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| } | ||
| loadFileStream(pathToFile, onProgress, onEnd, onError) { | ||
| if (import_node_fs.default.existsSync(pathToFile)) { | ||
| const stats = import_node_fs.default.statSync(pathToFile); | ||
| const total = stats.size; | ||
| let loaded = 0; | ||
| let streamData = ""; | ||
| const readStream = import_node_fs.default.createReadStream(pathToFile, { encoding: "utf8" }); | ||
| readStream.on("data", (chunk) => { | ||
| loaded += chunk.length; | ||
| streamData += chunk; | ||
| onProgress(loaded, total); | ||
| }); | ||
| readStream.on("end", () => { | ||
| const items = this._parse(streamData); | ||
| for (const key of Object.keys(items)) { | ||
| this._cache.set(items[key].key, items[key].value, { | ||
| expire: items[key].expires | ||
| }); | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| onEnd(); | ||
| }); | ||
| readStream.on("error", (error) => { | ||
| this.emit("error" /* ERROR */, error); | ||
| if (onError) { | ||
| onError(error); | ||
| } | ||
| }); | ||
| } else { | ||
| const error = new Error(`Cache file ${pathToFile} does not exist`); | ||
| this.emit("error" /* ERROR */, error); | ||
| if (onError) { | ||
| onError(error); | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all() { | ||
| const result = {}; | ||
| const items = [...this._cache.items]; | ||
| for (const item of items) { | ||
| result[item.key] = item.value; | ||
| } | ||
| return result; | ||
| } | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: cache items can store any value | ||
| get items() { | ||
| return [...this._cache.items]; | ||
| } | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath() { | ||
| return import_node_path.default.resolve(`${this._cacheDir}/${this._cacheId}`); | ||
| } | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath() { | ||
| return import_node_path.default.resolve(this._cacheDir); | ||
| } | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys() { | ||
| return [...this._cache.keys]; | ||
| } | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: type format | ||
| setKey(key, value, ttl) { | ||
| this.set(key, value, ttl); | ||
| } | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: type format | ||
| set(key, value, ttl) { | ||
| this._cache.set(key, value, ttl); | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key) { | ||
| this.delete(key); | ||
| } | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key) { | ||
| this._cache.delete(key); | ||
| this._changesSinceLastSave = true; | ||
| this.emit("delete" /* DELETE */, key); | ||
| } | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey(key) { | ||
| return this.get(key); | ||
| } | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get(key) { | ||
| return this._cache.get(key); | ||
| } | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear() { | ||
| try { | ||
| this._cache.clear(); | ||
| this._changesSinceLastSave = true; | ||
| this.save(); | ||
| this.emit("clear" /* CLEAR */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force = false) { | ||
| try { | ||
| if (this._changesSinceLastSave || force) { | ||
| const filePath = this.cacheFilePath; | ||
| const items = [...this._cache.items]; | ||
| const data = this._stringify(items); | ||
| if (!import_node_fs.default.existsSync(this._cacheDir)) { | ||
| import_node_fs.default.mkdirSync(this._cacheDir, { recursive: true }); | ||
| } | ||
| import_node_fs.default.writeFileSync(filePath, data); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("save" /* SAVE */); | ||
| } | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile() { | ||
| try { | ||
| if (import_node_fs.default.existsSync(this.cacheFilePath)) { | ||
| import_node_fs.default.rmSync(this.cacheFilePath); | ||
| return true; | ||
| } | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| return false; | ||
| } | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory = false) { | ||
| try { | ||
| this._cache.clear(); | ||
| this.stopAutoPersist(); | ||
| if (includeCacheDirectory) { | ||
| import_node_fs.default.rmSync(this.cacheDirPath, { recursive: true, force: true }); | ||
| } else { | ||
| import_node_fs.default.rmSync(this.cacheFilePath, { recursive: true, force: true }); | ||
| } | ||
| this._changesSinceLastSave = false; | ||
| this.emit("destroy" /* DESTROY */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist() { | ||
| if (this._persistInterval > 0) { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| this._persistTimer = setInterval(() => { | ||
| this.save(); | ||
| }, this._persistInterval); | ||
| } | ||
| } | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist() { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| } | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { | ||
| value: mod, | ||
| enumerable: true | ||
| }) : target, mod)); | ||
| //#endregion | ||
| let node_fs = require("node:fs"); | ||
| node_fs = __toESM(node_fs, 1); | ||
| let node_path = require("node:path"); | ||
| node_path = __toESM(node_path, 1); | ||
| let cacheable = require("cacheable"); | ||
| let flatted = require("flatted"); | ||
| let hookified = require("hookified"); | ||
| //#region src/index.ts | ||
| let FlatCacheEvents = /* @__PURE__ */ function(FlatCacheEvents) { | ||
| FlatCacheEvents["SAVE"] = "save"; | ||
| FlatCacheEvents["LOAD"] = "load"; | ||
| FlatCacheEvents["DELETE"] = "delete"; | ||
| FlatCacheEvents["CLEAR"] = "clear"; | ||
| FlatCacheEvents["DESTROY"] = "destroy"; | ||
| FlatCacheEvents["ERROR"] = "error"; | ||
| FlatCacheEvents["EXPIRED"] = "expired"; | ||
| return FlatCacheEvents; | ||
| }({}); | ||
| var FlatCache = class extends hookified.Hookified { | ||
| _cache = new cacheable.CacheableMemory(); | ||
| _cacheDir = ".cache"; | ||
| _cacheId = "cache1"; | ||
| _persistInterval = 0; | ||
| _persistTimer; | ||
| _changesSinceLastSave = false; | ||
| _parse = flatted.parse; | ||
| _stringify = flatted.stringify; | ||
| constructor(options) { | ||
| super(); | ||
| if (options) this._cache = new cacheable.CacheableMemory({ | ||
| ttl: options.ttl, | ||
| useClone: options.useClone, | ||
| lruSize: options.lruSize, | ||
| checkInterval: options.expirationInterval | ||
| }); | ||
| if (options?.cacheDir) this._cacheDir = options.cacheDir; | ||
| if (options?.cacheId) this._cacheId = options.cacheId; | ||
| if (options?.persistInterval) { | ||
| this._persistInterval = options.persistInterval; | ||
| this.startAutoPersist(); | ||
| } | ||
| if (options?.deserialize) this._parse = options.deserialize; | ||
| if (options?.serialize) this._stringify = options.serialize; | ||
| } | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache() { | ||
| return this._cache; | ||
| } | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir() { | ||
| return this._cacheDir; | ||
| } | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value) { | ||
| this._cacheDir = value; | ||
| } | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId() { | ||
| return this._cacheId; | ||
| } | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value) { | ||
| this._cacheId = value; | ||
| } | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave() { | ||
| return this._changesSinceLastSave; | ||
| } | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval() { | ||
| return this._persistInterval; | ||
| } | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value) { | ||
| this._persistInterval = value; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId, cacheDir) { | ||
| try { | ||
| const filePath = node_path.default.resolve(`${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}`); | ||
| this.loadFile(filePath); | ||
| this.emit("load"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile) { | ||
| if (node_fs.default.existsSync(pathToFile)) { | ||
| const data = node_fs.default.readFileSync(pathToFile, "utf8"); | ||
| const items = this._parse(data); | ||
| if (Array.isArray(items)) { | ||
| for (const item of items) if (item && typeof item === "object" && "key" in item) if (item.expires) this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| else if (item.timestamp) | ||
| /* v8 ignore next -- @preserve */ | ||
| this._cache.set(item.key, item.value, { expire: item.timestamp }); | ||
| else this._cache.set(item.key, item.value); | ||
| } else for (const key of Object.keys(items)) { | ||
| const item = items[key]; | ||
| /* v8 ignore next -- @preserve */ | ||
| if (item && typeof item === "object" && "key" in item) this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| else if (item && typeof item === "object" && item.timestamp) | ||
| /* v8 ignore next -- @preserve */ | ||
| this._cache.set(key, item, { expire: item.timestamp }); | ||
| else this._cache.set(key, item); | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| } | ||
| loadFileStream(pathToFile, onProgress, onEnd, onError) { | ||
| if (node_fs.default.existsSync(pathToFile)) { | ||
| const total = node_fs.default.statSync(pathToFile).size; | ||
| let loaded = 0; | ||
| let streamData = ""; | ||
| const readStream = node_fs.default.createReadStream(pathToFile, { encoding: "utf8" }); | ||
| readStream.on("data", (chunk) => { | ||
| loaded += chunk.length; | ||
| streamData += chunk; | ||
| onProgress(loaded, total); | ||
| }); | ||
| readStream.on("end", () => { | ||
| const items = this._parse(streamData); | ||
| for (const key of Object.keys(items)) this._cache.set(items[key].key, items[key].value, { expire: items[key].expires }); | ||
| this._changesSinceLastSave = true; | ||
| onEnd(); | ||
| }); | ||
| /* v8 ignore next -- @preserve */ | ||
| readStream.on("error", (error) => { | ||
| this.emit("error", error); | ||
| if (onError) onError(error); | ||
| }); | ||
| } else { | ||
| const error = /* @__PURE__ */ new Error(`Cache file ${pathToFile} does not exist`); | ||
| this.emit("error", error); | ||
| /* v8 ignore next -- @preserve */ | ||
| if (onError) onError(error); | ||
| } | ||
| } | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all() { | ||
| const result = {}; | ||
| const items = [...this._cache.items]; | ||
| for (const item of items) result[item.key] = item.value; | ||
| return result; | ||
| } | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items() { | ||
| return [...this._cache.items]; | ||
| } | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath() { | ||
| return node_path.default.resolve(`${this._cacheDir}/${this._cacheId}`); | ||
| } | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath() { | ||
| return node_path.default.resolve(this._cacheDir); | ||
| } | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys() { | ||
| return [...this._cache.keys]; | ||
| } | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key, value, ttl) { | ||
| this.set(key, value, ttl); | ||
| } | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key, value, ttl) { | ||
| this._cache.set(key, value, ttl); | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key) { | ||
| this.delete(key); | ||
| } | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key) { | ||
| this._cache.delete(key); | ||
| this._changesSinceLastSave = true; | ||
| this.emit("delete", key); | ||
| } | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey(key) { | ||
| return this.get(key); | ||
| } | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get(key) { | ||
| return this._cache.get(key); | ||
| } | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear() { | ||
| try { | ||
| this._cache.clear(); | ||
| this._changesSinceLastSave = true; | ||
| this.save(); | ||
| this.emit("clear"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force = false) { | ||
| try { | ||
| /* v8 ignore next -- @preserve */ | ||
| if (this._changesSinceLastSave || force) { | ||
| const filePath = this.cacheFilePath; | ||
| const items = [...this._cache.items]; | ||
| const data = this._stringify(items); | ||
| if (!node_fs.default.existsSync(this._cacheDir)) node_fs.default.mkdirSync(this._cacheDir, { recursive: true }); | ||
| node_fs.default.writeFileSync(filePath, data); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("save"); | ||
| } | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile() { | ||
| try { | ||
| if (node_fs.default.existsSync(this.cacheFilePath)) { | ||
| node_fs.default.rmSync(this.cacheFilePath); | ||
| return true; | ||
| } | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| return false; | ||
| } | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory = false) { | ||
| try { | ||
| this._cache.clear(); | ||
| this.stopAutoPersist(); | ||
| if (includeCacheDirectory) node_fs.default.rmSync(this.cacheDirPath, { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| else node_fs.default.rmSync(this.cacheFilePath, { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("destroy"); | ||
| } catch (error) { | ||
| /* v8 ignore next -- @preserve */ | ||
| this.emit("error", error); | ||
| } | ||
| } | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist() { | ||
| /* v8 ignore next -- @preserve */ | ||
| if (this._persistInterval > 0) { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| this._persistTimer = setInterval(() => { | ||
| this.save(); | ||
| }, this._persistInterval); | ||
| } | ||
| } | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist() { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| } | ||
| }; | ||
| var FlatCacheDefault = class { | ||
| static create = create; | ||
| static createFromFile = createFromFile; | ||
| static clearCacheById = clearCacheById; | ||
| static clearAll = clearAll; | ||
| static create = create; | ||
| static createFromFile = createFromFile; | ||
| static clearCacheById = clearCacheById; | ||
| static clearAll = clearAll; | ||
| }; | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. | ||
| * | ||
| * @method create | ||
| * @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| function create(options) { | ||
| const cache = new FlatCache(options); | ||
| cache.load(); | ||
| return cache; | ||
| const cache = new FlatCache(options); | ||
| cache.load(); | ||
| return cache; | ||
| } | ||
| /** | ||
| * Load a cache from the provided file | ||
| * @method createFromFile | ||
| * @param {String} filePath the path to the file containing the info for the cache | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| function createFromFile(filePath, options) { | ||
| const cache = new FlatCache(options); | ||
| cache.loadFile(filePath); | ||
| return cache; | ||
| const cache = new FlatCache(options); | ||
| cache.loadFile(filePath); | ||
| return cache; | ||
| } | ||
| /** | ||
| * Clear the cache identified by the given Id. This will only remove the cache from disk. | ||
| * @method clearCacheById | ||
| * @param cacheId {String} the id of the cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| */ | ||
| function clearCacheById(cacheId, cacheDirectory) { | ||
| const cache = new FlatCache({ cacheId, cacheDir: cacheDirectory }); | ||
| cache.destroy(); | ||
| new FlatCache({ | ||
| cacheId, | ||
| cacheDir: cacheDirectory | ||
| }).destroy(); | ||
| } | ||
| /** | ||
| * Clear the cache directory | ||
| * @method clearAll | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| function clearAll(cacheDirectory) { | ||
| import_node_fs.default.rmSync(cacheDirectory ?? ".cache", { recursive: true, force: true }); | ||
| node_fs.default.rmSync(cacheDirectory ?? ".cache", { | ||
| recursive: true, | ||
| force: true | ||
| }); | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| FlatCache, | ||
| FlatCacheEvents, | ||
| clearAll, | ||
| clearCacheById, | ||
| create, | ||
| createFromFile | ||
| }); | ||
| /* v8 ignore next -- @preserve */ | ||
| //#endregion | ||
| exports.FlatCache = FlatCache; | ||
| exports.FlatCacheEvents = FlatCacheEvents; | ||
| exports.clearAll = clearAll; | ||
| exports.clearCacheById = clearCacheById; | ||
| exports.create = create; | ||
| exports.createFromFile = createFromFile; | ||
| exports.default = FlatCacheDefault; |
+215
-214
@@ -1,221 +0,222 @@ | ||
| import { CacheableMemory } from 'cacheable'; | ||
| import { Hookified } from 'hookified'; | ||
| import { CacheableMemory } from "cacheable"; | ||
| import { Hookified } from "hookified"; | ||
| //#region src/index.d.ts | ||
| type FlatCacheOptions = { | ||
| ttl?: number | string; | ||
| useClone?: boolean; | ||
| lruSize?: number; | ||
| expirationInterval?: number; | ||
| persistInterval?: number; | ||
| cacheDir?: string; | ||
| cacheId?: string; | ||
| deserialize?: (data: string) => any; | ||
| serialize?: (data: any) => string; | ||
| ttl?: number | string; | ||
| useClone?: boolean; | ||
| lruSize?: number; | ||
| expirationInterval?: number; | ||
| persistInterval?: number; | ||
| cacheDir?: string; | ||
| cacheId?: string; | ||
| deserialize?: (data: string) => any; | ||
| serialize?: (data: any) => string; | ||
| }; | ||
| declare enum FlatCacheEvents { | ||
| SAVE = "save", | ||
| LOAD = "load", | ||
| DELETE = "delete", | ||
| CLEAR = "clear", | ||
| DESTROY = "destroy", | ||
| ERROR = "error", | ||
| EXPIRED = "expired" | ||
| SAVE = "save", | ||
| LOAD = "load", | ||
| DELETE = "delete", | ||
| CLEAR = "clear", | ||
| DESTROY = "destroy", | ||
| ERROR = "error", | ||
| EXPIRED = "expired" | ||
| } | ||
| declare class FlatCache extends Hookified { | ||
| private readonly _cache; | ||
| private _cacheDir; | ||
| private _cacheId; | ||
| private _persistInterval; | ||
| private _persistTimer; | ||
| private _changesSinceLastSave; | ||
| private readonly _parse; | ||
| private readonly _stringify; | ||
| constructor(options?: FlatCacheOptions); | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache(): CacheableMemory; | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir(): string; | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value: string); | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId(): string; | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value: string); | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave(): boolean; | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval(): number; | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value: number); | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId?: string, cacheDir?: string): void; | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile: string): void; | ||
| loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void; | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all(): Record<string, any>; | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items(): Array<{ | ||
| key: string; | ||
| value: any; | ||
| expires?: number; | ||
| }>; | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath(): string; | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath(): string; | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys(): string[]; | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key: string): void; | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key: string): void; | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey<T>(key: string): T; | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get<T>(key: string): T; | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear(): void; | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force?: boolean): void; | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile(): boolean; | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory?: boolean): void; | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist(): void; | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist(): void; | ||
| private readonly _cache; | ||
| private _cacheDir; | ||
| private _cacheId; | ||
| private _persistInterval; | ||
| private _persistTimer; | ||
| private _changesSinceLastSave; | ||
| private readonly _parse; | ||
| private readonly _stringify; | ||
| constructor(options?: FlatCacheOptions); | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache(): CacheableMemory; | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir(): string; | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value: string); | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId(): string; | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value: string); | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave(): boolean; | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval(): number; | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value: number); | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId?: string, cacheDir?: string): void; | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile: string): void; | ||
| loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void; | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all(): Record<string, any>; | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items(): Array<{ | ||
| key: string; | ||
| value: any; | ||
| expires?: number; | ||
| }>; | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath(): string; | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath(): string; | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys(): string[]; | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key: string): void; | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key: string): void; | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey<T>(key: string): T; | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get<T>(key: string): T; | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear(): void; | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force?: boolean): void; | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile(): boolean; | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory?: boolean): void; | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist(): void; | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist(): void; | ||
| } | ||
| declare class FlatCacheDefault { | ||
| static create: typeof create; | ||
| static createFromFile: typeof createFromFile; | ||
| static clearCacheById: typeof clearCacheById; | ||
| static clearAll: typeof clearAll; | ||
| static create: typeof create; | ||
| static createFromFile: typeof createFromFile; | ||
| static clearCacheById: typeof clearCacheById; | ||
| static clearAll: typeof clearAll; | ||
| } | ||
@@ -254,3 +255,3 @@ /** | ||
| declare function clearAll(cacheDirectory?: string): void; | ||
| export { FlatCache, FlatCacheEvents, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default }; | ||
| //#endregion | ||
| export { FlatCache, FlatCacheEvents, FlatCacheOptions, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default }; |
+9
-9
| { | ||
| "name": "flat-cache", | ||
| "version": "6.1.22", | ||
| "version": "6.1.23", | ||
| "description": "A simple key/value storage using files to persist the data", | ||
| "type": "module", | ||
| "main": "./dist/index.js", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "main": "./dist/index.cjs", | ||
| "module": "./dist/index.mjs", | ||
| "types": "./dist/index.d.mts", | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| "types": "./dist/index.d.mts", | ||
| "default": "./dist/index.mjs" | ||
| }, | ||
@@ -61,3 +61,3 @@ "require": { | ||
| "devDependencies": { | ||
| "tsup": "^8.5.1", | ||
| "tsdown": "^0.22.0", | ||
| "typescript": "^5.9.3" | ||
@@ -68,3 +68,3 @@ }, | ||
| "hookified": "^1.15.0", | ||
| "cacheable": "^2.3.4" | ||
| "cacheable": "^2.5.0" | ||
| }, | ||
@@ -76,3 +76,3 @@ "files": [ | ||
| "scripts": { | ||
| "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean", | ||
| "build": "rimraf ./dist && tsdown src/index.ts --format cjs,esm --dts --clean", | ||
| "lint": "biome check --write --error-on-warnings", | ||
@@ -79,0 +79,0 @@ "test": "pnpm lint && vitest run --coverage", |
-255
| import { CacheableMemory } from 'cacheable'; | ||
| import { Hookified } from 'hookified'; | ||
| type FlatCacheOptions = { | ||
| ttl?: number | string; | ||
| useClone?: boolean; | ||
| lruSize?: number; | ||
| expirationInterval?: number; | ||
| persistInterval?: number; | ||
| cacheDir?: string; | ||
| cacheId?: string; | ||
| deserialize?: (data: string) => any; | ||
| serialize?: (data: any) => string; | ||
| }; | ||
| declare enum FlatCacheEvents { | ||
| SAVE = "save", | ||
| LOAD = "load", | ||
| DELETE = "delete", | ||
| CLEAR = "clear", | ||
| DESTROY = "destroy", | ||
| ERROR = "error", | ||
| EXPIRED = "expired" | ||
| } | ||
| declare class FlatCache extends Hookified { | ||
| private readonly _cache; | ||
| private _cacheDir; | ||
| private _cacheId; | ||
| private _persistInterval; | ||
| private _persistTimer; | ||
| private _changesSinceLastSave; | ||
| private readonly _parse; | ||
| private readonly _stringify; | ||
| constructor(options?: FlatCacheOptions); | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache(): CacheableMemory; | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir(): string; | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value: string); | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId(): string; | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value: string); | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave(): boolean; | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval(): number; | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value: number); | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId?: string, cacheDir?: string): void; | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile: string): void; | ||
| loadFileStream(pathToFile: string, onProgress: (progress: number, total: number) => void, onEnd: () => void, onError?: (error: Error) => void): void; | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all(): Record<string, any>; | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| get items(): Array<{ | ||
| key: string; | ||
| value: any; | ||
| expires?: number; | ||
| }>; | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath(): string; | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath(): string; | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys(): string[]; | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| setKey(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| set(key: string, value: any, ttl?: number | string): void; | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key: string): void; | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key: string): void; | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey<T>(key: string): T; | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get<T>(key: string): T; | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear(): void; | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force?: boolean): void; | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile(): boolean; | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory?: boolean): void; | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist(): void; | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist(): void; | ||
| } | ||
| declare class FlatCacheDefault { | ||
| static create: typeof create; | ||
| static createFromFile: typeof createFromFile; | ||
| static clearCacheById: typeof clearCacheById; | ||
| static clearAll: typeof clearAll; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. | ||
| * | ||
| * @method create | ||
| * @param docId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| declare function create(options?: FlatCacheOptions): FlatCache; | ||
| /** | ||
| * Load a cache from the provided file | ||
| * @method createFromFile | ||
| * @param {String} filePath the path to the file containing the info for the cache | ||
| * @param options {FlatCacheOptions} options for the cache | ||
| * @returns {cache} cache instance | ||
| */ | ||
| declare function createFromFile(filePath: string, options?: FlatCacheOptions): FlatCache; | ||
| /** | ||
| * Clear the cache identified by the given Id. This will only remove the cache from disk. | ||
| * @method clearCacheById | ||
| * @param cacheId {String} the id of the cache | ||
| * @param cacheDirectory {String} directory for the cache entry | ||
| */ | ||
| declare function clearCacheById(cacheId: string, cacheDirectory?: string): void; | ||
| /** | ||
| * Clear the cache directory | ||
| * @method clearAll | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| declare function clearAll(cacheDirectory?: string): void; | ||
| export { FlatCache, FlatCacheEvents, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile, FlatCacheDefault as default }; |
-455
| // src/index.ts | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { CacheableMemory } from "cacheable"; | ||
| import { parse, stringify } from "flatted"; | ||
| import { Hookified } from "hookified"; | ||
| var FlatCacheEvents = /* @__PURE__ */ ((FlatCacheEvents2) => { | ||
| FlatCacheEvents2["SAVE"] = "save"; | ||
| FlatCacheEvents2["LOAD"] = "load"; | ||
| FlatCacheEvents2["DELETE"] = "delete"; | ||
| FlatCacheEvents2["CLEAR"] = "clear"; | ||
| FlatCacheEvents2["DESTROY"] = "destroy"; | ||
| FlatCacheEvents2["ERROR"] = "error"; | ||
| FlatCacheEvents2["EXPIRED"] = "expired"; | ||
| return FlatCacheEvents2; | ||
| })(FlatCacheEvents || {}); | ||
| var FlatCache = class extends Hookified { | ||
| _cache = new CacheableMemory(); | ||
| _cacheDir = ".cache"; | ||
| _cacheId = "cache1"; | ||
| _persistInterval = 0; | ||
| _persistTimer; | ||
| _changesSinceLastSave = false; | ||
| _parse = parse; | ||
| _stringify = stringify; | ||
| constructor(options) { | ||
| super(); | ||
| if (options) { | ||
| this._cache = new CacheableMemory({ | ||
| ttl: options.ttl, | ||
| useClone: options.useClone, | ||
| lruSize: options.lruSize, | ||
| checkInterval: options.expirationInterval | ||
| }); | ||
| } | ||
| if (options?.cacheDir) { | ||
| this._cacheDir = options.cacheDir; | ||
| } | ||
| if (options?.cacheId) { | ||
| this._cacheId = options.cacheId; | ||
| } | ||
| if (options?.persistInterval) { | ||
| this._persistInterval = options.persistInterval; | ||
| this.startAutoPersist(); | ||
| } | ||
| if (options?.deserialize) { | ||
| this._parse = options.deserialize; | ||
| } | ||
| if (options?.serialize) { | ||
| this._stringify = options.serialize; | ||
| } | ||
| } | ||
| /** | ||
| * The cache object | ||
| * @property cache | ||
| * @type {CacheableMemory} | ||
| */ | ||
| get cache() { | ||
| return this._cache; | ||
| } | ||
| /** | ||
| * The cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| get cacheDir() { | ||
| return this._cacheDir; | ||
| } | ||
| /** | ||
| * Set the cache directory | ||
| * @property cacheDir | ||
| * @type {String} | ||
| * @default '.cache' | ||
| */ | ||
| set cacheDir(value) { | ||
| this._cacheDir = value; | ||
| } | ||
| /** | ||
| * The cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| get cacheId() { | ||
| return this._cacheId; | ||
| } | ||
| /** | ||
| * Set the cache id | ||
| * @property cacheId | ||
| * @type {String} | ||
| * @default 'cache1' | ||
| */ | ||
| set cacheId(value) { | ||
| this._cacheId = value; | ||
| } | ||
| /** | ||
| * The flag to indicate if there are changes since the last save | ||
| * @property changesSinceLastSave | ||
| * @type {Boolean} | ||
| * @default false | ||
| */ | ||
| get changesSinceLastSave() { | ||
| return this._changesSinceLastSave; | ||
| } | ||
| /** | ||
| * The interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| get persistInterval() { | ||
| return this._persistInterval; | ||
| } | ||
| /** | ||
| * Set the interval to persist the cache to disk. 0 means no timed persistence | ||
| * @property persistInterval | ||
| * @type {Number} | ||
| * @default 0 | ||
| */ | ||
| set persistInterval(value) { | ||
| this._persistInterval = value; | ||
| } | ||
| /** | ||
| * Load a cache identified by the given Id. If the element does not exists, then initialize an empty | ||
| * cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted | ||
| * then the cache module directory `.cacheDir` will be used instead | ||
| * | ||
| * @method load | ||
| * @param cacheId {String} the id of the cache, would also be used as the name of the file cache | ||
| * @param cacheDir {String} directory for the cache entry | ||
| */ | ||
| load(cacheId, cacheDir) { | ||
| try { | ||
| const filePath = path.resolve( | ||
| `${cacheDir ?? this._cacheDir}/${cacheId ?? this._cacheId}` | ||
| ); | ||
| this.loadFile(filePath); | ||
| this.emit("load" /* LOAD */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Load the cache from the provided file | ||
| * @method loadFile | ||
| * @param {String} pathToFile the path to the file containing the info for the cache | ||
| */ | ||
| loadFile(pathToFile) { | ||
| if (fs.existsSync(pathToFile)) { | ||
| const data = fs.readFileSync(pathToFile, "utf8"); | ||
| const items = this._parse(data); | ||
| if (Array.isArray(items)) { | ||
| for (const item of items) { | ||
| if (item && typeof item === "object" && "key" in item) { | ||
| if (item.expires) { | ||
| this._cache.set(item.key, item.value, { expire: item.expires }); | ||
| } else if (item.timestamp) { | ||
| this._cache.set(item.key, item.value, { expire: item.timestamp }); | ||
| } else { | ||
| this._cache.set(item.key, item.value); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| for (const key of Object.keys(items)) { | ||
| const item = items[key]; | ||
| if (item && typeof item === "object" && "key" in item) { | ||
| this._cache.set(item.key, item.value, { | ||
| expire: item.expires | ||
| }); | ||
| } else { | ||
| if (item && typeof item === "object" && item.timestamp) { | ||
| this._cache.set(key, item, { expire: item.timestamp }); | ||
| } else { | ||
| this._cache.set(key, item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| } | ||
| loadFileStream(pathToFile, onProgress, onEnd, onError) { | ||
| if (fs.existsSync(pathToFile)) { | ||
| const stats = fs.statSync(pathToFile); | ||
| const total = stats.size; | ||
| let loaded = 0; | ||
| let streamData = ""; | ||
| const readStream = fs.createReadStream(pathToFile, { encoding: "utf8" }); | ||
| readStream.on("data", (chunk) => { | ||
| loaded += chunk.length; | ||
| streamData += chunk; | ||
| onProgress(loaded, total); | ||
| }); | ||
| readStream.on("end", () => { | ||
| const items = this._parse(streamData); | ||
| for (const key of Object.keys(items)) { | ||
| this._cache.set(items[key].key, items[key].value, { | ||
| expire: items[key].expires | ||
| }); | ||
| } | ||
| this._changesSinceLastSave = true; | ||
| onEnd(); | ||
| }); | ||
| readStream.on("error", (error) => { | ||
| this.emit("error" /* ERROR */, error); | ||
| if (onError) { | ||
| onError(error); | ||
| } | ||
| }); | ||
| } else { | ||
| const error = new Error(`Cache file ${pathToFile} does not exist`); | ||
| this.emit("error" /* ERROR */, error); | ||
| if (onError) { | ||
| onError(error); | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Returns the entire persisted object | ||
| * @method all | ||
| * @returns {*} | ||
| */ | ||
| all() { | ||
| const result = {}; | ||
| const items = [...this._cache.items]; | ||
| for (const item of items) { | ||
| result[item.key] = item.value; | ||
| } | ||
| return result; | ||
| } | ||
| /** | ||
| * Returns an array with all the items in the cache { key, value, expires } | ||
| * @method items | ||
| * @returns {Array} | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: cache items can store any value | ||
| get items() { | ||
| return [...this._cache.items]; | ||
| } | ||
| /** | ||
| * Returns the path to the file where the cache is persisted | ||
| * @method cacheFilePath | ||
| * @returns {String} | ||
| */ | ||
| get cacheFilePath() { | ||
| return path.resolve(`${this._cacheDir}/${this._cacheId}`); | ||
| } | ||
| /** | ||
| * Returns the path to the cache directory | ||
| * @method cacheDirPath | ||
| * @returns {String} | ||
| */ | ||
| get cacheDirPath() { | ||
| return path.resolve(this._cacheDir); | ||
| } | ||
| /** | ||
| * Returns an array with all the keys in the cache | ||
| * @method keys | ||
| * @returns {Array} | ||
| */ | ||
| keys() { | ||
| return [...this._cache.keys]; | ||
| } | ||
| /** | ||
| * (Legacy) set key method. This method will be deprecated in the future | ||
| * @method setKey | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: type format | ||
| setKey(key, value, ttl) { | ||
| this.set(key, value, ttl); | ||
| } | ||
| /** | ||
| * Sets a key to a given value | ||
| * @method set | ||
| * @param key {string} the key to set | ||
| * @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify | ||
| * @param [ttl] {number} the time to live in milliseconds | ||
| */ | ||
| // biome-ignore lint/suspicious/noExplicitAny: type format | ||
| set(key, value, ttl) { | ||
| this._cache.set(key, value, ttl); | ||
| this._changesSinceLastSave = true; | ||
| } | ||
| /** | ||
| * (Legacy) Remove a given key from the cache. This method will be deprecated in the future | ||
| * @method removeKey | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| removeKey(key) { | ||
| this.delete(key); | ||
| } | ||
| /** | ||
| * Remove a given key from the cache | ||
| * @method delete | ||
| * @param key {String} the key to remove from the object | ||
| */ | ||
| delete(key) { | ||
| this._cache.delete(key); | ||
| this._changesSinceLastSave = true; | ||
| this.emit("delete" /* DELETE */, key); | ||
| } | ||
| /** | ||
| * (Legacy) Return the value of the provided key. This method will be deprecated in the future | ||
| * @method getKey<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| getKey(key) { | ||
| return this.get(key); | ||
| } | ||
| /** | ||
| * Return the value of the provided key | ||
| * @method get<T> | ||
| * @param key {String} the name of the key to retrieve | ||
| * @returns {*} at T the value from the key | ||
| */ | ||
| get(key) { | ||
| return this._cache.get(key); | ||
| } | ||
| /** | ||
| * Clear the cache and save the state to disk | ||
| * @method clear | ||
| */ | ||
| clear() { | ||
| try { | ||
| this._cache.clear(); | ||
| this._changesSinceLastSave = true; | ||
| this.save(); | ||
| this.emit("clear" /* CLEAR */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Save the state of the cache identified by the docId to disk | ||
| * as a JSON structure | ||
| * @method save | ||
| */ | ||
| save(force = false) { | ||
| try { | ||
| if (this._changesSinceLastSave || force) { | ||
| const filePath = this.cacheFilePath; | ||
| const items = [...this._cache.items]; | ||
| const data = this._stringify(items); | ||
| if (!fs.existsSync(this._cacheDir)) { | ||
| fs.mkdirSync(this._cacheDir, { recursive: true }); | ||
| } | ||
| fs.writeFileSync(filePath, data); | ||
| this._changesSinceLastSave = false; | ||
| this.emit("save" /* SAVE */); | ||
| } | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Remove the file where the cache is persisted | ||
| * @method removeCacheFile | ||
| * @return {Boolean} true or false if the file was successfully deleted | ||
| */ | ||
| removeCacheFile() { | ||
| try { | ||
| if (fs.existsSync(this.cacheFilePath)) { | ||
| fs.rmSync(this.cacheFilePath); | ||
| return true; | ||
| } | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| return false; | ||
| } | ||
| /** | ||
| * Destroy the cache. This will remove the directory, file, and memory cache | ||
| * @method destroy | ||
| * @param [includeCacheDir=false] {Boolean} if true, the cache directory will be removed | ||
| * @return {undefined} | ||
| */ | ||
| destroy(includeCacheDirectory = false) { | ||
| try { | ||
| this._cache.clear(); | ||
| this.stopAutoPersist(); | ||
| if (includeCacheDirectory) { | ||
| fs.rmSync(this.cacheDirPath, { recursive: true, force: true }); | ||
| } else { | ||
| fs.rmSync(this.cacheFilePath, { recursive: true, force: true }); | ||
| } | ||
| this._changesSinceLastSave = false; | ||
| this.emit("destroy" /* DESTROY */); | ||
| } catch (error) { | ||
| this.emit("error" /* ERROR */, error); | ||
| } | ||
| } | ||
| /** | ||
| * Start the auto persist interval | ||
| * @method startAutoPersist | ||
| */ | ||
| startAutoPersist() { | ||
| if (this._persistInterval > 0) { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| this._persistTimer = setInterval(() => { | ||
| this.save(); | ||
| }, this._persistInterval); | ||
| } | ||
| } | ||
| /** | ||
| * Stop the auto persist interval | ||
| * @method stopAutoPersist | ||
| */ | ||
| stopAutoPersist() { | ||
| if (this._persistTimer) { | ||
| clearInterval(this._persistTimer); | ||
| this._persistTimer = void 0; | ||
| } | ||
| } | ||
| }; | ||
| var FlatCacheDefault = class { | ||
| static create = create; | ||
| static createFromFile = createFromFile; | ||
| static clearCacheById = clearCacheById; | ||
| static clearAll = clearAll; | ||
| }; | ||
| function create(options) { | ||
| const cache = new FlatCache(options); | ||
| cache.load(); | ||
| return cache; | ||
| } | ||
| function createFromFile(filePath, options) { | ||
| const cache = new FlatCache(options); | ||
| cache.loadFile(filePath); | ||
| return cache; | ||
| } | ||
| function clearCacheById(cacheId, cacheDirectory) { | ||
| const cache = new FlatCache({ cacheId, cacheDir: cacheDirectory }); | ||
| cache.destroy(); | ||
| } | ||
| function clearAll(cacheDirectory) { | ||
| fs.rmSync(cacheDirectory ?? ".cache", { recursive: true, force: true }); | ||
| } | ||
| export { | ||
| FlatCache, | ||
| FlatCacheEvents, | ||
| clearAll, | ||
| clearCacheById, | ||
| create, | ||
| createFromFile, | ||
| FlatCacheDefault as default | ||
| }; | ||
| /* v8 ignore next -- @preserve */ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
56568
-3.31%936
-22.13%1
Infinity%Updated