https://github.com/kaelzhang/cacheman-redis
Redis standalone caching library for Node.JS and also cache engine for cacheman
https://github.com/kaelzhang/cacheman-redis
cacheman cacheman-engine nodejs
Last synced: 5 months ago
JSON representation
Redis standalone caching library for Node.JS and also cache engine for cacheman
- Host: GitHub
- URL: https://github.com/kaelzhang/cacheman-redis
- Owner: kaelzhang
- License: other
- Created: 2019-03-22T02:48:10.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-22T02:48:27.000Z (almost 7 years ago)
- Last Synced: 2025-03-25T13:16:15.248Z (12 months ago)
- Topics: cacheman, cacheman-engine, nodejs
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/kaelzhang/cacheman-redis)
# @ostai/cacheman-redis
Redis standalone caching library for Node.JS and also cache engine for [cacheman](https://github.com/cayasso/cacheman).
This fork removes peer dependencies `redis` and only supports to create a `CachemanRedis` with a redis client
## Install
``` bash
$ npm i @ostai/cacheman-redis
```
## Usage
```js
const CachemanRedis = require('@ostai/cacheman-redis')
const Redis = require('ioredis')
const cache = new CachemanRedis(new Redis())
// set the value
cache.set('key', {foo: 'bar'}, err => {
if (err) {
throw err
}
console.log('succeeded')
})
// But for most cases,
// CachemanRedis is used as an engine of Cacheman
const Cacheman = require('cacheman')
const man = new Cacheman('prefix', {
engine: cache
})
const value = await man.get('key')
```
## API
### CachemanRedis(client, options?)
- **client** `RedisClient` redis `client` instance
- **options** `?Object`
- **prefix** `?string=''` key prefix
Create a `CachemanRedis` instance.
```js
const Redis = require('ioredis')
const cache = new CachemanRedis(new Redis())
```
### cache.set(key, value, ttl?, callback?)
Stores or updates a value.
```javascript
cache.set('foo', { a: 'bar' }, function (err, value) {
if (err) throw err
console.log(value) //-> {a:'bar'}
})
```
Or add a TTL(Time To Live) in seconds like this:
```javascript
// key will expire in 60 seconds
cache.set('foo', {a: 'bar'}, 60, (err, value) => {
if (err) throw err
console.log(value) //-> {a:'bar'}
})
```
### cache.get(key, callback?)
Retrieves a value for a given key, if there is no value for the given key a null value will be returned.
```js
cache.get(key, (err, value) => {
if (err) throw err
console.log(value)
})
```
### cache.del(key, callback?)
Deletes a key out of the cache.
```javascript
cache.del('foo', err => {
if (err) throw err
// foo was deleted
})
```
### cache.clear(callback?)
Clear the cache entirely, throwing away all values.
```javascript
cache.clear(err => {
if (err) throw err
// cache is now clear
})
```
## Run tests
``` bash
$ make test
```
## License
[MIT](LICENSE)