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

fix cache #70

Merged
merged 1 commit into from
Mar 2, 2023
Merged

fix cache #70

merged 1 commit into from
Mar 2, 2023

Conversation

dislido
Copy link
Contributor

@dislido dislido commented Nov 30, 2022

should be the same as line 96

fix #69

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

should be the same as line 96
@midnight-wonderer
Copy link

Guys @titanism , @3imed-jaberi (Pinging you two because you are the latest active.)
Could you take a quick look at this pull request? It should be a quick merge, IMO. And probably release ASAP.

Let me explain

  async function render (view, options) {
    view += settings.viewExt
    const viewPath = path.join(settings.root, view)
    debug(`render: ${viewPath}`)
    // get from cache
    if (settings.cache && cache[viewPath]) return cache[viewPath](options.scope, options)

    const tpl = await settings.fs.readFile(viewPath, 'utf8')

    const fn = ejs.compile(tpl, {
      filename: viewPath,
      _with: settings._with,
      compileDebug: settings.debug && settings.compileDebug,
      debug: settings.debug,
      delimiter: settings.delimiter,
      cache: settings.cache,
      async: settings.async,
      outputFunctionName: settings.outputFunctionName
    })
    if (settings.cache) cache[viewPath] = fn
    return fn.call(options.scope, options)
  }

This function does roughly three things

  • Cache lookup
if (settings.cache && cache[viewPath])
  • If nothing is in the cache, put things in the cache.
cache[viewPath] = fn
  • execute what was cached
fn.call(options.scope, options)

But the way to execute what was cached is inconsistent
we have

return cache[viewPath](options.scope, options)

vs

fn.call(options.scope, options)

The first one is broken; they should be the same, hence, the pull request.

@titanism titanism merged commit 767b4cc into koajs:master Mar 2, 2023
@titanism
Copy link
Contributor

titanism commented Mar 2, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

when cache is true, something wrong will be
4 participants