Coder Social home page Coder Social logo

sequelize-redis's Introduction

sequelize-redis

Build Status codecov.io Code Coverage Known Vulnerabilities

A semi-automatic caching wrapper for Sequelize v4 NodeJS framework

Installation


npm install sequelize-redis

requirements

Usage

  1. Init our Sequelize cache manager:
import SequelizeRedis from 'sequelize-redis';
import redis from 'redis';
import bluebird from 'bluebird';

// Let's promisify Redis
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);

// Define your redisClient
const redisClient = redis.createClient({ /* Redis configuration comes here */ });

// Let's start
const sequelizeRedis = new SequelizeRedis(redisClient);
  1. Wrap our the Sequelize original model:
// models.User refers to model of sequelize
const User = sequelizeRedis.getModel(models.User, { ttl: 60 * 60 * 24 });

The second argument of getModel is optional:

Key Description Default value
ttl Defines cache TTL (seconds) null
  1. Then we can start use the model wrapper:
const userUUID = '75292c75-4c7a-4a11-92ac-57f929f50e23';
const userCacheKey = `user_${userUUID}`;
// We can use the default sequelize methods by adding suffix of "Cached" 
// for example, findByIdCached: 
const [user, cacheHit] = await User.findByIdCached(userCacheKey, userUUID);
// We can also use the non cached methods (original methods)
const user = await User.findById(userUUID);

Results of Cached methods (for ex. findByIdCached) will be array with following arguments:

  1. Sequelize response (same as on original method)
  2. Cache hit indication (true / false)

Supported Methods: find findOne findAll findAndCount findAndCountAll findById all min max sum count

Cache Invalidation

Just use regular Redis API:

redisClient.del('SampleKey');

Contribution

Feel free to contribute and submit issues.

PR

Please make sure that your code is linted and getting build successfully

Thanks

Inspired by rfink/sequelize-redis-cache/

License

MIT (Idan Gozlan)

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.