Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-invalidate or auto-update cached relations #256

Closed
bajtos opened this issue May 14, 2014 · 4 comments
Closed

Auto-invalidate or auto-update cached relations #256

bajtos opened this issue May 14, 2014 · 4 comments
Labels

Comments

@bajtos
Copy link
Member

bajtos commented May 14, 2014

Consider the following code snippet from loopback-workspace:

// Project hasMany Models

project.models(function(err, list) { // (1)
  // print all models
  var modelName = // let the user choose one model
  addPermission(modelName, saveToFiles);
}

function addPermission(modelName, cb) { 
  // extract from project.addPermission()
  project.models( // (2)
    {where: { name: modelName }, limit: 1 },
    function(err, list) {
      list[0].options.acls.push({/* ACL config */});
      list[0].save(cb); // (3)
  });
}

function saveToFiles(cb) {
  // extract from project.saveToFiles
  project.models(function(err, list) { // (4)
    // build models.json from list
    // etc.
    cb();
  });
}

The line (1) fills "project.models" cache with the list of all related models. The line (2) queries the related models and returns a new copy of the model, which is saved back to the datasource in (3). Finally line (4) lists all models again, but receives stale data.

Since the modification was made through project.models, it should be possible to improve the caching mechanism, so that either:

  1. The query on related models returns existing objects from the cache, so that any updates are automatically propagated to the cache too.
  2. The save function of models returned by the query updates the cached record.
  3. The save function of models returned by the query invalidates the whole relation cache and forces the next "find-all" query to fetch fresh data from the datasource.

Workaround

Always pass 'true' as the first parameter of "find-all" queries. In other words, changing the line (4) to project.models(true, function(err, list) { fixes the problem.

Although the workaround is trivial, tracking the problem down to the related-models-cache is not straightforward.

bajtos pushed a commit to strongloop/loopback-workspace that referenced this issue May 14, 2014
Modify Project.toConfig() to always fetch the related models like model
and datasource definitions from the datastore, bypassing any possibly
stale cached values.

Related: strongloop/loopback#256
bajtos pushed a commit to strongloop/loopback-workspace that referenced this issue May 15, 2014
Modify Project.toConfig() to always fetch the related models like model
and datasource definitions from the datastore, bypassing any possibly
stale cached values.

Related: strongloop/loopback#256
@ritch
Copy link
Member

ritch commented May 16, 2014

The relations cache seems to be pretty useless or entirely broken. It looks like it is never invalidated. You have to grab a new instance to get up to date data.

I'd like to get rid of it in favor of all DAO methods hitting the connector API every time. I'd like to move the optimization to the connector level instead of the model instance level. A connector wide cache would improve all hits to a dataSource not just model relations.

@bajtos
Copy link
Member Author

bajtos commented Oct 22, 2014

I am closing this issue, since no user was asking for this enhancement and it clearly was not a priority for us so far. We can reopen it again later when there is more demand.

@kidwm
Copy link

kidwm commented Feb 23, 2015

Hey, I just encountered the same problem, and turn to delay the models listing query for 1 second as a workaround.

@superkhau
Copy link
Contributor

I am closing this issue, since no user was asking for this enhancement and it clearly was not a priority for us so far. We can reopen it again later when there is more demand.

@bajtos I think you forgot to close the issue. Closing.

reactDev037 added a commit to reactDev037/Workspace-backend that referenced this issue Sep 5, 2018
Modify Project.toConfig() to always fetch the related models like model
and datasource definitions from the datastore, bypassing any possibly
stale cached values.

Related: strongloop/loopback#256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants