Get the source code for this video for FREE → the-dotnet-weekly.ck.page/redis-cache Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
Be careful! while updating the cached value. Attaching to the DbContext is not enough. You must invalidate the cached value while saving (Unit of work). Otherwise all subsequent Get request will get the old Member value from the cache and not the updated one.
I can say for sure it is a a comprehensive explanation. thanks for everything you are doing. Can you start a playlist for unit testing, integration testing
Hey Milan Nice Explanation about redis and Distrubuted Cache , i was wondering how cache should/will work in case of filterting and pagination can you please make an video about this
Interesting about the change tracker for caching, it makes sense. But I havent thought about it so I will need to have another check. In my cached repository it updates as expected without using the attach call.
@@MilanJovanovicTech im using a memory cache and not Redis. Maybe its kept tracked in the memory cache, I will dig into this today and potentially remove the cache if that is the case. Im not calling Update or Attach.
Thanks for great tut! Is there any way to flush DB but stopping docker? Sometimes I want to make tests, and the thing is that redis db still contains X key, but I need to re-build it, is it possible?
Hi Milan, Thank u for the post. I have question. Should we handle caching in a Middleware? For example if we have something in cache then we return from a call right away. If we don't have anything in cache then we hit an entry point and on our "way back" if response is OK (200) we will write to cache. Thus we will not mix the busness logic with caching - something like a "decarator" pattern.
I’ve seen people put caches in repositories, mediatr handlers and even controller methods utilizing mediatr (in what looked like an implementation that assumed that cache was part of the “web” layer). I’m too lazy to research where this should fit and I don’t believe that we should always do the things the same way… context always matters. But I tend to put it in mediatr handlers.
@@alexlo5655 I’ve seen the Nick Chapsas video and it is indeed an awesome implementation. I wouldn’t bother using a decorator if there’s not a lot of business logic in the handler/service/controller. If all you’re doing is accessing your repository and querying data, I would probably add the dependency to the distributed caching right there and do the caching logic. But if you have complex business logic, than the decorator pattern seems a nice way to decouple even more.
Hi Milan, Awesome videos and explanation❤ Can you pls make video on the architecture, design and basic implementation on integration of different CRMs(Zoho/Dynamics, Salesforce, Pipedrive etc) into an web application keeping DDD in mind. How do you maintain consistency in DB, exponential backoff, retry mechanism, rate limiters etc....With EF and DB design. Or you can take example of different mailing services too like Gmail, Zoho mail, SMTP. It will be very usefull if you make a video from the System design HLD and LLD perspective.
hey milan i have a question if ef core caches data by default then using IMemoryCache with ef core is useless right ? i mean if ef core is already doing the job why should we use IMemoryCache?
Hello Milan! Isn't it recommended to secure the Redis service? In this way it can be accessed by anybody, right? Can you give me some advice on how to secure it?
I used the interface of distributed cache in a real project but i faced a lot of connection issues when we moved the project to azure cloud and bought azure redis cache so i had to switch back to stackexchange redis nuget to handle the connections better I hope if you can explain this issue and if there is a reason for it
I started to apply Redis to my APIs. This video is perfect, thanks. By the way, What's the name of that theme you're using on VS, please? It's better than the default.
@@MilanJovanovicTech I've been using it since I started programming in C#, and I never knew ReSharper had a theme. Thank you so much for your quick response. Wish you the best.
but it wont serialize entities with so much relations and circular references etc .... maybe it is better to cache to on a higher level like caching DTOs or view models ?
A bit shame you haven't explained why you put Redis into Repository ( DB Engines do have their own cache mechanism ) and not into Controller or QueryHandler.
Certainly not a shame, but a design choice. This is terrific well written code that when tested, can be considered productive ready. I don’t agree with caches in repositories either, but still this is a very good implementation for learning purposes.
@@wilsongarciawil Well, I disagree. A lot of code added whereas there is no gain. Look at response times in the previous video: ruclips.net/video/i_3I6XLAOt0/видео.html . I guess it will be even slower when you manually attach to change tracker ( vs direct to the DB Engine ). Hence it is shame, why Milan has not explain in the video this design choice.
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/redis-cache
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
Once again you provide a concise yet comprehensive explanation of everything you're doing. You are producing an excellent series of videos.
Thank you so much! 😊
Nice introduction! Next... handling cache invalidation/expiry!
We need to cover that topic!
Be careful! while updating the cached value. Attaching to the DbContext is not enough. You must invalidate the cached value while saving (Unit of work). Otherwise all subsequent Get request will get the old Member value from the cache and not the updated one.
I have to cover this in a future video
@@MilanJovanovicTech i need this case
I can say for sure it is a a comprehensive explanation. thanks for everything you are doing. Can you start a playlist for unit testing, integration testing
Great suggestion!
It was a very practical and complete training, thank you
Glad it was helpful!
Jako dobar i edukativan sadrzaj. Samo tako nastavi
Hvala puno! 😁
JsonSerializerSettings can be additionally stored as a private static field in order to not create a new instance with each request
Of course! Great suggestion :)
Amazing content Milan. Thank you for your time so much
I have so many great things coming, this doesn't even scratch the surface 😅
@@MilanJovanovicTech will follow you ;)
Hey Milan Nice Explanation about redis and Distrubuted Cache , i was wondering how cache should/will work in case of filterting and pagination can you please make an video about this
You just need to construct your cache keys to include the filter + page number
Interesting about the change tracker for caching, it makes sense. But I havent thought about it so I will need to have another check. In my cached repository it updates as expected without using the attach call.
How does EF know to track it without it being attached?
Perhaps you explicitly call Update before SaveChanges, which will get the job done
@@MilanJovanovicTech im using a memory cache and not Redis. Maybe its kept tracked in the memory cache, I will dig into this today and potentially remove the cache if that is the case.
Im not calling Update or Attach.
Thank you Milan..
Query : How to define expiry time or invalidate the cache.. could you please make separate video on to cover this.
You can pass it as another argument
Thank you Milan!
My pleasure
Thanks for great tut!
Is there any way to flush DB but stopping docker?
Sometimes I want to make tests, and the thing is that redis db still contains X key, but I need to re-build it, is it possible?
FLUSHALL redis.io/docs/latest/commands/flushall/?
Great video. One question : how to handle cache updates when the data has changed ?
Try this: ruclips.net/video/YdEbD53c8Bs/видео.html
Hi Milan,
Thank u for the post.
I have question.
Should we handle caching in a Middleware?
For example if we have something in cache then we return from a call right away.
If we don't have anything in cache then we hit an entry point and on our "way back" if response is OK (200) we will write to cache.
Thus we will not mix the busness logic with caching - something like a "decarator" pattern.
That's a good idea, and it's what I'm doing in the video. The CachedMemberRepository is a wrapper around the MemberRepository.
I’ve seen people put caches in repositories, mediatr handlers and even controller methods utilizing mediatr (in what looked like an implementation that assumed that cache was part of the “web” layer). I’m too lazy to research where this should fit and I don’t believe that we should always do the things the same way… context always matters. But I tend to put it in mediatr handlers.
@@MilanJovanovicTech ruclips.net/video/Tt5zIKVMMbs/видео.html
@@wilsongarciawil Nick Chapsas has a video on this
ruclips.net/video/Tt5zIKVMMbs/видео.html and this is what I mean about middleware.
@@alexlo5655 I’ve seen the Nick Chapsas video and it is indeed an awesome implementation. I wouldn’t bother using a decorator if there’s not a lot of business logic in the handler/service/controller. If all you’re doing is accessing your repository and querying data, I would probably add the dependency to the distributed caching right there and do the caching logic. But if you have complex business logic, than the decorator pattern seems a nice way to decouple even more.
Nice stuff thanks for explaining this in a neat format . What is the best pattern you recommend for creating a search engine api ?
What kind of search engine?
@@MilanJovanovicTech well basically a web search that searches from the content management systems
@@MilanJovanovicTech basically a content management web search
Hi Milan, Awesome videos and explanation❤
Can you pls make video on the architecture, design and basic implementation on integration of different CRMs(Zoho/Dynamics, Salesforce, Pipedrive etc) into an web application keeping DDD in mind. How do you maintain consistency in DB, exponential backoff, retry mechanism, rate limiters etc....With EF and DB design.
Or you can take example of different mailing services too like Gmail, Zoho mail, SMTP.
It will be very usefull if you make a video from the System design HLD and LLD perspective.
Sounds interesting, I'll see how I can fit it into my content list
Hi Milan, Nice video once again. ❤
What if the application has more than 200 permissions, should I go for jwt or other process?
What other process?
@@MilanJovanovicTech I mean, getting permissions from maim database or redis etc.
Great job!
Thanks!
Not much about redis, but other 'hacks' was great
Redis as an afterthought
Thank you, that was great
Glad you enjoyed it!
hey milan i have a question if ef core caches data by default then using IMemoryCache with ef core is useless right ? i mean if ef core is already doing the job why should we use IMemoryCache?
EF Core doesn't cache data by default. Where did you get that?
@@MilanJovanovicTech oh my mistake then i thought the change tracker do something similar to caching thank you for responding
What was the reason to use Attach? You just repopulate the entity in the context which is already in the database? What does it do ? For syncing ?
To make EF change tracking work properly. If we load the entity from cache, EF doesn't know about it
@@MilanJovanovicTech can you give an real world situation example why is it important?
Is it for syncing in between EF and Redis?
great video thank you
Glad you liked it
Hello Milan!
Isn't it recommended to secure the Redis service? In this way it can be accessed by anybody, right? Can you give me some advice on how to secure it?
Can't beat the docs: redis.io/docs/management/security/
Nice content!
Thank you 😊
Milan, what the difference between _context.Set().Attach(entity) and _context.Attach(entity)?
Both have the same end result, first one is more explicit
Thank you, Milan! Greetings from Russia, serbian brother🖐@@MilanJovanovicTech
I used the interface of distributed cache in a real project but i faced a lot of connection issues when we moved the project to azure cloud and bought azure redis cache so i had to switch back to stackexchange redis nuget to handle the connections better
I hope if you can explain this issue and if there is a reason for it
I'll have to try it out with Redis cloud and see
I started to apply Redis to my APIs. This video is perfect, thanks. By the way, What's the name of that theme you're using on VS, please? It's better than the default.
It's ReSharper
@@MilanJovanovicTech I've been using it since I started programming in C#, and I never knew ReSharper had a theme. Thank you so much for your quick response. Wish you the best.
If member is null you return null?
So the method should be name GetByIdOrDefaultAsync. As in somes comments you said :)
Thx a lot for your videos!!
Or we can just mark the return type: Member?
I think that's good enough
Thank you Sir. I have a question. is there any way to use client side caching with redis in dotnet?
What do you mean by "client" here? A web appliaction/UI?
@@MilanJovanovicTech
Yes. There is this feature in redis documentation.
but it wont serialize entities with so much relations and circular references etc ....
maybe it is better to cache to on a higher level like caching DTOs or view models ?
Caching DTOs is probably the right call here
very nice vide.
Can you make a video on duende server (identity-server 6) with asp.net core web api as backend and blazor server as frontend.
Perhaps, I'll add it to my content list
What was that decorate type?
Scrutor
How about caching DTOs? Is there any major difference
No, perfectly valid
Neat 👍🏻
Thank you!
Thanks
No problem
Why not using Polly.Caching?
I wasn't familiar with it, but I'll see what it can offer
A bit shame you haven't explained why you put Redis into Repository ( DB Engines do have their own cache mechanism ) and not into Controller or QueryHandler.
I definitely forget to mention things from time to time 😅
Thanks for reminding me, I'll see how I can make it right
Certainly not a shame, but a design choice. This is terrific well written code that when tested, can be considered productive ready. I don’t agree with caches in repositories either, but still this is a very good implementation for learning purposes.
@@wilsongarciawil I would probably create a dedicated CacheService that would have a simpler interface, but this is a good start.
@@wilsongarciawil Well, I disagree. A lot of code added whereas there is no gain. Look at response times in the previous video: ruclips.net/video/i_3I6XLAOt0/видео.html . I guess it will be even slower when you manually attach to change tracker ( vs direct to the DB Engine ). Hence it is shame, why Milan has not explain in the video this design choice.
The previous url is without timestamp -> ruclips.net/video/i_3I6XLAOt0/видео.html
Any cheaper alternative?
RAM?
There is! If you are able to create static singleton object. Store data in it. It offers faster rendering. Offers garbage collection.
👋
👋