Skip to content
Simple to use and universal cache layer for Sequelize
Branch: master
Clone or download
Latest commit fa21e48 Mar 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples [core] Bump peer-deps versions Mar 18, 2019
packages v2.0.1 Mar 18, 2019
.codeclimate.yml [adaptor-memcache-plus] Add memcache-plus adaptor Sep 6, 2018
.gitignore [core] switch repo to lerna Sep 6, 2018
.travis.yml
README.md
lerna.json
package-lock.json
package.json [core] update lerna to version 3.11.0 Feb 10, 2019

README.md

sequelize-transparent-cache

Build Status Coverage Status JavaScript Style Guide Code Climate npm version Dependency Status Greenkeeper badge

Simple to use and universal cache layer for Sequelize.

  • Abstract: does not depends on underlying database, or cache specific
  • Transparent: objects returned from cache are regular Sequelize instances with all your methods
  • Explicit: all calls to cache comes through cache() method
  • Lightweight: zero additional dependencies

Installation

Install sequelize-transparent-cache itself:

npm install --save sequelize-transparent-cache

Find and install appropriate adaptor for your cache system, see "Available adaptors" section below. In this example we will use ioredis

npm install --save sequelize-transparent-cache-ioredis

Example usage

const Redis = require('ioredis')
const redis = new Redis()

const RedisAdaptor = require('sequelize-transparent-cache-ioredis')
const redisAdaptor = new RedisAdaptor({
  client: redis,
  namespace: 'model',
  lifetime: 60 * 60
})

const sequelizeCache = require('sequelize-transparent-cache')
const { withCache } = sequelizeCache(redisAdaptor)

const Sequelize = require('sequelize')
const sequelize = new Sequelize('database', 'user', 'password', {
  dialect: 'mysql',
  host: 'localhost',
  port: 3306
})

// Register and wrap your models:
// withCache() will add cache() methods to all models and instances in sequelize v4
const User = withCache(sequelize.import('./models/user'))

sequelize.sync()
.then(() => {
  return User.cache().create({ // Create user in db and in cache
    id: 1,
    name: 'Daniel'
  })
})
.then(() => {
  return User.cache().findByPk(1) // Load user from cache
})
.then(user => {
  return user.cache().update({ // Update in db and cache
    name: 'Vikki'
  })
})

Look for all examples applications in examples folder.

Methods

Object returned by cache() call contains wrappers for limited subset of sequelize model or instance methods.

Instance:

Model:

In addition, both objects will contain client() method to get cache adaptor.

Available adaptors

You can easy write your own adaptor. Each adatper must implement 3 methods:

  • get(path: Array): Promise<value>
  • set(path: Array, value: Object): Promise
  • del(path: Array): Promise

Checkout existed adaptors for reference implementation.

You can’t perform that action at this time.