Git Product home page Git Product logo

Comments (1)

jodydonetti avatar jodydonetti commented on June 3, 2024

Hi @rmandvikar , and thanks for using FusionCache!

I'm not sure I'm following you here: the things you described are not specific for how to work with FusionCache, but how to call async methods in .net itself.

Normally the CancellationTokens are "chained", meaning that one present as a param in a method should be passed to a sub method call inside of it (if it does support it).
For example in a controller action you can have a CancellationToken as a param, and use that when calling the database inside of the action.

Having said that, when you have a method that accept a lambda (like the factory in a GetOrSet call), the lambda should be designed such that it can accept a CancellationToken itself, for 2 main reasons:

  1. it is the signature of the lambda itself, because the signature is for a method that accepts a CancellationToken so it should be there as a param
  2. by being a param of the lambda you can avoid some capturing

In your 2 examples I can see 2 separate differences:

  1. in the first one the lambda is specified as async, and inside it does an await, while in the second one the lambda is not marked as async and it does not await, so in the second one the Task<T> returned by valueProvider.GetValueAsync<T>(...) will be returned directly. The difference in this case is mostly related to how exceptions that can be thrown will be handled by the runtime
  2. in the first one you are using the CancellationToken provided to the lambda, whereas in the second one you are directly using the outer one, therefore capturing it in a closure

But again, these are not related to FusionCache itself: the same things can be said about the controller/action example I've made earlier, so I don't think I should add them to the documentation.

Closing this for now, but let me know what you think and if I've missed something I'll gladly reopen it.

from fusioncache.

Related Issues (20)

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.