A viewer requested a tutorial on using Redis with dependency injection without all of the ASP NET Core caching layers. They just wanted to work with it directly.
In this video tutorial, I'll show you how you can easily get setup to use Redis in your C# applications!
View Transcript
What if you wanted to use Reddus inside of your ASP.NET Core application without hybrid cache, without fusion cache, and without distributed cache? Hi, my name is Nick Cosantino and I'm a principal software engineering manager at Microsoft. In a recent video, a viewer had asked about being able to use Reddus without having to use all of those other things. They just wanted to be able to see how you can get up and running with dependency injection and Reddus. So, in this video, we're going to walk through a simple tutorial on how we can do just that. If that sounds interesting, remember to subscribe to the channel and check out that pin comment for my courses on dome train. That said, let's jump over to Visual Studio and see how to get started. All right, on my screen, I have modified the sample weather application. We'll be
walking through all of this code here. It's relatively straightforward. There's not too much to do to be able to modify it to be able to leverage caching from Reddus. But what I wanted to do was start with the Nougat packages. So I'm going to go over to the project file and we can see that I have stack exchange reddus extensions ASP.NET Core and then right below it stack exchange reddus extensions and then you can see system text JSON as well. Bringing in this package right at the top is also going to bring in Reddus for us. It's like a transient package reference that we'll get. But you can also reference that directly if you wanted to. But we need this right here to be able to get some of the dependency injection stuff taken care for us right away. Without that, we could go do
it ourselves. But I figured we could leverage the package because the maintainers have already done it for us. At the time of recording, you can see that I have these versions here. They're just version 11. By the time you go to do this, there may be newer versions. Things may be slightly different. So, please keep that in mind. We'll go back over to program.cs. What we're going to do is start off by talking about the dependency injection part right at the top. Now, it's really common in ASP.NET Core applications that when we're using this web application builder that we end up getting these extension methods that people who have packages they put together or generally if you're following tutorials and people are doing dependency injection, they'll create these fancy extension methods that allow us to say something like builder.services services or directly on the builder
go add or configure or do something but it's just an extension method and you can see that we are using add stack exchange reddus extensions inside here you can see system text JSON serializer there are other serializers available I just went ahead with system textjson to keep this one very simple but you can check them out online or if you search in the package manager you can find other ones as well this is going to be what is used for serializing the data that's going from our web application to and from Reddus if you have different performance characteristics you can consider that so just something to keep in mind that's what that means if you look at the next part we can see that there is reddis configuration being created right here so you might be wondering what's going on here are there other options
all of that and yes there are plenty of other options for configuring what I'm doing just to keep things very simple in this video is that we're going to set up the connection string. I could have just typed it directly into here cuz it's just going to be localhost. I'm going to be running a Docker container, which if you have not seen how to set up Reddus and run it locally on your own machine with Docker, you can check out this video up here because it's a quick one just to get you up and running. It's just going to be localhost, but I figured I'd go one step further just to see how we can load things from app config. But before we move on, this is just a reminder that I do have courses available on domain if you want to level up in
your C programming. If you head over to do train, you can see that I have a course bundle that has my getting started and deep dive courses on C. Between the two of these, that's 11 hours of programming in the C language, taking you from absolutely no programming experience to being able to build basic applications. You'll learn everything about variables, loops, a bit of async programming, and object-oriented programming as well. Make sure to check it out. Builder configuration get connection string and then reddus. So get connection string will look up a special named section inside of our app settings JSON which is just connection strings. It's not really that fancy. And then inside of there we will have a named connection string called reddus. To prove it I'll jump over to app settings JSON. You can see that I have connection strings called out here.
Reddus is the name and as I promised it's just localhost. If you want to run Reddus from Docker and have it routed through a different port, you can have the port here. So you can do that sort of thing, but I'm just running it on the default port. So just localhost will work for us. Let's jump back to program.cs. At this point, once we've added this code, we have dependency injection set up for us. That means that if we want to go using Reddus, it's actually pretty simple if we start using things like minimal APIs or just leveraging dependency injection in all the normal ways that we're used to. So to make a point of this, I'm going to go ahead on line 30 and expand the weather forecast route. Again, this is just from the sample project. So if you're like, what the heck
is all this code? If you've ever walked through Visual Studio, created a new project, it gives you this really fun little weather forecast app. All that we're going to do is take the value that we generate and cache it. And that means that the next time we come into this route, we can just go to Reddus, see if it's there, and return it. If you're interested in using this type of pattern with caching and all of that kind of thing, you can check out this playlist. This is where I have more caching tutorials. So, you don't have to go build it exactly like this. You can use the builtin stuff and wire up Reddus to that if you want. But like I said, the viewer that requested this just wanted to see how you can use dependency injection in Reddus. I just wanted to call
that out so you can understand the context. What we're going to be doing is on line 30, we can just ask for Reddus database. Right? There's an interface for this. I'm just calling it Reddus database. But if we go up to here, I'm just going to navigate to the definition so you can see where this is coming from. So this is inside of the extensions core abstractions for Reddus. Tons and tons of methods for working with Renis, which is awesome. So, just a little scroll through, right? Tons. We're not going to be using all those. All we're going to be doing is getting and setting. So, the first thing that we're going to do because when we have this here, the dependency injection is going to take care of that for us. Once we enter this method, which is the route for weather forecast, we
will get the i Reddis database thanks to DI. And from there, we're going to say Reddus database get a weather forecast array. And then it's keyed by weather. So this is how we look it up in Reddus. And it's really nice because it's a key value store. And because we hooked up a serializer, we're able to have that converted automatically for us. Leveraging that serializer. We ask for a key of weather, but what we get back is not a string by the time we're working with it. It's actually been deserialized with system text JSON because remember if I jump back over and we go look at the project here. This is what we were hooking up inside of our application. Once we have this called, we get back a weather forecast array if there was a weather key. Otherwise, we're going to get back null.
If the cacheed entry is not null, we're going to just write a console line. So when I go to demo this in just a moment, we can see that. and then we can return the cache value. Now, if we don't have an entry in the cache, what we can do is just write that to the console that we're going to go ahead and make one. This part right here is just whatever is in the weather forecast sample application to start with. Again, nothing very fancy there. Then what we're going to do is after we create the forecast array in memory, we're just going to say to the Reddus database, I'd like to add this forecast. Again, it's an array keyed by weather. Up here on line 32 and down here, 48, same key. So, we can get and set. And again, down here, the add
a async. Yes, it's a key value pair. Yes, the value should be a string. But we are getting all of that taken care of for us thanks to the serialization. So, we can just work with objects that can be serialized to and from strings. Great stuff here. So that way we don't have to go in all of the spots in our code that we want to do this, go write custom serialization. That would be a total pain in the butt. This get method right here is now wired up to use caching and it will print to the console for us. I would like to go demo this now. And just to call it out again, I have to go run the Docker image. So I'm going to jump over to Docker Desktop. I'm going to press the play button in the bottom right here. When
I do that, we get these optional settings. I like calling this out because it's easy to forget, especially if I'm recording a video and I'm not really thinking about it. I might forget this step right here where we have to have the host port mapped to Reddis running inside of Docker. So, let's go ahead and run this. We'll have a fresh instance here. We can see that once that's started, it says ready to accept TCP connections right at the very bottom there. Let's get rid of that. I'm going to go ahead and run the web application. Okay, so I have the console application running and I have Chrome pulled up here. Let's go ahead and make this a little bit bigger. I'm just going to type in weather forecast. When we do this, we should hit the route. So, I'm going to go ahead and
press enter on that. And we can see that over here we got some data back. So, we have our forecast, but I want to go expand this a little bit. We can see that we have a warning stack exchange Reddus extensions core, the Reddus client factory. So, we connected to Reddus and it's telling us that we didn't have a named Reddus configuration. So, it's just making one for us. And then we hit this line that said creating a new forecast. If I jump back over to the code, we'll get rid of co-pilot here for us. We can see that we didn't go into here because we would have seen this printed, but it said it's creating a new forecast, which hopefully means that we did add the item that forecast array into Reddus. pulling this back up just to see if that works. If it
is in Reddus, what we should find is that when I press enter here and we watch the console on the left, it should tell us that we're getting the cacheed entry. And the right hand side of my screen in the browser shouldn't change at all. Got a cached forecast. Nice. So, if you watch the right side and the left side, you can see it's exactly as I said, right? The right side doesn't change and we're always sending back the cached forecast. That's just a very quick video on how you can get set up using Reddus without having to use the builtin cached things that we have inside of ASP.NET Core, which includes the distributed cache, the hybrid cache, and if you like using fusion cache as well, you can use Reddus with all of those things, but I just wanted to provide a different way
in case you don't want to leverage those. The reason that I think this is important is yes, for this example, I did pick an ASP. core application. But if you were building something completely different, if you wanted to go build a console application or you had something that you wanted to use Reddus as a data store for some other reason, this is how you would be able to leverage this with dependency injection. So, I hope you found that helpful and I'll see you next time.