Coder Social home page Coder Social logo

Comments (10)

FFKL avatar FFKL commented on May 24, 2024 1

Async methods don't block execution thread. Sync methods don't allow process other async operations (like http request, timers) while fs works. Example with await/async syntax:

async () => {
  if (await Fs.exists(cacheFile)) {
    data = await Fs.readFile(cacheFile)
    data = JSON.parse(data)
  } else {
    // do something else
  }
}

It is possible to improve it by modern syntax and async) easy to read and easy to use.

from cache-all.

FFKL avatar FFKL commented on May 24, 2024 1

You're right. I missed It. But sync methods in constructor it's not good decision too. 😄 I think, for initialization we should use init() method for each type of caches. Now redis cache also use async functions for auth.

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024 1

Hi bro @FFKL ,
Version 2.0.8 was published to npm, I separated init method for each cache type.

Thank you for your supported 😄

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

Hi bro!

About sync methods of fs, in this case, i think change to async doesn't make sense because next lines still must wait to previous line was done, example in get function:

if (Fs.existsSync(cacheFile)) {
    data = Fs.readFileSync(cacheFile)
    data = JSON.parse(data)
  } else {
    return fn(null, null)
}

// Next lines

As you see, we must check the file is exist before we can read it, same for other lines, right?
Are you have any idea to improve it? Thank you so much!

Additional, in core files, i used callback, this is a mistake 😢but it worked fine.

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

Good morning bro

I understood, I previously thought that the expensive task like I/O, Network,... will be executed on a worker in worker pool, not the main node event loop, so it doesn't block the main thread (event loop). I read some article about that.

But by you notice, seems I have misunderstood the problem
I think i will create a benchmark for this issue.

Thank you so much

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

About callback, i think we will rewrite whole project to typescript as version 3, so don't care about callback syntax, lol 🤣

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

Ah, I understood, i totally wrong, the IO task still be execute on a worker, but the event loop still waiting to it result if it's synchronous.

You saved my life, lol. It should be release in v2.0.8, i will working on it tonight or maybe you may help me create a PR, I grateful for that.

Thank you so much

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

Hi bro @FFKL, i merged and add test hook for it, but we still have a problem in fileStore constructor, we can't await constructor, so if we execute a cache method immediately after init it. Example:

fileCache.init()

await fileCache.set('key', { foo: 'bar' }) // FileStore cache instance still initializing

In this case, i think we should use sync method in constructor, it will not take too much cost because we just init it once. So should i change constructor to use sync method (same with old constructor)?

Thank you so much

from cache-all.

bahung1221 avatar bahung1221 commented on May 24, 2024

I missed redis constructor, too, lol.

It's ok, we can implement separate init method for each type of caches, and it should be async.
I think i will implement it in the next day, i bother you too much 🤣

Thank bro

from cache-all.

FFKL avatar FFKL commented on May 24, 2024

@bahung1221 cool! Good job 👍

from cache-all.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.