MediatR Caching Behavior - Caching In C# .NET Core Web API Using MediatR

Поделиться
HTML-код
  • Опубликовано: 14 янв 2025

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

  • @jonowilliams26
    @jonowilliams26  3 года назад +2

    Jump straight into the code here: 00:46​ ✌

  • @PyronTheMage
    @PyronTheMage 3 года назад +2

    Looking forward for part 5. Really love this series :)

  • @shahzshafie
    @shahzshafie 3 года назад +1

    simple, short, and clear = awesome tutorials

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

    Thanks a lot Jono for the awesome video.
    I hope you talk about Cache Invalidation using MediatR Behavior too

  • @ebrahimmansur9815
    @ebrahimmansur9815 3 года назад +1

    Good work mate ...loving it !

    • @jonowilliams26
      @jonowilliams26  3 года назад

      Thank you! Appreciate the positive feedback 👍

  • @lightandtheheat
    @lightandtheheat 3 года назад +2

    I love how concise your videos are, and that they're devoid of ego, unlike some other (purple) channels on these topics.
    Question: is it safe to assume that returning a cache hit will interrupt the pipeline? In other words, you didn't call "next()" during a cache hit. Does that mean that caching must always come last in the pipeline? Or is there a way to implement this such that validation or whatever can come afterward?

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      Thank you ! I’m glad you like the videos! To answer your question, yes, in the example all other pipelines after the cache will be short circuited and not executed.

  • @belalbadawy3593
    @belalbadawy3593 3 года назад +1

    Very nice video hope that you do series about clean architecture with CQRS

    • @jonowilliams26
      @jonowilliams26  3 года назад

      Thanks ! Glad you liked the video! I will add it to my backlog!

  • @asrajan55
    @asrajan55 3 года назад +2

    I am somewhat uncomfortable about calling the Request an ICacheable. Isn't it the response that is cached and not the request? Maybe IGetCachedResponse?

  • @PelFox
    @PelFox 3 года назад +2

    Where would you typically run cache invalidation with mediatr? You could implement a similar approach using a pipeline, but often time you need more information than what's provided in the command. Eg. you may cache a single item by Id, but then that item may also exist in another request which returns a collection, which is cached by a different key.

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      There is a couple of different options. You could have your command implement an Invalidatable interface and use a behaviour similar to the caching. However, I think the better approach would be using domain events which are handled using mediatR event publishing. Then you can inject other services into your handler to perform all the logic you need. I might cover this in the future video 👍

  • @HuHnies
    @HuHnies 3 года назад +1

    Hi, nice video, just what I needed.
    An adjustment to your code would be to check for success or failure before caching - something like this:
    if (response is IResult typedResponse)
    if (typedResponse.IsSuccess)

    _memoryCache.Set("yourkey", response);

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      Thanks for the positive feedback! And thanks for the suggestion! I appreciate it

  • @Siddiskongen
    @Siddiskongen Год назад

    Kaish!!!

  • @MortezaDalil
    @MortezaDalil 3 года назад

    Thanks for your great video. What is your solution for add handler to keep added item in cache? Is it good idea for command Handler to add item in database and cache(if not exist) to achieve better performance?

  • @jamesherrero7334
    @jamesherrero7334 3 года назад

    solid video.. subscribed!
    Please continue posting dotnet content!
    edit: Went into the repo to look at the code and I could only view the test folder?

  • @j_ason_rpg
    @j_ason_rpg 3 года назад

    How do you cache a query that has no parameter? Such as a query that returns a list but whose underlying method doesn't take parameters.

    • @davidpement
      @davidpement 3 года назад

      If you're still looking for an answer to this, the CacheKey would just be a constant.

  • @zakariabenmassaoud
    @zakariabenmassaoud 3 года назад

    Clean and clear! Can I implement a Redis Cache the same way as Memory Cache?

    • @jonowilliams26
      @jonowilliams26  3 года назад +1

      Thank you ! Yeah you can. Just replace memory cache with redis 👍

  • @hubert23911
    @hubert23911 3 года назад +1

    Which video has the validation behavior? Btw. great video, tho is it always the client's job to send query id or we can somehow hash the request on server to determin whether or not we encountered same request in the past?

    • @jonowilliams26
      @jonowilliams26  3 года назад +2

      Still currently editing that one at the moment, so hopefully will be out soon. Also, we are using the id sent by the client to cache the query. So in the example I sent Get todo by ID request using 1, it added the response to the cache. And then I sent the same get todo by ID request and instead of going to the request handler, it just got the response out of the cache.

    • @hubert23911
      @hubert23911 3 года назад

      @@jonowilliams26 thanks, now I get it!

  • @markpolak8622
    @markpolak8622 3 года назад

    Hi, your tutorial really helped me, could you do a video about testing MediatR commands and queries? Thank you.

  • @senaris
    @senaris 3 года назад

    Why just not using 'MemoryCache.Default' since it's found in 'System.Runtime.Caching''? Based on your example it would provide the same functionality.

  • @gabriel99999-r
    @gabriel99999-r 3 года назад

    Great but how to invalidate ? Without that this is incomplete. I'd be happy to see that

  • @cnikolov
    @cnikolov 3 года назад

    add Thread.Sleep(1000* 30) in get people method to appreciate the cache :D

  • @IssaFram
    @IssaFram 3 года назад

    please use this. for instance members, methods, etc. But the video is good.

  • @johnmorsley
    @johnmorsley 2 года назад

    Great video, thanks. But why do you say cache funny? It's pronounced kash

  • @zakustolondoo
    @zakustolondoo 3 года назад +2

    Thank you so much for all these videos. You should publish some courses on Udemy or pluralsight.com. I love your straight to the point tutorials.