Giter VIP home page Giter VIP logo

cacheman-redis's Introduction

Build Status

@ostai/cacheman-redis

Redis standalone caching library for Node.JS and also cache engine for cacheman.

This fork removes peer dependencies redis and only supports to create a CachemanRedis with a redis client

Install

$ npm i @ostai/cacheman-redis

Usage

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.

const Redis = require('ioredis')
const cache = new CachemanRedis(new Redis())

cache.set(key, value, ttl?, callback?)

Stores or updates a value.

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:

// 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.

cache.get(key, (err, value) => {
  if (err) throw err
  console.log(value)
})

cache.del(key, callback?)

Deletes a key out of the cache.

cache.del('foo', err => {
  if (err) throw err
  // foo was deleted
})

cache.clear(callback?)

Clear the cache entirely, throwing away all values.

cache.clear(err => {
  if (err) throw err
  // cache is now clear
})

Run tests

$ make test

License

MIT

cacheman-redis's People

Contributors

appleboy avatar boblauer avatar cayasso avatar kaelzhang avatar khmm12 avatar tjulien avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.