Skip to content

How to clear cache? #37

Description

@btimeteam

I have a problem that when I update a model the cache does not update, the same information remains, I tried to hook them in sequelize, but it did not work.

const   config      = require('../config'),
        Sequelize   = require('sequelize'),
        cacher      = require('sequelize-redis-cache'),
        redis       = require('redis'),
        redisCache  = redis.createClient(config.redis.port, config.redis.host),
        database    = new Sequelize(config.DATABASE_URI, {
            define: {
                hooks: {
                    afterCreate:    (instance, options) => clearCache(instance, options),
                    afterDestroy:   (instance, options) => clearCache(instance, options),
                    afterUpdate:    (instance, options) => clearCache(instance, options),
                    afterSave:      (instance, options) => clearCache(instance, options),
                    afterUpsert:    (instance, options) => clearCache(instance, options),
                }
            }
        });

const clearCache = function(instance, options) {
    if (instance.constructor.name === 'Session') {
        return;
    }

    cacher(database, redisCache).model(instance.constructor.name).clearCache();
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions