The NEW caching you should be using in .NET 7

  Рет қаралды 78,023

Nick Chapsas

Nick Chapsas

Күн бұрын

Пікірлер: 142
@ExpensivePizza
@ExpensivePizza 2 жыл бұрын
There are 2 hard problems in computer science: cache invalidation, naming things and off-by-1 errors.
@b4singularity
@b4singularity 2 жыл бұрын
😂
@jamesjohnmitchell
@jamesjohnmitchell 2 жыл бұрын
There are 2 hard problems in computer science 0) Cache invalidation 1) Naming things 5) Async call backs 2) Off by one errors 3) Buffer overrun 4) Scope creep
@EdKolis
@EdKolis Жыл бұрын
The -2 billion main weapons of the Spanish inquisition are surprise, fear, a fanatical devotion to the Pope, integer overflow errors...
@johncunningham1928
@johncunningham1928 4 ай бұрын
thats actually 0011 problems
@LDdrums20
@LDdrums20 Ай бұрын
So true
@Szwarcus15
@Szwarcus15 2 жыл бұрын
Like learning from your videos. "Alexis Texas" made my day 🤣🤣
@theAutomaTom
@theAutomaTom 7 ай бұрын
I think she's more of a WCF gal, and less REST.
@siya.abc123
@siya.abc123 2 жыл бұрын
The web is about to get a massive performance boost ! This is awesome
@Dustyy01
@Dustyy01 2 жыл бұрын
Just an amazing showcase in this short video. Very very helpful for many ppl🔥👍
@OldShoolGames
@OldShoolGames 2 жыл бұрын
Great video as always, thank you for your effort !
@greenwoodricky
@greenwoodricky Жыл бұрын
Absolute legend. Just implemented caching and needed to evict by id and I was struggling to find anything anywhere.
@StuartHemming
@StuartHemming 2 жыл бұрын
Would love to see how this new stuff works in conjunction with Redis
@gregcyrus2739
@gregcyrus2739 2 жыл бұрын
Very useful for search results.
@antonmartyniuk
@antonmartyniuk 2 жыл бұрын
Fantastic feature. Will use it day 1 after release of .NET 7
@maciejgawe4407
@maciejgawe4407 2 жыл бұрын
Best .NET channel mate, short but full of info
@hassejansson
@hassejansson Жыл бұрын
How can can we use/implement this in a service class?
@alexclark6777
@alexclark6777 2 жыл бұрын
Not sure if there's a way around this when defining policies, but it would be nice if there was a "non-blocking" solution to when the cache has an expired entry that's being requested by the client. I wrote a simple cache mechanism a while back which would return the "stale" object to the client while updating the cache in the background when it determined (triggered by a request) that the entry had outlived its expiration date. The vast majority of the time the client didn't care that it was getting something that was (for e.g.) 61 minutes old, especially if it meant a rapid response. Often times throughput is more important than rigidly adhering to the policy's TTL.
@Kingside88
@Kingside88 2 жыл бұрын
Caching is great and I like the new feature. Also your'e presantation was great. But as always with caching: When the application grows and data became complex caching can open hard to maintain issues
@nickchapsas
@nickchapsas 2 жыл бұрын
That’s part of the fun of solution architecture. I’ve solved these problems many times and I’m always having so much fun coming up with days to deal with cache eviction and efficient caching
@jerryjeremy4038
@jerryjeremy4038 2 жыл бұрын
I really like your hacky methods
@EwanDobsonFan
@EwanDobsonFan 2 жыл бұрын
Very nice feature can’t wait to try this out
@avunda703
@avunda703 2 жыл бұрын
Hey Nick, and thank you for all amazing content! Any plans on making a video on structs? When to use them, what pitfalls to avoid etc.
@nickchapsas
@nickchapsas 2 жыл бұрын
Great video idea! Will add it in the backlog
@juanmiguel431
@juanmiguel431 2 жыл бұрын
Is it better having the IDs as guid or is it perfectly fine having them as integers ?
@sinoergin
@sinoergin 2 жыл бұрын
Great video dude. Also new caching system is look like powerful. Thanks for ur effort!
@sadaja25
@sadaja25 2 жыл бұрын
Hi Nick, super awsome content, as usual :)
@sergiom.954
@sergiom.954 2 жыл бұрын
Very useful, thanks for the video 👏👏
@expertdevs
@expertdevs 2 жыл бұрын
Great video as usual, keep it up
@20windfisch11
@20windfisch11 2 жыл бұрын
Is it also possible to use this cache in gRPC services?
@metaltyphoon
@metaltyphoon 2 жыл бұрын
Are Sick Chapnick and Chip Nacksas your bothers?
@nickchapsas
@nickchapsas 2 жыл бұрын
It’s indeed a big family
@zhebed1195
@zhebed1195 2 жыл бұрын
If i will use classical controller will I be able to use this caching in .net 7 or it is only working in minimal apis?
@NiamorH
@NiamorH 2 жыл бұрын
he showed attributes so I guess there will be support for regular controllers
@harindaka
@harindaka 2 жыл бұрын
Hey Nick. Apparently outputcache does not cache authenticated responses by default. The only solution I could find is to create a custom IOutputCachePolicy. Is there no way to get builder methods to make the base policy include/cache authenticated responses or a better way?
@jespereriksen270
@jespereriksen270 2 жыл бұрын
I'm dealing with this myself. And it seems that by adding a custom policy, i can't assign different policies to different endpoints. No matter how i tag / arrange the policies, i can only get the base policy to work.
@solero_
@solero_ 2 жыл бұрын
You should do a course about creating production quality APIs
@nickchapsas
@nickchapsas 2 жыл бұрын
There is something coming soon
@oksatoki
@oksatoki 2 жыл бұрын
@@nickchapsas please make it long enough and detailed if possible hahaha
@martinrj30
@martinrj30 2 жыл бұрын
looking forward to the ETAG episode
@catalan2857
@catalan2857 2 жыл бұрын
Hey Nick thanks so much for the awesome content! Could you tell which font you're using?
@sohampatel1063
@sohampatel1063 2 жыл бұрын
It is Jetbrain mono
@catalan2857
@catalan2857 2 жыл бұрын
@@sohampatel1063 No its not, compare by curly brackets
@baranacikgoz
@baranacikgoz Жыл бұрын
Is there a way to use 'Tag' with Controllers?
@michaldivismusic
@michaldivismusic 2 жыл бұрын
Shout-out Alexis Texas 👍
@EdKolis
@EdKolis Жыл бұрын
This looks really useful! Thanks for explaining it, it's not all that intuitive looking!
@mariomamalis
@mariomamalis 2 жыл бұрын
This is great. One question I have: we cannot really use this in more complex endpoints that have paging sorting filtering etc that would make each request very dynamic right? This is perfect for usage in simple endpoints that return either all or one record from the database. Is that correct?
@MrRclemens
@MrRclemens 2 жыл бұрын
Does the endpoint know which page the user requested? If so, you can use it as a caching variable
@ApolloJKD1973
@ApolloJKD1973 2 жыл бұрын
@@MrRclemens good point. The problem is that the same page can return different data if the sorting and filtering is different. I guess you can create cache keys for each combo. Not sure of this could become problematic if there are thousands of permutations but I hear you.
@EdKolis
@EdKolis Жыл бұрын
Couldn't the sorting at least be done in a different layer, or even client side?
@ApolloJKD1973
@ApolloJKD1973 Жыл бұрын
@@EdKolis not if you wanna sort the entirety of the data
@stefangranath3841
@stefangranath3841 2 жыл бұрын
Awesome stuff
@sohampatel1063
@sohampatel1063 2 жыл бұрын
It looks like this feature is removed because i am now unable to find those methods but previously i was. Can you please confirm whether it it coming in .net 7 or not?
@zapeweb
@zapeweb 2 жыл бұрын
Hey, Nick! Another nice content. I'm new to caching and have a question (maybe a noob one): in a 3 layer solution (UI + BLL + DAL), where should I implemente the caching? At the UI or at the DAL? Regards!!
@nickchapsas
@nickchapsas 2 жыл бұрын
You can have it at both for different reasons in each area. There isn’t a one size fits all solution. Generally UI layer caching will be the best performer because you don’t have to do any compute but it’s tricky especially with authentication and different users
@shioli3927
@shioli3927 2 жыл бұрын
What he showed in the video would be between your UI and BLL usually. As in API response caching. You can do the same for frontends, mostly. Unless you are using blazor client or something like vue for the frontend then this type of caching is irrelevant, you´d just keep global state (which is also significantly simpler, because you don´t have to worry about different users and such with a client side UI solution). Personally, I´d prioritize caching at the API layer like he showed unless the data is too large and transfer time is what you are worried about more than compute time (which probably means you would not want to keep it in memory on your server anyways). Caching at the API layer gives you the benefits no matter what client is being used (even if the answer is no client and the user scripted his own).
@danielcarmona7386
@danielcarmona7386 2 жыл бұрын
Great video! One question prob I didn't understand, how was this done before? Something implicit? Would you mind helping me find old documentation? Thanks!
@nickchapsas
@nickchapsas 2 жыл бұрын
It was a whole different feature. Check for “Response caching”
@sinamohammadzadeh1813
@sinamohammadzadeh1813 Жыл бұрын
Some of my requests are POST with some body parameters, How can I cache post methods and vary it by body parameters?
@pedroferreira9234
@pedroferreira9234 2 жыл бұрын
It's normal to use a combination of outputcache(distributed) and low level distributed cache right?
@nickchapsas
@nickchapsas 2 жыл бұрын
Yea totally normal. Some requests might be tricky to output cache, for example complex authenticated requests, so having caching on your data layer for example is totally normal and in fact, a good practice
@Punkologist
@Punkologist 2 жыл бұрын
Hi, I have a question about this that I haven't really been able to find the answer to. I have an endpoint that gets notifications for a user with no params at all like : Unread() It uses the token/principal to see who the user is and called the backend with that. This is to make sure only the authenticated user can get their own data. So I can't really filter by params etc.. How would I use this to cache that result, but then if a new notification is added invalidate the cache just for that user? I kind of want to set a tag at run time based on the user? Then I can evict that tag on the insert endpoint as I will know which user's data is being inserted. Edit: nevermind I have figured it out using your custom policy example to set the tag based on the email claim :)
@neilsg2001
@neilsg2001 2 жыл бұрын
Maui or xaml or DDD please 🙏
@friendlyfox2189
@friendlyfox2189 2 жыл бұрын
when I log in, the output cache is ignored, is there any way I can output cache while a user is logged in?
@pilotboba
@pilotboba 2 жыл бұрын
I assume this uses in process memory. How does this affect container memory configuration? Also, with a scaled out backend is there support for some second-level cache like redis or whatever?
@nickchapsas
@nickchapsas 2 жыл бұрын
Memory is relative to what you’re caching. If your responses are 50kb long then you can do the math based on the cache variations etc. yes this is extensible with Redis and there will be a dedicated video showing how to use it
@pilotboba
@pilotboba 2 жыл бұрын
@@nickchapsas Sure, I was curious if there was some upper limit which would automatically evict older cached items if there was memory pressure.
@stanislav7228
@stanislav7228 2 жыл бұрын
Hey Nick, thank you for the video! I have a small question about your example. In the POST handler, the service creates the customer, evicts the cache by tag, and you pass the cancellation token there (11:10). But what if the user cancels the request before the EvictByTagAsync method invokes, will it lead to the behavior when the customer was created but the cache is not evicted? Thank you very much!
@nickchapsas
@nickchapsas 2 жыл бұрын
Technically it is possible but it is very unlikely because in a real app you would have some sort of event that is being published and then a service to consume it and evict the cache from Redis. This flow is just for demo purposes
@testcloud3404
@testcloud3404 2 жыл бұрын
And what when we have several app instances? Is there any way to tackle that?
@nickchapsas
@nickchapsas 2 жыл бұрын
Yeap, you can use a distributed cache like Redis as the storage for the cache. It’s very easy and I will show it in a future video
@Anymn1
@Anymn1 2 жыл бұрын
The problem I always run into with caching is when you are dealing with mutliple webservers. Caching goes fine, but it's rather complex to invalidate the caches on the other servers. Any ideas for that?
@nickchapsas
@nickchapsas 2 жыл бұрын
Simply use a distributed datastore like Redis to store your cache data. There will be a video fairly soon showing how you can easily do that.
@maslennikovvaleriy
@maslennikovvaleriy 2 жыл бұрын
Maybe we can use VaryByValue? We have HttpContext there, so we can vary by value of our route value. 12:06
@nickchapsas
@nickchapsas 2 жыл бұрын
That will make the cache vary but it doesn’t give any context to the eviction method since you can’t pass a value from the vary method to the tag method
@florent9555
@florent9555 Жыл бұрын
You typically dont want to have an endpoint to return all the users. Imagine having 100k user, do you really want to return all of those? Could be quite memory intensive for the server that the webserver is running on. I usually have some kind of pagination by default for those kind of endpoints
@bipinchandrakant3085
@bipinchandrakant3085 2 жыл бұрын
Thanks for the awesome content. I tried to implement the similar approach in the Controller to set a tag but I don't see an option to set the Tag directly within the attribute. I created a new policy in options to set the tag name and bind the policy. WIth this I was able to evict the cache as expected. Is there an better way to set tag instead of creating a policy? [HttpGet] [OutputCache(Duration = 60, PolicyName = "AddUserTag")] public List Get() { return dataStore.Select(data => data.Value).ToList(); }
@bartoszpalmi5750
@bartoszpalmi5750 Жыл бұрын
I have the same problem. Did you found better solution?
@bipinchandrakant3085
@bipinchandrakant3085 Жыл бұрын
@@bartoszpalmi5750 No, I haven't found a better solution.
@benoit92320
@benoit92320 2 жыл бұрын
When do you come in France?
@nickchapsas
@nickchapsas 2 жыл бұрын
Are there any big .NET developer conferences in France?
@yitzchokneuhaus
@yitzchokneuhaus 2 жыл бұрын
Hey nick. Is it possible to integrate this with MediatR and repository pattern? Or it must coupled with your controllers?
@nickchapsas
@nickchapsas 2 жыл бұрын
This is more of an API Layer cache so by definition it should be coupled with your UI later, in your case the controllers
@mikaelsoderstrom2434
@mikaelsoderstrom2434 2 жыл бұрын
Great video! Is it possible to add "chained" tags in some way? Let's say that I have a list of users (UserGroup) and then I call DELETE /users/123 after which I would like to clear the cache for both "users" and "usergroups". I could of course make sure to do it manually, but is there anything built-in to clear both of them at the same time?
@nickchapsas
@nickchapsas 2 жыл бұрын
Yeah the WithTag method accepts an array so you can have as many as you want
@02244
@02244 2 жыл бұрын
Hi! Can you make video about .NET MAUI?
@nickchapsas
@nickchapsas 2 жыл бұрын
It’s not a topic that interests me so I won’t be covering it
@johnli7818
@johnli7818 2 жыл бұрын
just wondering Output Caching is out at .Net1.0 based on their website. How is this Caching different to that one??why do you call it new? I like you video. this Cache can really help my project.
@johnli7818
@johnli7818 2 жыл бұрын
btw, does this Output caching work with controller actions not the way you write it (not minimal API), would love to see another video about that.
@nickchapsas
@nickchapsas 2 жыл бұрын
You’re looking at the very old .NET Framework output caching. You can still use the new output caching with controllers with attributes
@johnli7818
@johnli7818 2 жыл бұрын
@@nickchapsas thanks for the reply. I can’t wait them to release the .Net7
@pagorbunov
@pagorbunov 2 жыл бұрын
Who is Sick Chapnick?
@astralpowers
@astralpowers 2 жыл бұрын
Will this have a redis specific implementation that will allow you to delete all cached items by tags? There is no way to remove cache item by tags with IDistributedCache (which only has remove by id) and this makes cache invalidation painful.
@nickchapsas
@nickchapsas 2 жыл бұрын
The Redis provider will be able to do this yes
@astralpowers
@astralpowers 2 жыл бұрын
@@nickchapsas Yeah, I just saw that there will be an IOutputCacheStore that does this invalidation. I hope this will support .NET 6 because it will be tough to convince my boss to use .NET 7, and I don't really want to implement this logic myself
@darkogele
@darkogele Жыл бұрын
How do i do this on controller based not minimapl api ?
@nickchapsas
@nickchapsas Жыл бұрын
Same setup but with attributes on the actions
@darkogele
@darkogele Жыл бұрын
@@nickchapsas For some reason is not working for me it keeps getting the data from the SQL.... but when i set it with the standard IMemoryCache is working fine.. anyway thank you for replaying Nick you are awesome :)
@xentricator
@xentricator 2 жыл бұрын
Hello Nick. I'm interested in using this with Redis, but noticed that IResponseCache is synchronous. Is there not an async alternative?
@nickchapsas
@nickchapsas 2 жыл бұрын
Response cache is different from output cache. I will be showing in a video soon how you can use Redis with this
@xentricator
@xentricator 2 жыл бұрын
@@nickchapsas Thanks for the clarification, I obviously got mixed up. I should have been looking at IOutputCacheStore which you will probably be showing in your next video on this topic
@gbelkin4
@gbelkin4 2 жыл бұрын
Hi Nick, thanks for video. Question: all of that is in-memory cache, right? So, if i want to use some distributed cache (Redis) for set of microservices instances, this approach won't help me, right?
@apr0l
@apr0l 2 жыл бұрын
5:24
@gbelkin4
@gbelkin4 2 жыл бұрын
@@apr0l oops, thanks
@vmachacek
@vmachacek 2 жыл бұрын
hello Nick, would you mind posting sample project to Github?
@nickchapsas
@nickchapsas 2 жыл бұрын
The project is available on GitHub for my Patreons
@vmachacek
@vmachacek 2 жыл бұрын
@@nickchapsas is there also code you presented as part of Minimal API @ NDC recently? I'm very interested to see how you put it together
@nickchapsas
@nickchapsas 2 жыл бұрын
@@vmachacek That's here: github.com/Elfocrash/clean-minimal-api
@AaronShumaker
@AaronShumaker Жыл бұрын
It's a shame query notifications aren't supported in Azure DB, and first class support for hooking .NET cache invalidation to them. It really is the perfect feature for it, but it's a pain to setup on a table by table basis end-to-end and locks you out of using Azure DB. App level cache invalidation falls apart as soon as you realize you've got other systems updating the same data. Ideally you'd have a data service that EVERYTHING absolutely must go through, but the reality is that in practice has limitations and performance issues for many use cases, and it's hard to get usage adopted universally throughout an organization. Query notifications solve alot of these problems because the database, where the common denominator is for updates, becomes the notifier of invalidations for the app level caches.
@urzaaaaa
@urzaaaaa 2 жыл бұрын
You cache like crazy and then IIS puts your Web app to sleep after 15 minutes :)
@nickchapsas
@nickchapsas 2 жыл бұрын
I have never used IIS in my life
@chanwengkin3122
@chanwengkin3122 2 жыл бұрын
in case you r using IIS, you can actually set it to always running to that it does not terminate or suspend
@Syntax-Savvy
@Syntax-Savvy 2 жыл бұрын
IIS are the old days but you can also configurebit away.
@MegaSuperhund
@MegaSuperhund 2 жыл бұрын
@@nickchapsas 😂😂😂😂
@Punkologist
@Punkologist 2 жыл бұрын
@@nickchapsas sadly some of us work in dinosaur companies lol. Although we are moving everything to Azure now.
@dimitris470
@dimitris470 Жыл бұрын
Interesting, but somehow it bothers me in terms of separation of concerns. The framework kinda goes into the trap that so many others go, trying to be everything in a tightly coupled highly integrated bulk. In the era of containers, I'd rather setup a specialized proxy like varnish if my app really needs caching.
@nothingisreal6345
@nothingisreal6345 2 жыл бұрын
Now this video already demonstrated the new caching in .NET 7. But I didn't understand what has changed compared to .NET 6.
@nickchapsas
@nickchapsas 2 жыл бұрын
It’s completely new. It’s not a change. It’s a new type of caching all together
@6stringmonk
@6stringmonk 2 жыл бұрын
As Nick says in the video, the existing cache tries to implement caching as per the spec and that can cause your cache to be bypassed in many common situations. For example, when a user hits F5 in chrome, it sends a Cache-Control: max-age=0; header and the caching middleware will not serve the request from cache. It looks like this new cache will behave more like you'd expect if want application output caching. If anyone is curious, there is a messy workaround for the old cache. You can strip the cache-control header from incoming requests before it hits the cache middleware. I had to learn about this the hard way ;).
@seannewell397
@seannewell397 2 жыл бұрын
Css or JavaScript paths? What you smokin. That's in my CDN not my API.
@nickchapsas
@nickchapsas 2 жыл бұрын
You mean gateway/reverse proxy. CDN is for static files. It can be but it doesn’t have to be. As explained in the video this is application level caching centralised in a distributed cache like Redis, with application specific rules. Nothing to do with gateway level caching
@bosegeorge1425
@bosegeorge1425 2 жыл бұрын
Nick it will be great if you can upload courses to udemy.
@nickchapsas
@nickchapsas 2 жыл бұрын
Unfortunately it wouldn’t be great for me since I would be losing money if I did that
@haterealm
@haterealm 2 жыл бұрын
Woohoo, Microsoft reinvented OutputCacheAttribute, that was around MVC 3.
@davidfowl
@davidfowl 2 жыл бұрын
Software goes in cycles
@emmepombar3328
@emmepombar3328 2 жыл бұрын
This is all good until you have multiple APIs running in a cloud. When API 1 updates a dataset A that is cached in API 2, you have a problem.
@nickchapsas
@nickchapsas 2 жыл бұрын
I made a follow up video on how to use Redis when apps are scaled out
@hassejansson
@hassejansson 2 жыл бұрын
I noticed a huge error in your data, you've entered the wrong birth year for Alexis, her birtday year is in 1985.. ;)
@ZaahidAdams
@ZaahidAdams 2 жыл бұрын
Shoutout to Alexis Texas
@LindenMikus
@LindenMikus 2 жыл бұрын
possible to use this on console app as well?
@k3daevin
@k3daevin 2 жыл бұрын
Sure
@gordonfreimann
@gordonfreimann 2 жыл бұрын
Alexis Texas 😂😂😂
@nickchapsas
@nickchapsas 2 жыл бұрын
Finally a man of culture
@01mrsir
@01mrsir 2 жыл бұрын
pagination??
@nickchapsas
@nickchapsas 2 жыл бұрын
You can cache based on page number and page size and invalidate the cache as shown
@maskettaman1488
@maskettaman1488 2 жыл бұрын
What? What about it?
@theAutomaTom
@theAutomaTom 7 ай бұрын
I don't believe that "opt-out by header value" works, man.
@umer.on.youtube
@umer.on.youtube Жыл бұрын
STOP USING THIS FOOLISH IDE. USE VS CODE OR VS
@berkanbilgin2287
@berkanbilgin2287 8 ай бұрын
Dude… for the love of the God…. You speak sooo fast and with that accent it really makes it so hard to understand.. remember you are making tutorials.. its like “bla bla bla bla bla” and you move so fast.. please consider these feedbacks for better outcomes. Thanks for the video
Stop using LINQ to order your primitive collections in C#
14:57
Nick Chapsas
Рет қаралды 98 М.
What's New in .NET 9 with Examples
25:02
Nick Chapsas
Рет қаралды 57 М.
黑天使只对C罗有感觉#short #angel #clown
00:39
Super Beauty team
Рет қаралды 20 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 36 МЛН
The New .NET 9 HybridCache That You Must Upgrade To!
14:34
Nick Chapsas
Рет қаралды 55 М.
Coding Shorts: Response Caching in ASP.NET Core
11:33
Shawn Wildermuth
Рет қаралды 8 М.
Intro to In-Memory Caching in C#
33:56
IAmTimCorey
Рет қаралды 81 М.
When You Shouldn't Use Await Async in .NET
7:51
Nick Chapsas
Рет қаралды 42 М.
How IEnumerable can kill your performance in C#
11:02
Nick Chapsas
Рет қаралды 119 М.
Getting Started with Dapper in .NET
23:42
Nick Chapsas
Рет қаралды 23 М.
Implementing In-Memory Caching in ASP.NET Core Applications
16:58
Why .NET's memory cache is kinda flawed
14:13
Nick Chapsas
Рет қаралды 56 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 260 М.
Cache Systems Every Developer Should Know
5:48
ByteByteGo
Рет қаралды 520 М.