Making Your APIs Blazingly Fast in .NET 8!

Поделиться
HTML-код
  • Опубликовано: 16 авг 2023
  • Use code ARCH15 and get 15% off the brand new Solution Architecture course on Dometrain: dometrain.com/course/getting-...
    Become a Patreon and get source code access: / nickchapsas
    Hello, everybody, I'm Nick, and in this video, I will show you how you can use the brand you Redis provider to introduce scalable, distributed output caching in .NET and make your APIs and web apps extremely fast!
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasGitHub
    Follow me on Twitter: bit.ly/ChapsasTwitter
    Connect on LinkedIn: bit.ly/ChapsasLinkedIn
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

Комментарии • 89

  • @jamestyrer451
    @jamestyrer451 8 месяцев назад +6

    I have a crow that now says "Hello everybody, i'm Nick". Thanks for all the videos love your channel.

  • @mightygingercles6481
    @mightygingercles6481 9 месяцев назад +10

    I was very confused how this differentiated from the ResponseCache middleware. Maybe I missed it during your video, but I found the answer in the MS docs for Response Cache:
    "Is typically not beneficial for UI apps such as Razor Pages because browsers generally set request headers that prevent caching. Output caching, which is available in ASP.NET Core 7.0 and later, benefits UI apps. With output caching, configuration decides what should be cached independently of HTTP headers."

  • @TkrZ
    @TkrZ 9 месяцев назад +34

    I know you could probably create a custom policy for it, but it'd be nice if there was an easier way (e.g. a fluent method call) to specify what types of response to cache. For example I'd like to cache Ok responses for the city "London" for 5 minutes, but cache the NotFound responses for the city "asdf123" for several hours or days.

    • @alimctavish998
      @alimctavish998 9 месяцев назад +2

      This Is What I Do I Sit On You! , Sit On You! , Sit On You!....

  • @pablocom
    @pablocom 8 месяцев назад

    I love to see your passion explaining those things, ty for sharing

  • @LrgamezD
    @LrgamezD 9 месяцев назад +13

    Amazing video. Now that I'm working with Docker I just realize how easy it makes development without the need of installing 3rd party sotfware. Obsessed with container technology now

    • @michaszura8940
      @michaszura8940 8 месяцев назад +1

      Agree. I was amazed with Docker and it's possibilities, when it comes to additional software or, for instance, integration tests.

  • @kijanawoodard
    @kijanawoodard 9 месяцев назад +3

    Good video.
    Side note, the full redis-stack image (omit -server on the end of the image name) also runs Redis Insight in case someone doesn't have Another Redis Desktop Manager.

  • @eduarddumitru1
    @eduarddumitru1 9 месяцев назад +6

    In Copenhagen we cache the weather output for no more than 10 seconds 🤣

  • @hamadanjum4720
    @hamadanjum4720 9 месяцев назад

    Waited so long for this. Greate job.

  • @chrismantonuk
    @chrismantonuk 9 месяцев назад +2

    Perfect timing! Thanks 🙏 I was just looking at a high latency request today which would benefit from some caching. Question: can you invalidate certain keys? For example if you wanted to fetch fresh weather for London but not Milan, can you use tagging to say “I want to invalidate the cache for ?city=London but keep ?city=Milan”?

  • @microtech2448
    @microtech2448 9 месяцев назад

    Hey, thanks for the video. Can you please explain how to create own sample service with certain options just like any other services we use in startup and configure their options?

  • @samuelschwager
    @samuelschwager 9 месяцев назад +7

    there are 3 things in computer science that are hard: 1) cache invalidation 2) off by one errors

    • @phizc
      @phizc 9 месяцев назад +4

      Forgot naming things. And scope creep.😂

  • @mattiaricci910
    @mattiaricci910 9 месяцев назад +1

    Is it possibile to evict by key? For example if the weather for London gets updated (let's pretend that it's my API that's updating the weather), could I just delete the cache entry for that specific key (maybe using the query string)?

  • @juliangzr4998
    @juliangzr4998 9 месяцев назад

    Great vídeo Nick. I wonder if it works along with Authorization. Once i tried ourput caching in .NET 6 with authorized controllers and it didn't work. It would be great to have the opportunity to vary by Authorization header

  • @jerremm
    @jerremm 9 месяцев назад

    If multiple requests come in before the first one is returned, so the rest still call the weatherservice or do they wait for the first one to finish?

  • @blackenedsprite8542
    @blackenedsprite8542 9 месяцев назад

    Will this be updated in the API course?

  • @sriramancs1119
    @sriramancs1119 9 месяцев назад +12

    Hey Nick. Thanks for the video. Here you are using a simple location string as parameter. How about complex object as parameter? Will the caching work correctly, as the object may differ on every api call?

    • @DamianLesiuk
      @DamianLesiuk 9 месяцев назад +2

      It don't. You can't even invalidate by param name. It always invalidates everything.

    • @gileee
      @gileee 9 месяцев назад

      Caching is actually a very difficult problem as soon as the complexity of your API increases even a little bit. Like what about caching endpoints that return lists, what about objects with different keys that can be used for fetching (I guess just duplicate the cache entries per query right), what if those endpoints return an object with lazy loaded fields? I look at caching more as a necessary evil to be used sparingly when absolutely all else fails.

  • @NikolaiKononov
    @NikolaiKononov 9 месяцев назад

    What console extension do you use?

  • @user-ti5ce4hg1o
    @user-ti5ce4hg1o 9 месяцев назад +4

    when the cache expires, and you get 100 calls at that endpoint at the same time, will they all go in trying to get data (i.e., requires you to thread lock the access to the data refresh call) or redis has a built-in option to control that access? (i.e., only one call goes in to refresh the data and the remaining 99 wait for the cached data after the first call gets served).
    And if redis has such multi-threaded control option built-in, is it scalable or only per instance?

    • @LeMustache
      @LeMustache 9 месяцев назад +2

      It seems that by default locking is enabled which should prevent the issue you're describing

    • @griffin123griffin
      @griffin123griffin 9 месяцев назад

      it is specifically designed for that

  • @zoltanzorgo
    @zoltanzorgo 8 месяцев назад +1

    As this is intended to facilitate scaling up, and the key used looks to contain the port number as well - this can result in different keys for each instance of the service depending on the infrastructure.
    I am wondering how can that be excluded?
    And in general: how can I format the key that is created?

    • @cicpolk
      @cicpolk 8 месяцев назад

      Exactly my concern too. With the approach in video, service instances will not share the cache keys at all.

  • @ourjamie
    @ourjamie 9 месяцев назад +2

    Now that is useful

  • @Masteroxify
    @Masteroxify 7 месяцев назад

    The problem with caching is to determine how long we can store cached value.
    In case of token it is cool because if token would become obsolete then we get error. In case of weather we could return wrong data.

  • @isnotnull
    @isnotnull 9 месяцев назад

    Really COOL!

  • @Kingside88
    @Kingside88 9 месяцев назад

    I do not using it but just wonder if this could useful for translations for example.

  • @urzalukaskubicek9690
    @urzalukaskubicek9690 9 месяцев назад

    I was hoping you will show us the distributed part :)

  • @canergurelsoy6875
    @canergurelsoy6875 9 месяцев назад

    This is a nice one, but I'M just wondering where you gat .NET 8 RC1?

  • @DamianLesiuk
    @DamianLesiuk 9 месяцев назад +1

    It also works with net 7.

  • @MrIsrael3000
    @MrIsrael3000 9 месяцев назад +2

    Nice feature, How nice could be doing caching from the httpclient? To avoid call the API, is it anything like that?

    • @mackie1001
      @mackie1001 9 месяцев назад

      A Redis IDistributedCache implementation would be your friend here

  • @baranacikgoz
    @baranacikgoz 9 месяцев назад +5

    I remember your video implementing redis along with output cache yourself, microsoft listenned you ;;))

  • @kamushekdev
    @kamushekdev 9 месяцев назад +41

    As someone who lives in Saint-Petersburg I can say that the weather could change several times within 5 minutes XDXD

  • @99MrX99
    @99MrX99 9 месяцев назад +2

    Can you invalidate for a specific entry? Like only invalidate for the London weather?

    • @josda1000
      @josda1000 21 день назад

      Each call is per city in this case, so yes. It depends on how the API's request parameters are set up. A cache entry is recorded per api method and parameter set.

  • @matiascasag
    @matiascasag 9 месяцев назад +1

    What if you want to return the cached data ordered in a different way? How would you do it?

    • @dvdrelin
      @dvdrelin 7 месяцев назад

      it is an output cache and depends on request.
      if request has an instruction of ordering as parameter (e.g. in query string such as ..&order=desc&...), the cache middleware will scan for such key.
      And if not found the mapped delegate will be called and the result will be stored with key that accounts this parameter.
      there are two ways - or cache each ordered response (the same tag will help on evicting) or cache unordered and then order on the client side.

    • @dvdrelin
      @dvdrelin 7 месяцев назад

      also you may decouple fetching and presentation of data, cache just data that fetched from storage by predicate and prepare it to present from api doing necessary ordering always.

  • @porcinetdu6944
    @porcinetdu6944 9 месяцев назад +1

    What happened if multiple people hit the endpoint at the same moment when it is not cache anymore ? Because the http call is long may it be possible that there is multiple people entering the controller and so make multiple http call ?

  • @OALBERTO
    @OALBERTO 9 месяцев назад

    Hey Nick, have you thought of creating a subscription model for dometrain? I’ve take courses from you in the past and I’m wondering if this is something we could expect in a near future now that you are inviting external experts

    • @insteor
      @insteor 9 месяцев назад +2

      hi there. could you please share your experience with completed course? if you don't mind to share what courses you have taken and how useful found them. thanks in advance!

    • @OALBERTO
      @OALBERTO 8 месяцев назад +1

      I've taken the Minimal API one. It was very helpful at the time because Nick covered the topic in depth.@@insteor

  • @ajkkarthik9049
    @ajkkarthik9049 3 месяца назад

    can we add specific redis db based on language culture instead of 0 by default like i need 0 for english 1 for spanish
    ?

  • @Velociapcior
    @Velociapcior 9 месяцев назад +10

    Hey Nick I use output cache in my application, but I would like to have option to invalidate cache by some dynamic value. For example when user with specific id is updated, I would like to evict his entry from cache. With this approach you evicted all 'weather' tagged entries. What if we would like to evict only Milan and leave London in cache? This issue stops me from putting output cache in my whole applicaiton. Is it worth of follow up video?

    • @DaminGamerMC
      @DaminGamerMC 9 месяцев назад

      Instead of using this minimal API approach you want to use the service with dependency injection and cache it by UUID for example. Use the service like he did invalidation.

    • @Velociapcior
      @Velociapcior 9 месяцев назад

      @@DaminGamerMC I am not using minimal APIs at all. Currently there is no out of the box functionality to invalidate specific entries in this Output cache functionality that I am aware of. IOutputCacheStore has only EvictByTag method. And with tags defined as in the video, you are not able to evict single entry. Only all of entries related to this tag

    • @EikeSchwass
      @EikeSchwass 9 месяцев назад

      @@Velociapcior couldn't you just use a different tag for every entry (i.e. the key/id of the user)? Then when you want to invalidate a user, just evictbytag(userId)?

    • @Velociapcior
      @Velociapcior 9 месяцев назад +3

      @@EikeSchwass how would you like to do that in attribute?

    • @diaaway
      @diaaway 9 месяцев назад

      Would love to have an out of box solution for this!

  • @Jachtann
    @Jachtann 3 месяца назад

    Trying to implement redis cache right now, because api that I'm creating is being deployed into k8s.
    I did it little different and experiencing timeouts when testing with thousands of requests at once.
    I believe its somehow thread related? Like threads are waiting to be completed?
    I will definettely try this way you showed in video, to see if I'll experience same problem.
    If anybody have some experience with this problem, or just have any ideas, please, let me know.

  • @seesharp81321
    @seesharp81321 9 месяцев назад

    Dude lives in GB and dares to ask 'how much can the weather change in 5 minutes?'! Brilliant! 😂

  • @maksymlazorenko7636
    @maksymlazorenko7636 9 месяцев назад

    Hi Nick, but what about Multitenant application ? Does it persist per (token?)

  • @jerryjeremy4038
    @jerryjeremy4038 19 дней назад

    What if I want to use MS SQL? Is there a nuget package for that?

  • @user-zk5ym9ut1j
    @user-zk5ym9ut1j 9 месяцев назад +1

    I don't get the idea of distributed cache since such data as in example can be easily cached in memory to avoid additional network call + cache DB overhead
    Today memory is fairly cheap and caching of small sets of data in external system looks redundant

    • @kasperstergaard1592
      @kasperstergaard1592 9 месяцев назад +2

      Because you might run multiple instances of your service for scalability. If you cache in memory they'll each end up with different caches and give inconsistent responses.
      I memory is the right answer if you only have one instance. It's the wrong answer if you have more.

    • @TkrZ
      @TkrZ 9 месяцев назад +1

      If you have more than one instance of your application running behind a load balancer, with in-memory caching the work will be need to be ran on each instance for it to be cached locally to that instance. If you use a distributed cache only one of the instances needs to perform the work.

    • @user-zk5ym9ut1j
      @user-zk5ym9ut1j 9 месяцев назад

      @@kasperstergaard1592 well, it's not that bad, even YT or Twitter does this. It depends on nature of the data

  • @manchastyle
    @manchastyle 8 месяцев назад

    Which IDE are you using ?

    • @robvaughan6649
      @robvaughan6649 8 месяцев назад

      JetBrains Rider.

    • @nztzn
      @nztzn 3 месяца назад +1

      JetBrains Rider

  • @crackdesign
    @crackdesign 5 месяцев назад

    For those like me scratching their heads why output caching doesn't work with authenticated endpoints: you'll need to either redefine base policy or write your custom one.

  • @ProfessorCodemunkie
    @ProfessorCodemunkie 9 месяцев назад

    I've never been a fan of caching results at the endpoint level. What happens if you want to use your Weather service in another endpoint? Each one would be caching different values whereas if your cache logic was in the service it would be available for any consumer.

  • @JansthcirlU
    @JansthcirlU 9 месяцев назад +1

    your API key is visible in the video, just so you know

    • @reikooters
      @reikooters 9 месяцев назад +3

      In older videos he's said "don't bother trying to use it cause it will be invalid by the time I release the video" and I think he stopped bothering to say that now

    • @nickchapsas
      @nickchapsas  9 месяцев назад +8

      Yeah they are always invalidated after shooting

    • @mariocamspam72
      @mariocamspam72 9 месяцев назад +5

      ​@@nickchapsasHey, at least you get extra engagement from these types of comments

    • @JansthcirlU
      @JansthcirlU 9 месяцев назад

      @@reikooters ah I see, thanks

    • @JansthcirlU
      @JansthcirlU 9 месяцев назад

      @@nickchapsas thanks for clarifying!

  • @richardpayne
    @richardpayne 9 месяцев назад

    The weather doesn't change in 5 minutes? I thought you lived in the UK, so you should know better. :D

  • @lizard450
    @lizard450 6 месяцев назад

    lol I wrote this years ago... oh well guess i'll trash it now haha

  • @psyckojoe
    @psyckojoe 9 месяцев назад

    œ

  • @IcaroFelix2023
    @IcaroFelix2023 9 месяцев назад +1

    That looks a good approach to keep sensitive data of users from authorization module, better than use browser's localstorage or session storage.

    • @elan2199
      @elan2199 9 месяцев назад

      I don't think it makes sense

  • @RaVq91
    @RaVq91 9 месяцев назад +3

    .NET8 has nothing to do with redis. Thumb down for that click bait.