BrandGhost

Use Redis With HybridCache to Cache Your EF Core Data!

In earlier videos in the series, we saw how we could convert our Entity Framework Core data access from IMemoryCache to HybridCache! But... there was one big problem: It was still just a memory cache behind the scenes! Let's see how we can get Redis wired up to our HybridCache!
View Transcript
Welcome to another video in this series where we're exploring the repository pattern, ND framework core, Dapper, SQL, and different caching paradigms that we can play with. In this video, we're going to keep building on the previous videos, and the last one, we were looking at leveraging hybrid cache, but we were only using the local memory cache version of this. In this video, we'll be diving into the Reddus implementation that we can leverage. So, finally, it is going to actually act as a hybrid cache, which is really cool. So, we'll look at how we can set that up. A couple of different caching options. And I've also got my trusty assistant here. Never mind. I don't think he's actually going to be very helpful here. But Vinnie is going to be keeping me company in the background. Hopefully, he can jump in to help as needed, but we might not actually need him, especially if he's just going to be sleeping. So, let's jump over to Visual Studio, see what we got going on. I'll do a little bit of brief coverage from the previous videos, but if you've not checked them out yet, you can go check out this video next, and that way you can catch up and see how we got to where we are. Okay, to kick things off, I'm going to jump over to the project file, and we want to look at the different dependencies that we need here. So, in the last video, we were looking at adding in this package here, which is getting the hybrid caching option for us. And that means we need Microsoft Extensions caching hybrid package. And at the time of recording, I'm just using version 9.4 as you can see here. But in order to get Reddus working, we have this other package that's right below here. So you can see very similar naming convention, Microsoft extensions caching and then stack exchange reddus. And that's going to allow us to hook up Reddus here. I'm using this version here. I don't think that's going to be a huge issue, huge change across versions as long as they keep compatibility, but that's what I'm using at the time of recording. So, you're going to want to make sure that you have both of these. In the previous videos where we're setting up our repository pattern and the different data access that we want to work with, I am using Entity Framework Core for this video. In particular, I am using SQL light to go back that. But that's going to be sort of a side effect. The real focus here is going to be using hybrid cache. Jumping over to program.cs, we can see that at the top, this is a pretty small ASP.NET Core application. What I'm doing is calling this extension method to set up entity framework core with the hybrid cache. This is exactly what we talked about in the previous video. So go very quickly through it, but you can see that I'm adding in the dependencies. If I expand this one right here, this is where we get entity framework core added in with our SQLite database. Of course, you can pick any other database you want with Entity Framework Core. And then we can see here that I am wiring up our repository. Disclaimer for every single video I make with a repository in Entity Framework Core. I'm not saying you need a repository for it. People seem to hate this idea. This is just part of the series where we can swap out different implementations. But what we looked at in the last video was wrapping that repository with a hybrid caching one. So this is going to be using the decorator pattern. So that's what this is. But if I close this off, this just allows us on line six to get those dependencies set up. But we're not done yet because we need two more things. One is to add the hybrid cache. Again, we had this in the previous video, but if you just have what I have highlighted here from line 5 down to 14, this is going to give you a hybrid cache purely with the inmemory implementation. That's the previous video. That looks kind of boring if you've already seen the memory cache in action. But if we go down to line 15, thanks to the Nougat package we looked at adding here, this is going to allow us to hook up Reddus. Now, in order to have Reddus connected here, we have to do a couple of more steps. So, I haven't filmed it yet, but when that video is ready, you can check it out up here. I'll link it afterwards. I'm going to show you how you can get Reddus running locally. So, we'll see a little bit of that here, but not going to show you how to go get everything configured. We'll do a little bit more of an in-depth walkthrough, especially because you might have Reddus already hosted somewhere, and you don't have to care about that for here. But we can see here that when we have this uh Reddus cache part hooked up, we get options that we can go work with. And specifically, we need to set the configuration on these options. That's just going to be the connection string. So what I'm doing is off the configuration which if I go over to app settings JSON in the solution explorer you'll see that get connection string reddis connection string. So let's jump over to that. You can see that I have this connection strings uh section in the configuration. This is sort of a named convention for connection strings in particular. And then reddis connection string. I can give it any name I want here but I'm just using this. And then this is the default when I'm running out of the local Docker instance that I have on my computer. With that said, if I jump back here, this section here from line 5 to 18 is what we need to hook up a hybrid cache with Entity Framework Core wired up to SQLite and then of course having Reddus also hooked into that. That's going to be the basics. Beyond that, we have our repository pattern that I've covered in the previous videos that has caching wrapped around it. Before we continue on, just a quick note from this video sponsor, which is Packed Publishing. Now, Pack Publishing has sent me over another book from Mark J. Price, and this one is titled Realworld Web Development with Net 9. And I love Mark J. Price as an author. I have a bunch of his books sitting on my bookshelf now. One of the reasons that I love his books is that he has lots of practical examples that he'll walk you through. That means that as you're reading the book, you can build applications following along with him. He explains the nuances of what's going on. You can build things leveraging VS Code and other popular technologies. You'll get exposed to Entity Framework Core and things like that. But overall, Mark conveys ideas very clearly, very concisely, and has lots of examples to work through. So, I highly recommend this. This is again sent over from Packed Publishing, and Mark also has lots of other awesome books available on Amazon. Make sure to check them out. And back to the video. What I want to do next is just show you that I have Docker Desktop running here. And if I go back over to images, you can see that I have a Reddus image and then I have a container that's running. And if I click on this, you can see just kind of prove that it's running. You know, there's the console output for it ready to accept TCP connections at the bottom here. And what I'm going to prove now is that when we go run this, so back to Visual Studio, when we go run this in particular on line 11 and 12, I have different cache expirations set up. Couple reasons for this. One is that I just wanted to be able to show you that you have different configuration options. This is specifically on the hybrid cache. So I'm setting a in-memory cache uh sorry, this is the in-memory cache expiration, and this is going to be the uh sort of overall cache expiration. So the reddest part is going to be leveraging this one. Um so that's one of the reasons. The second is that when we go to actually run this, we'll see that uh I will be hitting the cache after 5 seconds still, which means it must be going to Reddus, which is the interesting part. But also, if we want to look down specifically at the Reddus options, there are a lot of other options that you can play around with. Just to kind of show you, I'm not going to go through everything. There's so many things here, but if you need to play around with your Reddis configuration, tons of stuff that you can do here. If you have questions about this, you can leave them in the comments. If there's particular scenarios, say your Reddis configuration. I am by no means a Reddus expert, but if there's something interesting that you have a challenge with, if you want to ask about it, it might be something I can dive into, make a video on it to try and help. So, just remember that if you have questions and I can try to answer them, that can help other people as well. I'll get rid of that though. This is what we're going to be working with. And I think at this point, what I can do is run the application. And then what I'm going to do is hit one of these APIs just to show you down here. Yes, they're all get methods. No, not what I recommend. It's just so I can open the browser and then refresh things quickly for us. But what I can do is get an entry. And I'll show you SQLite expert in just a moment. We'll get an entry and we're going to watch the console. And the reason we're going to do that is because when we get an entry and we miss the cache, Entity Framework Core will print out the SQL that it's using to go select that record. So that means it actually hit the database through Entity Framework Core. Makes sense. Now if caching is working regardless of the caching layer so inmemory or reddus what's going to happen is that it's not going to print the SQL that's going to the database and that's because it didn't have to go to the database that's good news. So the thing that's going to tell us in memory or not is the expiration right? So within 5 seconds, we're not really certain if it's hitting the memory cache or the reddis cache. And that's just because we can't tell. There's no other print statements involved. But after 5 seconds, if the local cache is going to expire after 5 seconds, anything between, you know, after 5 seconds and up to 15 seconds must be the reddis cache. So let's go ahead and run this application. I'll pull this up. I will dock this over to the side here. And now that I have both of these up side by side, just a quick note, this is going to be very bright when I open up SQLite Expert. You can see that I do have records in our database. The one that I have up in the browser, you can see ends in 7930. That's going to be this one here. You can even see that it from the previous run, I already have it. It's already pulled in the information and displaying in the browser. I just wanted to show you this is a record in the database. Okay. So at this point if I go press enter we should be able to see that we have the SQL output here. If I do it again and again and again see how it's not printing out the SQL anymore. If I keep doing it it's been more than 5 seconds at this point. If I wait just a little bit longer and we exceed that 15-second threshold. That means if I were to go press enter now we should go hit the database once again. And if I keep trying to access this, we are hitting the cache. So I'll keep doing it. Right, this has been longer than five seconds now. Now if I wait a few more seconds just to kind of seal the deal here one more time. If I press enter now, we should see that it goes to the database. And there we go. So in this particular case, we can see that it is using this expiration. And because this expiration is going to be the hybrid one, you can see gets or sets the overall cache duration of this entry past the backend distributed cache. So it is going to be going to the reddis cache. Now just to illustrate my point, what I want to do is comment out the reddis cache part. Right? So we have the local cache expiration which is 5 seconds and this one was going to be in particular for the distributed cache. So I'm taking out Reddus. We're still using a hybrid cache, but now it's only backed by the memory one. If we go ahead and run this, we should see that our caching is only going to be the part that is in memory. So, if I go run this, we'll hit the SQL database. If I run it again, we're still within 5 seconds, but now it's been about 5 seconds, and we go back to the database. We don't have that second tier of cache that has that longer expiration. This is purely going to be in memory at this point and that's why it's using that shorter expiration. So overall that's going to be how we can configure our hybrid cache and in particular adding in the reddis part. Again if you've watched the previous video on this this is extremely similar to when we had hybrid cache just with the memory one. The only new parts that we added in were right here where we specifically add in reddus and that just means that we need that nougat package. Aside from that, we just needed a connection string. Now, if you just wanted to wire this up as fast as possible, you don't even have to pull it from the configuration. You could have dropped it right into the code just to try it out. So, really, it's only a few lines of code to go add Reddus support in for your application. Now, that does mean that you have to have Reddus running somewhere. So, like I said, when that video is ready, you can check it out after if you want to see how you can run Reddus just like I did in this Docker container. And that way you don't have to have a really fancy hosting setup if you just want to be able to try this stuff out locally. One more quick reminder that if you were wondering about how all the caching's working, again, this is covered extensively in all of the other videos. I'm trying to build this playlist so people can jump through. But just a quick note if you wanted to see here is the hybrid caching repository. This one wraps around another eye repository. That's what that decorator pattern was about. It's going to wrap around an existing one so that all of the methods on our repository pattern have some type of caching. Right? So you can see cache getter create and then it just calls into the repository underneath. We do that for all of these different methods here. And that way when you're accessing the repository, it will have a pathway that asks the cache first or does something with the cache before going to the repository itself. So again, if you're interested in that, check out the other videos in this playlist. Start from the beginning and you can see how we build all of this stuff up. Thank you so much for watching and please continue to watch the other videos in the series if you found this helpful. Thanks and I'll see you next time.

Frequently Asked Questions

What is HybridCache and how does it work with Redis?

HybridCache is a caching solution that allows you to leverage both in-memory caching and a distributed cache like Redis. In this video, I demonstrate how to set up HybridCache with Entity Framework Core and Redis, enabling your application to utilize the strengths of both caching methods for improved performance.

Do I need to have Redis installed locally to use it with HybridCache?

Yes, you need to have Redis running somewhere, whether it's locally or hosted. In this video, I show how to run Redis in a Docker container, which makes it easy to set up for local development.

What are the benefits of using Redis with HybridCache compared to just in-memory caching?

Using Redis with HybridCache allows for a longer cache expiration time and supports distributed caching, which is beneficial for applications that need to scale. In-memory caching is fast but limited to the application's memory, while Redis can handle larger datasets and can be shared across multiple instances of your application.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload