HybridCache - The New Caching Library in .NET 9

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

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

  • @MilanJovanovicTech
    @MilanJovanovicTech  Месяц назад +1

    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

  • @Lunsterful
    @Lunsterful Месяц назад +1

    You're really good at this, great video.

  • @notXarv
    @notXarv Месяц назад

    Great content Milan, thanks! 🙏

  • @KingOfBlades27
    @KingOfBlades27 Месяц назад

    Really interesting 🤔 Have to investigate this more. The hybrid version indeed seems to be really useful.

  • @SalmanShafiq-y3q
    @SalmanShafiq-y3q Месяц назад +2

    really love this one ❤
    it would be helpful a video how to use hybrid cache as a caching behavior in Mediator Pipeline behavior in Clean Architecture.

  • @CarrigansGuitarClub
    @CarrigansGuitarClub Месяц назад

    That is a big improvement on IM and ID

  • @zoltanzorgo
    @zoltanzorgo Месяц назад +7

    No, it is not a better option - at least not yet. I have tried to use it in my current project, but I had to abandon this path. It's missing essential features for an L1-L2 cache, (like the backplane) and the API lacks some that would cost nothing to add, like a simple "get." No wonder it is still in preview after .NET 9 was released a while ago. However, it had its impact already: with the push from the good ideas in it (the flags and the tagging), for example, the author of FusionCache improved his library by adding those (check out the preview version). Maybe others will follow suit.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад +2

      The backplane is working?

    • @zoltanzorgo
      @zoltanzorgo Месяц назад +2

      In HybridCache? No support there. In FusionCache it is, as far as my use-cases go.

    • @antonmartyniuk
      @antonmartyniuk Месяц назад +2

      Do you use FusionCache? A much better option

  • @asgeirs
    @asgeirs Месяц назад

    Thanks for a great video. Suggest a video showing how to use HybridCache with AWS ElastiCache

    • @MilanJovanovicTech
      @MilanJovanovicTech  29 дней назад +1

      Great suggestion!

    • @asgeirs
      @asgeirs 28 дней назад

      @ also how you would setup a local dev environment to test and etc. Would it be best to setup a local docker redis instance or connect directly to ElastiCache from dev machine which seams to be somewhat convoluted

  • @10Totti
    @10Totti Месяц назад +1

    Best Tutorial!

  • @israellouisyvantraore8866
    @israellouisyvantraore8866 Месяц назад

    I would like to know in a clean architecture, can we use hybridcach to store informations for the AutorizationHandler? Where we could get some values in order to grant access or revoke? Do you know if the logic can be used in an interface and served as scope across the logic? But still preserve the data in the cash?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад

      If you're using HybridCache in the implementation - then yes

    • @israellouisyvantraore8866
      @israellouisyvantraore8866 Месяц назад

      @@MilanJovanovicTech great, I just done it and you are right, surprisingly Chat GPT was wrong

  • @antonmartyniuk
    @antonmartyniuk Месяц назад

    Have you compared performance of HybridCache vs IMemoryCache and IDistributedCache. Interesting how much overhead we get with locking in HybridCache.
    Also a FusionCache is a much better option atm, has much more features and is stable for years. At least atm

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад +1

      I think you're right about FusionCache.
      Didn't do a performance comparison

  • @Ahmed-ui5wn
    @Ahmed-ui5wn Месяц назад

    Nice video. Could you please show us how to do this for output caching and how to invalidate it

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад +1

      Ok I will try

    • @dfbdtrhgwtwd7149
      @dfbdtrhgwtwd7149 Месяц назад

      Hmm... What do you mean by "output caching"? The term "output caching" applies, for example, to web pages _dynamically_generated_ on the webserver. But there is nothing being generated here. So, it is just caching. Returned value IS the output value!

  • @hoangtan883
    @hoangtan883 Месяц назад

    Is HybridCache a lite version of FusionCache?
    If so, what are the advantages of using HybridCache over FusionCache?
    Thanks!

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад

      Different things. FusionCache had this functionality way before. HybridCache is MSFT's take on it.

  • @ItsSeanPhillips
    @ItsSeanPhillips Месяц назад

    So is SlidingExpiration officially not part of the HybridCacheOptions?

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад

      Seems not:
      - github.com/dotnet/aspnetcore/issues/56754
      - github.com/dotnet/extensions/issues/5649

  • @HoàngPhan-y7u
    @HoàngPhan-y7u Месяц назад

    The content is great. However, if 1,000 requests hit the endpoints simultaneously, it’s likely that multiple requests will access the database, not just one. Hopefully, you have other videos on caching that can be applied to real-world scenarios.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад

      @@HoàngPhan-y7u If it's a 1000 requests for the same key, only one will hit the database.

    • @HoàngPhan-y7u
      @HoàngPhan-y7u Месяц назад

      @@MilanJovanovicTech This is a common issue called the "cache stampede" or "thundering herd" problem.
      A cache stampede (or thundering herd) occurs when multiple requests try to access a cached item that has expired at the same time, causing multiple concurrent attempts to regenerate the same cached value. This can lead to:
      1. Excessive database load as multiple requests try to fetch the same data
      2. Potential race conditions
      3. Performance degradation
      To prevent cache stampede with IMemoryCache, you need to implement additional patterns such as:
      1. Sliding cache lock pattern
      2. Using SemaphoreSlim for synchronization
      3. Implementing a background refresh before expiration
      4. Using stale-while-revalidate pattern
      Microsoft recommends using IMemoryCache.GetOrCreate or GetOrCreateAsync methods, but these alone don't completely solve the stampede problem in high-concurrency scenarios.

    • @HoàngPhan-y7u
      @HoàngPhan-y7u Месяц назад

      To prevent multiple concurrent requests from hitting the database for the same key, we can implement a distributed lock pattern using Redis and RedLock.

  • @dotnetwithmark
    @dotnetwithmark Месяц назад +1

    I had always an issue with the cache and is how do you know if the data has changed or not?

    • @деменция-н4п
      @деменция-н4п Месяц назад +2

      when you change the data you'll know that data changes)

    • @dotnetwithmark
      @dotnetwithmark Месяц назад +2

      @@деменция-н4п i got it, so you said if in other thread i changed the data i need to add an extra logic to mark that cached information as old o removed it?

    • @omerabay
      @omerabay Месяц назад

      That's why you configure cache expiration. Data is served from the cache until it expires or becomes invalidated. Once the cache expires, the updated data can be retrieved. If you want to see changes to your data immediately, you would need to check the server every time. However, doing so defeats the purpose of using the cache-aside pattern.

    • @MilanJovanovicTech
      @MilanJovanovicTech  Месяц назад

      Clear the cache when making the update (simplest solution)

    • @dfbdtrhgwtwd7149
      @dfbdtrhgwtwd7149 Месяц назад

      There are multiple strategies for invalidating the cache. None of them is ideal - each one has their own pros and cons. The simplest ones are manual invalidation and time-based invalidation. More advanced are based on some sort of an external agent monitoring and updating the values.