https://github.com/reaxi/cache-eight
file caching with v8 serialization strategy
https://github.com/reaxi/cache-eight
cache file hacktoberfest json node-js nodejs v8
Last synced: about 2 months ago
JSON representation
file caching with v8 serialization strategy
- Host: GitHub
- URL: https://github.com/reaxi/cache-eight
- Owner: reaxi
- License: mit
- Created: 2022-03-01T14:44:02.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-01T05:57:31.000Z (over 2 years ago)
- Last Synced: 2025-03-08T19:05:20.551Z (2 months ago)
- Topics: cache, file, hacktoberfest, json, node-js, nodejs, v8
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# cache-eight
> **cache** + v**8**
- checksum keys
- access data from json files (persistent cache)
- optimize functions execution time for expensive operations
- data is saved using v8's serialize / deserialize
## why?
caching expensive pure functions results
same inputs -> same results
### use case
from 3s to 1s
> @todo
## Usage
```ts
import Cache from 'cache-eight';const cache = new Cache();
``````
folder file
> OS_temp_dir/.cache8/appName -> cache.json
```#### high level api
```ts
const values = [1, 2, 3];
const key = values.toString();const cached = await cache.get(key, () => values.map(value => value * 2));
// will return the cached if valid or the actual value (and serialize for the future)
```custom options:
```ts
const customPath = path.resolve(process.cwd());//custom folder:
const customCache = new Cache(customPath);//custom file:
customCache.get(k, () => v, { filePath: 'my-file.json' });// cache duration (time to live):
customCache.get(k, () => v, { ttl: 1000 * 60 }); // number in ms;
```#### granular control api
```ts
const values = [1, 2, 3];
const key = values.toString();async function get() {
const cached = await cache.deserialize(key);// if has cached value, return early (skipping further functions)
if (cached) return cached;//else get it from somewhere
const result = values.map(value => value * 2);//cache the result
await cache.serialize(key, result);return result;
}await get();
```## API
```ts
get(key, () => val, options?) => val
serialize(key, val, options?) => val
deserialize(key, options?) => cachedVal | false// key and val are any valid Json objects
options = {
filPath: string + .json //file.json
ttl: number //ms
}```