https://github.com/danielwpz/cacheasy
Cache library for nodejs
https://github.com/danielwpz/cacheasy
Last synced: 3 months ago
JSON representation
Cache library for nodejs
- Host: GitHub
- URL: https://github.com/danielwpz/cacheasy
- Owner: danielwpz
- Created: 2019-09-12T08:28:18.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-24T20:09:00.000Z (over 3 years ago)
- Last Synced: 2025-02-16T01:13:33.758Z (5 months ago)
- Language: JavaScript
- Size: 86.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cacheasy
Multi-backend cache library for nodejs## API
1. `CacheStore`
- `new CacheStore(store)`
Create a new cache store with given backend.- `CacheStore.cache(key, generator, options)`
Get the cached value for key. If cache missed, generator will be run and the returned value will be cached.
generator could either be a `async function` which returns a value, or a value it self.
`options.ttl`: TTL- `CacheStore.set(key, generator, options)`
Simple set the key with generator.
`options.ttl`: TTL- `CacheStore.get(key)`
Get cached value for key.2. `MemoryCacheStore`
- `new MemoryCacheStore()`
Create new memory based cache store.3. `RedisCacheStore`
- `new RedisCacheStore(redisOptions)`
Create new redis based cache store.
options: [ioredis](https://github.com/luin/ioredis/blob/master/API.md) options## Example
```javascript
const Cacheasy = require('../index')
const { RedisCacheStore } = Cacheasy.store
const assert = require('assert')const redisStore = new RedisCacheStore()
const cache = new Cacheasy(redisStore)async function test () {
return cache.cache('foo', async () => 'bar')
}async function fail () {
return cache.cache('boom', () => {
throw new Error('something happened')
})
}test()
.then(test) // cache hit for this time
.then(value => assert(value === 'bar'))
.then(fail) // exceptions will not be cached
.catch(err => {
assert(err.message === 'something happened')
})
```