Blazing Fast Caching Service In ASP.NET Core | Redis Compatible

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024

ความคิดเห็น • 109

  • @MilanJovanovicTech
    @MilanJovanovicTech  ปีที่แล้ว +4

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

  • @wilkovanderveen9099
    @wilkovanderveen9099 ปีที่แล้ว +1

    As always, a very intuitive video. Last week I was working on a cache implementation which has the requirement of using a Func to retrieve related cache items. That bought me to an alternative: the easycaching library which is somewhat more advanced and also has the possibility to use redis or file based caching next to in memory caching.

  • @sasquatch_devs
    @sasquatch_devs 9 หลายเดือนก่อน +1

    Wow, this made so many things click in my head. Thank you for sharing!

  • @LilPozzer
    @LilPozzer 9 หลายเดือนก่อน +1

    This is so easy to watch and follow along!

  • @kodindoyannick5328
    @kodindoyannick5328 8 หลายเดือนก่อน

    Awesome. Very clear! Thanks Milan, the best instructor

  • @mahyarazad
    @mahyarazad 4 หลายเดือนก่อน

    Very informative video. I've spent almost two days finding the correct implementation of the Task.WhenAll for parallelism. I wish I had seen your video sooner :-)

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 หลายเดือนก่อน +2

      There's always a gem hidden somewhere. I wish YT search made stuff like that easier to find...

  • @BK-19
    @BK-19 ปีที่แล้ว +1

    Great!! Thank you for clear cut understanding..

  • @Tamer_Ali
    @Tamer_Ali ปีที่แล้ว +1

    Thanks Milan for your awesome video.
    as I understood from the comments you were thinking to discuss another parts related to this video topic but long videos is your problem
    You can create a vote for that and see the viewers opinions .

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      Check if people want long videos?

    • @Tamer_Ali
      @Tamer_Ali ปีที่แล้ว

      @@MilanJovanovicTech yes, depend on what you will talk about in the video. some videos like this one may talk about 30 min

  • @RMarjanovic
    @RMarjanovic ปีที่แล้ว +4

    A real clean solution using MediatR's pipeline behaviors would be to define an interface that would force implementing a key string and have all queries with that interface go through a caching behavior pipeline. Could be a really nice continuation on this video.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      I've done something similar in the past, so I'll see if I can fit it into next week's recording session

  • @magendranv6251
    @magendranv6251 ปีที่แล้ว +1

    Good one! its clear and easy to understand.

  • @nirajjoshi9147
    @nirajjoshi9147 ปีที่แล้ว +1

    I can just say that awesome effort 🎉

  •  ปีที่แล้ว +3

    Congratulations for the very instructive video. I think the generic use of caches should have been implemented as a Pipelines Behavior. Also missing half the work to expire the cache when modifying the product collection. In any case, congratulations on the video. Everything very clear.😊

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +3

      I'd be careful with placing caching in a PipelineBehavior, since you may not want to cache everything.
      Agree on the cache expiration part, but then it would be too long of a video.

  • @tonystoynev7969
    @tonystoynev7969 ปีที่แล้ว +3

    Nice video! I would enjoy a video for using Redis as a message broker

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      Now that should make for an interesting video, honestly!

  • @cuachristine
    @cuachristine ปีที่แล้ว +9

    RemoveByPrefixAsync doesn't really work as it will only remove keys that were added by the current instance of your service. Keys with the same prefix added other boxes in the web farm won't be deleted.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +4

      Agreed, and I do believe I stated that in the video 🤔
      I'll tackle a nice way to solve this using Redis Pub-Sub.

    • @denizsatr3079
      @denizsatr3079 ปีที่แล้ว

      ​@@MilanJovanovicTech Why you are not putting current keys into redis with special key? Then you can easily fetch all keys from redis and manipulate them?

    • @Max1weber
      @Max1weber 6 หลายเดือนก่อน

      ​@@denizsatr3079correct I would do that as well

    • @pilz9x
      @pilz9x 6 หลายเดือนก่อน

      ​@@MilanJovanovicTechhello, where I can get your new solution? Thanks

  • @ДаниїлТерентьєв
    @ДаниїлТерентьєв ปีที่แล้ว +1

    Thank you, Milan, for a useful video, discovered new approaches how to use distributed caching. But, you didn't show how to use prefixes in this video which upsets a little(

  • @milad.ashrafi
    @milad.ashrafi ปีที่แล้ว +1

    Thank you, Milan, for this tutorial, One question: why do you use IDistributedCache which will not support Atomic feature, instead of IConnectionMultiplexer?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      Because IDistributedCache is a .NET interface - and you can have multiple implementations of that. IConnectionMultiplexer is a StackExchange.Redis type

  • @chrismantonuk
    @chrismantonuk 5 หลายเดือนก่อน

    20:03 Very nice 😎

  • @salmanshafiq8151
    @salmanshafiq8151 8 หลายเดือนก่อน +1

    Great ❤
    But why Cache expiration is not used here?

    • @MilanJovanovicTech
      @MilanJovanovicTech  8 หลายเดือนก่อน

      Just omitted, you want to have it for sure

  • @diegomelgar2696
    @diegomelgar2696 10 หลายเดือนก่อน +1

    Hi @MilanJovanovicTech
    First, great video and really well explained.
    I have one question, if we want to update a cache key with it's value. Will SetAsync method do the job or is it better to create a new method like UpdateAsync()?

    • @MilanJovanovicTech
      @MilanJovanovicTech  10 หลายเดือนก่อน +1

      SetAsync - just write over the key

    • @diegomelgar2696
      @diegomelgar2696 10 หลายเดือนก่อน

      @@MilanJovanovicTech Nice, thanks Milan

  • @FrancescoLamoni
    @FrancescoLamoni 10 หลายเดือนก่อน

    I don't know if someone already say it... but i'll do it anyways... When you use the cache in the get products query handler, you check if cache result is not null then you return the product response... tecnically you duplicate a row of code, so the best implementation is checking if the cache result is null, in that case taking products from database, put them into product response, set the cache, close the if and finally (in any case) return product response... both methods works fine, but this is a bit cleaner

    • @MilanJovanovicTech
      @MilanJovanovicTech  10 หลายเดือนก่อน

      I prefer the early-return version I used here

    • @FrancescoLamoni
      @FrancescoLamoni 10 หลายเดือนก่อน

      @@MilanJovanovicTech yes, but if the if statement is not verified, it does early return anyway... it skip all the process

  • @mvasquez
    @mvasquez ปีที่แล้ว +1

    Great content, Milan. Thank you! One question, though, if the CacheService is injected as a singleton, is it really necessary to account for thread-safety in the internal data structure? Or can it just be an instance member of the singleton as there will be only one and its lifecycle will be managed by the IoC container, anyway? Do you see any disadvantages to this approach?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      I'd rather not risk it, even in that case. But in theory that should work, as long as you're still using a ConcurrentDictionary

    • @mvasquez
      @mvasquez ปีที่แล้ว

      @@MilanJovanovicTech Thanks a lot for the reply!

  • @antonmartyniuk
    @antonmartyniuk ปีที่แล้ว +2

    Looks very similar to the OutputCache in NET 7, which is also supported by minimal APIs

  • @matthewrossee
    @matthewrossee 10 หลายเดือนก่อน

    Is there a way to combine this caching technique with PagedList from your earlier video? I get an error about lack of parameterless constructor and when I create one, then the deserializer populates the object with default values, not the correct ones. I use microsoft's json serializer, not newtonsoft.

    • @MilanJovanovicTech
      @MilanJovanovicTech  10 หลายเดือนก่อน +1

      Add a constructor with all arguments and decorate it with JsonConstructor (or whatever attribute the MS serializer uses)

  • @YehorBachurinDev
    @YehorBachurinDev 4 หลายเดือนก่อน

    Thank you!
    Could you please share what model of keyboard you're using? Or is it a custom build?

  • @bahaaldeen3467
    @bahaaldeen3467 ปีที่แล้ว +2

    Can you explain how to use SignalR in DDD/ clean architecture, where does it fit and how to use it

  • @raminsolhi9288
    @raminsolhi9288 ปีที่แล้ว +1

    Thanks a lot, are awesome!

  • @milosmilovanovic3618
    @milosmilovanovic3618 ปีที่แล้ว +1

    Am I right to say that the implementation with static dictionary is only viable for the specific scenario of in-memory caching?
    Does Redis implement a mechanism like that, or it has a way of getting all of the cache keys?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      The cache keys would be scoped "per-service". In a distributed scenario, you'd have to do some gymnastics.
      There isn't a way to do delete-by-prefix in Redis. But I think you can fetch all the cache keys somehow, and decide which to delete.

    • @pilotboba
      @pilotboba ปีที่แล้ว +1

      @@MilanJovanovicTech You can cache your list of cache keys for a prefix in the distributed cache. It's inception. :)

  • @birajmainali10
    @birajmainali10 27 วันที่ผ่านมา

    How the `RemoveByPrefix` is going to work in case of horizontal scaling or in application restart?

    • @MilanJovanovicTech
      @MilanJovanovicTech  27 วันที่ผ่านมา

      Can't recall the exact operation on Redis to delete a set of keys. But we'd need to maintain a set of keys for a given prefix, and then delete that set.

  • @amirabenyahia1902
    @amirabenyahia1902 11 หลายเดือนก่อน

    Hello Milan , I wonder while searching about the choice of caching 's type for apis , I mean there's In-Memory caching,Distributed caching,Response caching ,what should I choose from these types if in my case if I’m just calling another external api in the Query handler and get the result ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 หลายเดือนก่อน +1

      Do you care about caching that result?
      - Yes: probably a memory cache
      Do you also have multiple API instances:
      - Then consider using a distributed cache

    • @amirabenyahia1902
      @amirabenyahia1902 11 หลายเดือนก่อน

      @@MilanJovanovicTech okay Thanks ^^

  • @alializade8531
    @alializade8531 ปีที่แล้ว +1

    Milan, thank you, this video is so amazing. Regarding the Distributed Cache Memory system on the Microsoft docs has been written that: "The AddDistributedMemoryCache should only be used in development and/or testing scenarios, and is not a viable production implementation." How can you explain it? Thanks in advance.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      I'd say it's fine for a single server, given that it uses in-memory storage. But it's not a *real* distributed cache

    • @alializade8531
      @alializade8531 ปีที่แล้ว

      @@MilanJovanovicTech Thank you so much for your reply.

  • @testtest-c4z
    @testtest-c4z ปีที่แล้ว +1

    Very good video, I congratulate you. I ask you a question, suppose I have an application with a mysql database, my app calls an api to query and show products to the user, if I implement you Redis to cache the busquedaby that the api first see if you have stored in cache and if not just then go to query the database, with the first user who enters the app Eedis stored in the server cache and then if another user comes and asks to see the products would be set in that cache even if they are different users? The cache has a time and then it is deleted and the search to the database is done again, right? Thank you very much

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      All users will see the cached value until it expires

    • @testtest-c4z
      @testtest-c4z ปีที่แล้ว

      Thank you very much for answering one last question. In redis you build tables to build the bbdd like in mongodb?

  • @MaximT
    @MaximT 8 หลายเดือนก่อน +1

    It wont work with multiple instances of the service. And there will be a growing number of unused cache keys in the dictionary? Who will clean them up?

    • @MilanJovanovicTech
      @MilanJovanovicTech  8 หลายเดือนก่อน

      You can use a Redis channel to send a "clear the cache keys" message

  • @mybestofriendo-hk7ud
    @mybestofriendo-hk7ud 4 หลายเดือนก่อน

    Hi Milan. Can you provide content about Garnet which is Redis alternative from Microsoft? Thanks for your tutorials.

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 หลายเดือนก่อน

      Will consider

    • @MilanJovanovicTech
      @MilanJovanovicTech  4 หลายเดือนก่อน

      Multiple state machines would mean multiple endpoints listening for messages

  • @thelastninja4825
    @thelastninja4825 6 หลายเดือนก่อน

    Isnt there a memory cache helper with tags? then just remove by tag?

  • @svetoslav.hadzhiivanov
    @svetoslav.hadzhiivanov ปีที่แล้ว +1

    Why not .NET 7's output caching along with Redis?

  • @sergiq2
    @sergiq2 ปีที่แล้ว +1

    Does Redis is free ? If I would like to host it on my own on Azure, should I use IAAS ? Azure cache for Redis is quite expensive feature for internal CRM app with less than 20 users.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      You can simply use IMemoryCache in this case

    • @sergiq2
      @sergiq2 ปีที่แล้ว

      Yes, of course, IMemoryCache is an alternative but this caching solution is hosted by the same process like web api, so when I have let's say 100k of emails in JSON format, it is a lot of memory which will be shared between this in-memory cache and server stuff. I think the IMemoryCache has different purpose (less data, expiration strategy etc.)
      Redis, like you presented, is hosted by separate process with own strategy about managing the memory etc.
      I wonder how many data in Redis means - enough for freeware version and how to host it on Azure.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      @@sergiq2 How much data is 100k emails? An email is at most 255 characters, lets say it's stored as a string at 8B per character. Worst case is ~2kB per email, times 100k = 204MB? Which is not a lot of memory

    • @sergiq2
      @sergiq2 ปีที่แล้ว

      @@MilanJovanovicTech not in my case. I have just prepared JSON files for 264 emails which I have got from the business (Subject, From, To, HTML body, dates, file name). The smalest file has 2480 characters and the biggest one has 219 000 characters. The average is about 45 000.

  • @shajareyetuba
    @shajareyetuba 5 หลายเดือนก่อน

    good job

  • @dilipmevada
    @dilipmevada 3 หลายเดือนก่อน

    Hi, Can you please share code repo of this video?

  • @el-tono
    @el-tono 9 หลายเดือนก่อน

    nice

  • @shilnp
    @shilnp ปีที่แล้ว +1

    Github repo for this code?

  • @harkiratsingh358
    @harkiratsingh358 ปีที่แล้ว +1

    It is very costly . Is there any cheaper alternative??

  • @phearomneang
    @phearomneang ปีที่แล้ว

    nice

  • @matthewrossee
    @matthewrossee 10 หลายเดือนก่อน

    10:14 What about other solutions?

    • @MilanJovanovicTech
      @MilanJovanovicTech  10 หลายเดือนก่อน +1

      Store them in the cache

    • @matthewrossee
      @matthewrossee 10 หลายเดือนก่อน

      @@MilanJovanovicTech You mean like this?
      var cacheKeys = await _distributedCache.GetAsync("cacheKeys");
      cacheKeys.Add("newCachekeys");
      await _distributedCache.SetStringAsync("cacheKeys", JsonSerializer.Serialize(cacheKeys));

  • @loam
    @loam ปีที่แล้ว +1

    Dude, give me your brain, please, I wanna make cool projects 😄😄

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      I'm sure you can do that as well without it 😁

  • @taner-saydam
    @taner-saydam ปีที่แล้ว +1

    Nice

  • @techpc5453
    @techpc5453 ปีที่แล้ว +1