Git Product home page Git Product logo

sequelize-redis-cache's Introduction

sequelize-redis-cache Build Status

Small fluent interface for caching sequelize database query results in redis more easily. Simply put, this is a wrapper around sequelize retrieval methods that will automatically check in the configured redis instance for a value (based on a hash of the query and model name), then retrieve from the database and persist in redis if not found. It is promise based, so it will resemble sequelize for the most part, and be co/koa friendly.

Installation


npm install sequelize-redis-cache

Usage

var cacher = require('sequelize-redis-cache');
var redis = require('redis');
var Sequelize = require('sequelize');

var rc = redis.createClient(6379, 'localhost');
var db = new Sequelize('cache_tester', 'root', 'root', { dialect: 'mysql' });
var cacheObj = cacher(db, rc)
  .model('sequelize-model-name')
  .ttl(5);
cacheObj.find({ where: { id: 3 } })
  .then(function(row) {
    console.log(row); // sequelize db object
    console.log(cacheObj.cacheHit); // true or false
  });

You can also execute and cache raw queries:

var cacheObj = cacher(db, rc)
  .ttl(5);
cacheObj.query('SELECT * FROM widgets LIMIT 10')
  .then(function(row) {
    console.log(row); // Array of raw objects
  });

Check the tests out for more info, but it's pretty simple. The currently supported methods are:

query find findOne findAll findAndCountAll all min max sum

Notes

This library does not handle automatic invalidation of caches, since it currently does not handle inserts/updates/deletes/etc. I'd be in favor of someone submitting a patch to accommodate that, although I think that would be a significant undertaking.

Here is a good guideline for caching strategies: http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Strategies.html

License

MIT - Rekt

sequelize-redis-cache's People

Contributors

rfink avatar thujikun avatar jonathanleang avatar notjustclarkkent avatar naoto-ida avatar

Stargazers

sanjin avatar

Watchers

James Cloos avatar sanjin 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.