https://github.com/jonathanong/redis-cache-fn
cache a function via redis
https://github.com/jonathanong/redis-cache-fn
Last synced: over 1 year ago
JSON representation
cache a function via redis
- Host: GitHub
- URL: https://github.com/jonathanong/redis-cache-fn
- Owner: jonathanong
- License: mit
- Created: 2016-11-01T02:21:02.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T06:49:58.000Z (over 3 years ago)
- Last Synced: 2024-04-15T02:12:02.059Z (over 2 years ago)
- Language: JavaScript
- Size: 666 KB
- Stars: 5
- Watchers: 1
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-cache-fn
[](https://github.com/jonathanong/redis-cache-fn/actions/workflows/node.js.yml)
[](https://codecov.io/gh/jonathanong/redis-cache-fn)
Caches your functions via Redis.
Features:
- Uses Redis caching, expiration, and pub/sub.
- Concurrency locking - if the function is being run elsewhere with the same arguments, it will wait for the result of that function instead of executing again.
- Caching - caches results for as long as you want. If you set `ttl=0`, then you're just this library for concurrency locking, which is completely fine. However, please keep in mind that locking mechanism in this module is not robust - it is more suited towards caching.
- Only tested with [ioredis](https://github.com/luin/ioredis)
Use Cases:
- Race conditions
- API calls with rate limits
- Expensive database calls
- Expensive function calls
Differences from [redis-cache-decorator](https://github.com/jonathanong/redis-cache-decorator):
- Uses a class system so that it's more maintainable
- A slightly different API
- Doesn't bother throwing when functions timeout. Use another library instead.
- Removes stream support
## API
### const Cache = require('redis-cache-fn')(options)
- `client ` - a `ioredis` client for GET/SET/PUBLISH, etc.
- `subscriber ` - a `ioredis` client for `PSUBSCRIBE`
- `namespace = ''` - a prefix for all the events
- `encoding = 'json'` - how data is encoded between redis and node.js.
Supported values are:
- `json` - the default
- `string`
- `buffer`
- `ttl = '30s'` - the TTL expiration in seconds.
- `timeout = 0` - how long to wait for the function to execute before executing the function again. By default, there is no timeout.
- Ex. if it the current function has waited 30s for the function to complete elsewhere, it will say "F IT" and run the function itself.
- `pollInterval = '1s'` - how often to poll for new values.
- `precache = 3/4` - when the age hits this threshold, execute the function again to so that the cache remains fresh.
- `onError = err => console.error(err.stack)` - an error handler for redis network errors.
### Cache = Cache.extend(options\)
Subclasses the cache, extending its options.
All options are overwritten except:
- `namespace` - the namespace is simply concatenated with `:`s.
### fn = Cache.wrap(function\)
Decorates the function so that it hits the cache first.
### { Cache } = fn
The function's Cache constructor.
### promise = fn()
Returns a promise.
### promise.cache
The cache instance of this function call.
### const hash = Cache.createHash(args)
### const cache = new Cache(args)
### value = await cache.set(value)