https://github.com/uwdata/file-cache
File-based cache for JSON-serializable data.
https://github.com/uwdata/file-cache
Last synced: 2 months ago
JSON representation
File-based cache for JSON-serializable data.
- Host: GitHub
- URL: https://github.com/uwdata/file-cache
- Owner: uwdata
- License: bsd-3-clause
- Created: 2022-09-19T22:33:12.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-20T20:05:57.000Z (almost 3 years ago)
- Last Synced: 2025-04-19T00:11:17.504Z (2 months ago)
- Language: JavaScript
- Size: 37.1 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-cache
File-based cache for JSON-serializable data.
## Install
Requires at least Node.js v14.14.0.
```
npm install @uwdata/file-cache
```## Usage
`@uwdata/file-cache` is an ESM-only module - you are not able to import it with `require()`.
### Standard Usage
```js
import { fileCache, deleteCache } from '@uwdata/file-cache';// create a new cache, writes to '.cache' in current working dir
// the cache directory will be created if it does not exist
const cache = await fileCache();// set cache value writes both to in-memory map and to disk
await cache.set('key', { value: true });// get cache value reads from disk if not found in-memory
const value = await cache.get('key');
// value = { value: true}// delete cache value from both in-memory map and disk
await cache.delete('key');// delete cache values and folder from disk
// subsequent use of existing cache instance is ill-advised
await deleteCache();
```### Custom Usage
```js
import { fileCache, deleteCache } from '@uwdata/file-cache';// create a new cache with custom directory and time-to-live
const cache = await fileCache({
cacheDir: '.my-cache-dir', // custom cache directory
defaultTTL: 60 * 1000, // default time-to-live before expiration (ms)
});// set cache value along with a custom TTL value for the entry
await cache.set('key', { value: true }, 120 * 1000);// delete cache values and folder from disk
// subsequent use of existing cache instance is ill-advised
await deleteCache('.my-cache-dir');
```