sequelize-simple-cache-ioredis v1.0.1
sequelize-simple-cache
This is a simple, transparent, client-side, in-memory cache for Sequelize. Cache invalidation is based on time-to-live (ttl). Selectively add your Sequelize models to the cache. Works with all storage engines supported by Sequelize.
This cache might work for you if you have database tables that (1) are frequently read but very rarely written and (2) contain only few rows of data.
In a project, we had a couple of database tables with a sort of configuration. Something like 4 or 5 tables with some 10 rows of data. Nearly every request needed this data, i.e., it was read all the time. But updated only very rarely, e.g, once a day. So, pre-fetching or simple in-memory caching would work for us.
If that's not matching your scenario, better look for something more sophisticated such as Redis or Memcached.
Tested with
- Sequelize 6, Node 12/14/15, integration tested with Postgres 11/12 (via pg 8) and sqlite3 v5 (memory)
- Sequelize 5, Node 10/12/13, integration tested with Postgres 10/11 (via pg 7) and sqlite3 v4 (memory)
Install
npm install sequelize-simple-cache-ioredis
Usage
Setup the cache along with loading your Sequelize models like this:
const Sequelize = require('sequelize');
const SequelizeSimpleCache = require('sequelize-simple-cache-ioredis');
// create db connection
const sequelize = new Sequelize('database', 'username', 'password', { ... });
// create cache -- referring to Sequelize models by name, e.g., `User`
const cache = new SequelizeSimpleCache({
User: { ttl: 5 }, // 5 second
Page: { }, // default ttl is 30 second
},{},{
host:"127.0.0.1",
port:6379,
db:0
});
// assuming you have your models in separate files with "model definers"
// -- e.g, see below or https://github.com/sequelize/express-example --
// add your models to the cache like this
const User = cache.init(require('./models/user')(sequelize));
const Client = cache.init(require('./models/client')(sequelize));
// no caching for this one (because it's not configured to be cached)
// will only add dummy decorators to the model for a homogeneous interface to all models
const Order = cache.init(require('./models/order')(sequelize));
// the Sequelize model API is fully transparent, no need to change anything.
// first time resolved from database, subsequent times from local cache.
const fred = await User.findOne({ where: { name: 'john' }});
./models/user.js
might look like this:
const { Model } = require('sequelize');
class User extends Model {}
module.exports = (sequelize) => User.init({ /* attributes */ }, { sequelize });
Please note that SequelizeSimpleCache
refers to Sequelize models by name.
The model name is usually equals the class name (e.g., class User extends Model {}
→ User
).
Unless it is specified differently in the model options' modelName
property
(e.g., User.init({ /* attributes */ }, { sequelize, modelName: 'Foo' })
→ Foo
).
The same is true if you are using sequelize.define()
to define your models.
More Details
Supported methods
The following methods on Sequelize model instances are supported for caching:
findOne
, findAndCountAll
, findByPk
, findAll
, count
, min
, max
, sum
.
In addition, for Sequelize v4: find
, findAndCount
, findById
, findByPrimary
, all
.