The EASY Way to Add Cache to a Repository in .NET

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

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

  • @TonoNamnum
    @TonoNamnum Год назад +2

    I don't think most people see how important this is. Amazing thanks!

  • @bengie23
    @bengie23 Год назад +10

    I have done exactly what you showed here for several years and I have always called it a decorator. I see your point about the difference between the 2 patterns and despite the fact that theoretically there's a difference, I guess most ppl will continue to call it a decorator.
    Now I would like to give you and idea and challenge you to go to the next level, can you show how to implement a repository with selective caching as well as cache invalidation scenarios but also putting in place a nosql or search index. The idea is, some calls go directly to the db, no cache, no index, others have caching only, no invalidation, others do have invalidation and finally also those that talk to the search Index every time there's a change.
    I have done a few things like that myself but I would like to know and learn from you.
    Thanks for the video, even though this is common in the industry it's always important to ask ourselves why we do it. 👍

    • @gui.ferreira
      @gui.ferreira  Год назад +1

      Thanks for your comment.
      I've cache invalidation on my list. Let me think how I would share that in a way that makes sense for this channel 😉

  • @NotInventedHereShow
    @NotInventedHereShow Год назад +1

    Love videos like this. Great explanation of the difference between the decorator and proxy pattern.

  • @alexandermackintosh1755
    @alexandermackintosh1755 2 месяца назад

    Thanks for the video! How does this work when you come to unit test the cached version ? You'd need to make an abstract base class so you can mock it out or use the real inner ProductRepository right?

  • @alexandero.9413
    @alexandero.9413 Год назад +1

    Very nicely explained. Thanks!

  • @Tamer_Ali
    @Tamer_Ali 11 месяцев назад +1

    Thanks a lot Gui. keep going
    could you talk about cache invalidation in one of your upcoming videos?

    • @gui.ferreira
      @gui.ferreira  11 месяцев назад

      Hey! It's on the pipeline 😉

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

      @@gui.ferreira paginated list should be cached too?

    • @gui.ferreira
      @gui.ferreira  11 месяцев назад

      ​@@Tamer_Ali Depends. It's tricky. Especially if you have filters. However, sometimes, having the first page cached can be useful to improve the user experience.

  • @franciscovilches6839
    @franciscovilches6839 Год назад +2

    Risk of using both implementations is when you need to invalidate cache. E.g. you use the db implementation to update a record, yet the cahed implementation will still return the old record

    • @gui.ferreira
      @gui.ferreira  Год назад

      That's why Caching is hard. Cache invalidation and data consistency are difficult.

  • @laionik
    @laionik Год назад +2

    Hey Gui, what about situation when one of the methods is reusing another one from repository/service?
    I have a situation whehre
    one method is taking data from an external API (huge amount unfortunately, corpo style -_-)
    Another one is about to search one specified case out of the whole set, so instead of asking the API I would prefer to use the cached one if it is already cached.
    Both methods are in the same class, so how can I call the cached service/cached data from the implemented service?

    • @gui.ferreira
      @gui.ferreira  Год назад

      Are they in the same class and part of the same interface?
      Can you have the Proxy do that? So, the second method can search the cache, if it's not there, check the first method's cache, if still can't find it, ask the API?! Does it make sense?

  • @jose-sebastian-garcia
    @jose-sebastian-garcia Год назад

    How do you invalidate the memorycache if the record is obsolete?
    I would like to reduce the database calls.
    Does a facade pattern mixed with proxy a possible solution for this?

  • @gakshay9537
    @gakshay9537 Год назад +2

    Nice explanation differentiating the decorator and proxy pattern.
    But consider a case where there 4 interfaces and each interface is having around 5 methods. Assuming only one method in interface can be cached.
    In this case, 4 new implementations should be created but only one method should be cahnged. All the remaining methods should be called from the main class.
    How can we avoid this case. Is there an easy way where I can just add new implementation for the method which needs caching .

    • @gui.ferreira
      @gui.ferreira  Год назад

      Yes, you are right.
      The first question that I would raise, is if that interface shouldn't be segregated (the I in SOLID).
      The Decorator implementation can let you do that if you go through the inheritance root. refactoring.guru/design-patterns/decorator/csharp/example
      Not my favourite approach to be honest.

    • @gakshay9537
      @gakshay9537 Год назад +1

      @@gui.ferreira thank you for responding. Let me go through that blog

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

    Should the cache be registered as a singleton rather than scoped? My concern is that using a scoped lifetime might result in the cache being recreated for each new logical operation within the application, which seems inefficient

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

    Could you talk about cache stampede too if possible?

    • @gui.ferreira
      @gui.ferreira  11 месяцев назад

      Taking note 🫡

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

      @@gui.ferreira thanks

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

    Which IDE is this?

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

    now we have dublicate methods

  • @FB-eb6tx
    @FB-eb6tx Год назад +1

    Thanks for this content!