Skip to content

Problem with cache initiation #24

@carmmac

Description

@carmmac

I'm following the instructions provided and initiate the cache while defining my models:

// --- models/index.js ---

const SequelizeSimpleCache = require(`sequelize-simple-cache`);

const defineCategory = require(`./category`);
const defineComment = require(`./comment`);
const defineArticle = require(`./article`);
const defineArticleCategory = require(`./article-category`);
const Aliase = require(`./aliase`);

const cache = new SequelizeSimpleCache({
  Article: {ttl: false}, // cache forever
  Category: {ttl: false},
  Comment: {ttl: false}
}, {
  debug: true,
  ops: 10,
});

module.exports = (sequelize) => {
  const Category = defineCategory(sequelize);
  const Comment = defineComment(sequelize);
  const Article = defineArticle(sequelize);
  const ArticleCategory = defineArticleCategory(sequelize);

  cache.init(Category);
  cache.init(Comment);
  cache.init(Article);

  Article.hasMany(Comment, {as: Aliase.COMMENTS, foreignKey: `articleId`});
  Comment.belongsTo(Article, {as: Aliase.ARTICLE, foreignKey: `articleId`});
  
  ...

  return {Category, Comment, Article, ArticleCategory};
};

Each model is located in a separate module and is defined via class:

// --- models/category.js ---

const {DataTypes, Model} = require(`sequelize`);

class Category extends Model {}

module.exports = (sequelize) => Category.init({
  name: {
    type: DataTypes.STRING,
    allowNull: false
  }
}, {
  sequelize,
  modelName: `Category`,
  tableName: `categories`
});

When I start my server, the cache seems to accept my models:

CACHE INIT {
  type: 'Category',
  ttl: false,
  methods: [
    'findOne',  'findAndCountAll',
    'findByPk', 'findAll',
    'count',    'min',
    'max',      'sum',
    'find',     'findAndCount',
    'findById', 'findByPrimary',
    'all'
  ],
  methodsUpdate: [
    'create',
    'bulkCreate',
    'update',
    'destroy',
    'upsert',
    'findOrBuild',
    'insertOrUpdate',
    'findOrInitialize',
    'updateAttributes'
  ],
  limit: 50,
  clearOnUpdate: true,
  hit: 0,
  miss: 0,
  load: 0,
  purge: 0,
  ratio: NaN,
  size: { Category: 0 }
}

But while using app in dev mode the cache debug console remains the same:

CACHE OPS {
  hit: 0,
  miss: 0,
  load: 0,
  purge: 0,
  ratio: NaN,
  size: { Category: 0, Comment: 0, Article: 0 }
}

I assume the size key refers to the cached instances, but it's empty despite any queries made to DB.

I have tried to initiate cache just after my models are defined and before they are filled with mock data, the result is the same.
What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions