flat-cache
Advanced tools
Comparing version 6.0.0 to 6.1.0
import { CacheableMemory } from 'cacheable'; | ||
import { Hookified } from 'hookified'; | ||
@@ -11,4 +12,15 @@ type FlatCacheOptions = { | ||
cacheId?: string; | ||
parse?: (data: string) => any; | ||
stringify?: (data: any) => string; | ||
}; | ||
declare class FlatCache { | ||
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; | ||
@@ -19,2 +31,5 @@ private _cacheDir; | ||
private _persistTimer; | ||
private _changesSinceLastSave; | ||
private readonly _parse; | ||
private readonly _stringify; | ||
constructor(options?: FlatCacheOptions); | ||
@@ -56,2 +71,9 @@ /** | ||
/** | ||
* 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 | ||
@@ -76,6 +98,6 @@ * @property persistInterval | ||
* @method load | ||
* @param docId {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 | ||
* @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(documentId: string, cacheDir?: string): void; | ||
load(cacheId?: string, cacheDir?: string): void; | ||
/** | ||
@@ -163,3 +185,3 @@ * Load the cache from the provided file | ||
/** | ||
* Clear the cache | ||
* Clear the cache and save the state to disk | ||
* @method clear | ||
@@ -173,3 +195,3 @@ */ | ||
*/ | ||
save(): void; | ||
save(force?: boolean): void; | ||
/** | ||
@@ -209,3 +231,3 @@ * Remove the file where the cache is persisted | ||
*/ | ||
declare function create(documentId: string, cacheDirectory?: string, options?: FlatCacheOptions): FlatCache; | ||
declare function create(options?: FlatCacheOptions): FlatCache; | ||
/** | ||
@@ -233,2 +255,2 @@ * Load a cache from the provided file | ||
export { FlatCache, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile }; | ||
export { FlatCache, FlatCacheEvents, type FlatCacheOptions, clearAll, clearCacheById, create, createFromFile }; |
@@ -6,3 +6,14 @@ // src/index.ts | ||
import { parse, stringify } from "flatted"; | ||
var FlatCache = class { | ||
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(); | ||
@@ -13,3 +24,7 @@ _cacheDir = ".cache"; | ||
_persistTimer; | ||
_changesSinceLastSave = false; | ||
_parse = parse; | ||
_stringify = stringify; | ||
constructor(options) { | ||
super(); | ||
if (options) { | ||
@@ -33,2 +48,8 @@ this._cache = new CacheableMemory({ | ||
} | ||
if (options?.parse) { | ||
this._parse = options.parse; | ||
} | ||
if (options?.stringify) { | ||
this._stringify = options.stringify; | ||
} | ||
} | ||
@@ -80,2 +101,11 @@ /** | ||
/** | ||
* 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 | ||
@@ -104,9 +134,14 @@ * @property persistInterval | ||
* @method load | ||
* @param docId {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 | ||
* @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 | ||
*/ | ||
// eslint-disable-next-line unicorn/prevent-abbreviations | ||
load(documentId, cacheDir) { | ||
const filePath = path.resolve(`${cacheDir ?? this._cacheDir}/${documentId}`); | ||
this.loadFile(filePath); | ||
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); | ||
} | ||
} | ||
@@ -121,6 +156,7 @@ /** | ||
const data = fs.readFileSync(pathToFile, "utf8"); | ||
const items = parse(data); | ||
const items = this._parse(data); | ||
for (const key of Object.keys(items)) { | ||
this._cache.set(key, items[key]); | ||
} | ||
this._changesSinceLastSave = true; | ||
} | ||
@@ -180,3 +216,3 @@ } | ||
setKey(key, value, ttl) { | ||
this._cache.set(key, value, ttl); | ||
this.set(key, value, ttl); | ||
} | ||
@@ -192,2 +228,3 @@ /** | ||
this._cache.set(key, value, ttl); | ||
this._changesSinceLastSave = true; | ||
} | ||
@@ -200,3 +237,3 @@ /** | ||
removeKey(key) { | ||
this._cache.delete(key); | ||
this.delete(key); | ||
} | ||
@@ -210,2 +247,4 @@ /** | ||
this._cache.delete(key); | ||
this._changesSinceLastSave = true; | ||
this.emit("delete" /* DELETE */, key); | ||
} | ||
@@ -231,7 +270,14 @@ /** | ||
/** | ||
* Clear the cache | ||
* Clear the cache and save the state to disk | ||
* @method clear | ||
*/ | ||
clear() { | ||
this._cache.clear(); | ||
try { | ||
this._cache.clear(); | ||
this._changesSinceLastSave = true; | ||
this.save(); | ||
this.emit("clear" /* CLEAR */); | ||
} catch (error) { | ||
this.emit("error" /* ERROR */, error); | ||
} | ||
} | ||
@@ -243,10 +289,18 @@ /** | ||
*/ | ||
save() { | ||
const filePath = this.cacheFilePath; | ||
const items = this.all(); | ||
const data = stringify(items); | ||
if (!fs.existsSync(this._cacheDir)) { | ||
fs.mkdirSync(this._cacheDir, { recursive: true }); | ||
save(force = false) { | ||
try { | ||
if (this._changesSinceLastSave || force) { | ||
const filePath = this.cacheFilePath; | ||
const items = this.all(); | ||
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); | ||
} | ||
fs.writeFileSync(filePath, data); | ||
} | ||
@@ -259,5 +313,9 @@ /** | ||
removeCacheFile() { | ||
if (fs.existsSync(this.cacheFilePath)) { | ||
fs.rmSync(this.cacheFilePath); | ||
return true; | ||
try { | ||
if (fs.existsSync(this.cacheFilePath)) { | ||
fs.rmSync(this.cacheFilePath); | ||
return true; | ||
} | ||
} catch (error) { | ||
this.emit("error" /* ERROR */, error); | ||
} | ||
@@ -273,8 +331,14 @@ return false; | ||
destroy(includeCacheDirectory = false) { | ||
this._cache.clear(); | ||
this.stopAutoPersist(); | ||
if (includeCacheDirectory) { | ||
fs.rmSync(this.cacheDirPath, { recursive: true, force: true }); | ||
} else { | ||
fs.rmSync(this.cacheFilePath, { recursive: true, force: true }); | ||
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); | ||
} | ||
@@ -308,9 +372,5 @@ } | ||
}; | ||
function create(documentId, cacheDirectory, options) { | ||
function create(options) { | ||
const cache = new FlatCache(options); | ||
cache.cacheId = documentId; | ||
if (cacheDirectory) { | ||
cache.cacheDir = cacheDirectory; | ||
} | ||
cache.load(documentId, cacheDirectory); | ||
cache.load(); | ||
return cache; | ||
@@ -332,2 +392,3 @@ } | ||
FlatCache, | ||
FlatCacheEvents, | ||
clearAll, | ||
@@ -334,0 +395,0 @@ clearCacheById, |
{ | ||
"name": "flat-cache", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "A simple key/value storage using files to persist the data", | ||
@@ -40,3 +40,4 @@ "type": "module", | ||
"cacheable": "^1.7.1", | ||
"flatted": "^3.3.1" | ||
"flatted": "^3.3.1", | ||
"hookified": "^1.1.0" | ||
}, | ||
@@ -43,0 +44,0 @@ "files": [ |
130
README.md
@@ -16,7 +16,19 @@ [<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable) | ||
- Automatically saves the data to disk via `persistInterval` setting. Off By Default | ||
- Easily Loads the data from disk and into memory | ||
- Easily Loads the data from disk and into memory with `load` or `loadFile` | ||
- Uses `ttl` and `lruSize` to manage the cache and persist the data | ||
- Only saves the data to disk if the data has changed even when using `persistInterval` or calling `save()` | ||
- Uses `flatted` to parse and stringify the data by default but can be overridden | ||
- Uses `flatted` to parse and stringify the data by default but can be overridden using `parse` and `stringify` in options | ||
# Table of Contents | ||
- [Installation](#installation) | ||
- [Getting Started](#getting-started) | ||
- [Breaking Changes from v5 to v6](#breaking-changes-from-v5-to-v6) | ||
- [Global Functions](#global-functions) | ||
- [FlatCache Options (FlatCacheOptions)](#flatcache-options-flatcacheoptions) | ||
- [API](#api) | ||
- [Events (FlatCacheEvents)](#events-flatcacheevents) | ||
- [Parse and Stringify for File Caching](#parse-and-stringify-for-file-caching) | ||
- [How to Contribute](#how-to-contribute) | ||
- [License and Copyright](#license-and-copyright) | ||
# Installation | ||
@@ -47,13 +59,67 @@ ```bash | ||
This will save the data to disk every 5 minutes and will remove any data that has not been accessed in 1 hour or if the cache has more than 10,000 items. | ||
This will save the data to disk every 5 minutes and will remove any data that has not been accessed in 1 hour or if the cache has more than 10,000 items. The `expirationInterval` will check every 5 minutes for expired items and evict them. This is replacement to the `save()` method with a `prune` option as it is no longer needed due to the fact that the in-memory cache handles pruning by `ttl` expiration or `lruSize` which will keep the most recent there. | ||
# FlatCache Options | ||
- `ttl` - The time to live for the cache in milliseconds. Default is `0` which means no expiration | ||
- `lruSize` - The number of items to keep in the cache. Default is `0` which means no limit | ||
- `useClone` - If `true` it will clone the data before returning it. Default is `false` | ||
- `expirationInterval` - The interval to check for expired items in the cache. Default is `0` which means no expiration | ||
- `persistInterval` - The interval to save the data to disk. Default is `0` which means no persistence | ||
- `cacheDir` - The directory to save the cache files. Default is `./cache` | ||
- `cacheId` - The id of the cache. Default is `cache1` | ||
here is an example doing load from already existing persisted cache | ||
```javascript | ||
import { load } from 'flat-cache'; | ||
const cache = load('cache1', './cacheAltDirectory'); | ||
``` | ||
This will load the cache from the `./cacheAltDirectory` directory with the `cache1` id. If it doesnt exist it will not throw an error but will just return an empty cache. | ||
# Breaking Changes from v5 to v6 | ||
There have been many features added and changes made to the `FlatCache` class. Here are the main changes: | ||
- `FlatCache` is now a class and not a function which you can create instances of or using legacy method `load`, `loadFile`, or `create` | ||
- `FlatCache` now uses `CacheableMemory` as the primary storage and then persists the data to disk | ||
- `FlatCache` now uses `ttl` and `lruSize` to manage the cache and persist the data | ||
- `FlatCache` now uses `expirationInterval` to check for expired items in the cache. If it is not set it will do a lazy check on `get` or `getKey` | ||
- `getKey` still exists but is now is an alias to `get` and will be removed in the future | ||
- `setKey` still exists but is now is an alias to `set` and will be removed in the future | ||
- `removeKey` still exists but is now is an alias to `delete` and will be removed in the future | ||
Here is an example of the legacy method `load`: | ||
```javascript | ||
const flatCache = require('flat-cache'); | ||
// loads the cache, if one does not exists for the given | ||
// Id a new one will be prepared to be created | ||
const cache = flatCache.load('cacheId'); | ||
``` | ||
Now you can use the `load` method and ES6 imports: | ||
```javascript | ||
import { FlatCache } from 'flat-cache'; | ||
const cache = new FlatCache(); | ||
cache.load('cacheId'); | ||
``` | ||
If you do not specify a `cacheId` it will default to what was set in `FlatCacheOptions` or the default property `cacheId` of `cache1` and default `cacheDir` of `./cache`. | ||
If you want to create a new cache and load from disk if it exists you can use the `create` method: | ||
```javascript | ||
import { create } from 'flat-cache'; | ||
const cache = create({ cacheId: 'myCacheId', cacheDir: './mycache', ttl: 60 * 60 * 1000 }); | ||
``` | ||
# Global Functions | ||
In version 6 we attempted to keep as much as the functionality as possible which includes these functions: | ||
- `create(options?: FlatCacheOptions)` - Creates a new cache and will load the data from disk if it exists | ||
- `createFromFile(filePath, options?: FlatCacheOptions)` - Creates a new cache from a file | ||
- `clearByCacheId(cacheId: string, cacheDir?: string)` - Clears the cache by the cacheId | ||
- `clearAll(cacheDirectory?: string)` - Clears all the caches | ||
# FlatCache Options (FlatCacheOptions) | ||
- `ttl?` - The time to live for the cache in milliseconds. Default is `0` which means no expiration | ||
- `lruSize?` - The number of items to keep in the cache. Default is `0` which means no limit | ||
- `useClone?` - If `true` it will clone the data before returning it. Default is `false` | ||
- `expirationInterval?` - The interval to check for expired items in the cache. Default is `0` which means no expiration | ||
- `persistInterval?` - The interval to save the data to disk. Default is `0` which means no persistence | ||
- `cacheDir?` - The directory to save the cache files. Default is `./cache` | ||
- `cacheId?` - The id of the cache. Default is `cache1` | ||
- `parse?` - The function to parse the data. Default is `flatted.parse` | ||
- `stringify?` - The function to stringify the data. Default is `flatted.stringify` | ||
# API | ||
@@ -67,2 +133,3 @@ | ||
- `persistInterval` - The interval to save the data to disk | ||
- `changesSinceLastSave` - If there have been changes since the last save | ||
- `load(cacheId: string, cacheDir?: string)` - Loads the data from disk | ||
@@ -80,5 +147,44 @@ - `loadFile(pathToFile: string)` - Loads the data from disk | ||
- `clear()` - Clears the cache | ||
- `save()` - Saves the data to disk | ||
- `save(force? boolean)` - Saves the data to disk. If `force` is `true` it will save even if `changesSinceLastSave` is `false` | ||
- `destroy()` - Destroys the cache and remove files | ||
# Events (FlatCacheEvents) | ||
Events have been added since v6 to allow for more control and visibility into the cache. Here are the events that are available: | ||
- `on(event: 'save', listener: () => void)` - Emitted when the cache is saved | ||
- `on(event: 'load', listener: () => void)` - Emitted when the cache is loaded | ||
- `on(event: 'delete', listener: (key: string) => void)` - Emitted when the cache is changed | ||
- `on(event: 'clear', listener: () => void)` - Emitted when the cache is cleared | ||
- `on(event: 'destroy', listener: () => void)` - Emitted when the cache is destroyed | ||
- `on(event: 'error', listener: (error: Error) => void)` - Emitted when there is an error | ||
Here is an example of how to use the `error` events: | ||
```javascript | ||
import { FlatCache, FlatCacheEvents } from 'flat-cache'; | ||
const cache = new FlatCache(); | ||
cache.on(FlatCacheEvents.error, (error) => { | ||
console.error(error); | ||
}); | ||
``` | ||
`FlatCacheEvents` is an enum that contains the event names for the `on` method. You do not have to use it but makes it easier to know what events are available. | ||
# Parse and Stringify for File Caching | ||
By default `flat-cache` uses `flatted` to parse and stringify the data. This is to allow for more complex data structures to be saved to disk. If you want to override this you can pass in your own `parse` and `stringify` functions. Here is an example: | ||
```javascript | ||
import { FlatCache } from 'flat-cache'; | ||
const cache = new FlatCache({ | ||
parse: JSON.parse, | ||
stringify: JSON.stringify, | ||
}); | ||
``` | ||
This will use `JSON.parse` and `JSON.stringify` to parse and stringify the data. This is useful if you want to use a different library or have a custom way of parsing and stringifying the data. | ||
**NOTE: This could cause issues if you are trying to load data that was saved with a different parser or stringifier.** | ||
# How to Contribute | ||
@@ -85,0 +191,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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.
Found 1 instance in 1 package
50249
1049
191
0
3
+ Addedhookified@^1.1.0