Using Redis cache with ASP.Net Core Web API (Both running inside Docker containers)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to dotnet core central channel today I am going to build a asp.net core 3.0 Web API application where I'm going to use release as a distributed in memory cache provider for today's video there are three main things I'm going to cover number one what is 3ds cache and what are the different features offered is cache number two installing a release cache in a docker container and talked a little bit about the release CLI and number three building a asp.net core with API application running in a docker container and accessing the release cache for in memory distributed caching solution fast what is release release is a distributed in memory data store it can be used as a cache and that's where it's most popularities and that's what I'm going to use today but apart from that it can be used as a database given its supports transaction but in terms of database it can be used as a no sequel database given that it can have any kind of data structure which can be saved inside it the next feature which really support is also a pop sub engine so it provides method for publish and subscribe and just like any other Pops of engine there can be multiple subscriber subscribing to a topic or channel as it is called in reduce and then if publisher publishes a message into the channel all the subscriber will receive it I personally have not used treaties as a puff sub engine I have been using rabbitmq for a long time and I find it to be extremely lightweight and its user interface which is a web-based one is also very powerful and provides a lot of insight into the individual cues and topics the next thing that really supports is a stream it was introduced in version 5 of reduce and it uses the same philosophy as kafka stream in a sense that there can be multiple subscriber reading into a stream of data at any point in time and at any data point and they can go back and reread or do whatever they want as long as the stream is alive it also has the concept of consumer group which is first introduced in Kafka multiple consumers can read part of a stream I am NOT going to care deeper into stream as well so I personally have been using either Kafka or the AWS kinases stream but I can understand if somebody is already using 3ds as the data store as well as cash there might be an initiation towards using it as a stream as well so as a part of my next agenda let's go ahead and install really seen docker containers so let's start text I am going to download the latest docker image once the image is pulled and installed I'm going to create a container and start release instance I'm going to name it as local release can be named anything next thing I'm going to do is I'm going to export the port 63 79 which is the port release uses into the local machine so the container is created now I'm just going to do to occur PS to ensure that it's running yeah the next thing I'm going to do is just check the log and I can see that the radius is ready to accept connections the next thing I'm going to do is quickly go through a couple of features of the Reedy CLI so to open the really CLI what I'm going to do is I'm going to open the bash command inside of the container where it is is running and then open up the Reedy CLI so once I'm in the container I can just type really CLI and you can see the CLI is open now the next thing I can do is I can just do get ping and if it returns pong that means the radius is up and running so they just said to do ping yeah and I can get the pong response bags working as expected next thing what I can do is I can just set a cash value and get it back so I can use the set command and this is the same command we'll be using in the seashell code also it's just that it is going to be in c-sharp api instead of the CLI as you can see here there is a expiration as well which I'm going to discuss little bit detail when we get into the dotnet code but I'm not going to pass any expiration right now so it's going to be in the cache forever now I am getting an okay back which means the cache is sad now I can just do a CAD and pause the same key and here I get the cash back so the reduce cache is up and running and it is doing what is expected now the next thing is to create dotnet core application and then after creating the dotnet core application we are going to use reduce NuGet package and start accessing the cache so I'm going to go ahead and create SP dot nerdcore web application and I'm going to name the application as Rudy's demo I'm going to select API asp.net core 3 dot o and here I'll enable docker support I'm going to get rid of HTTP it's not needed at least not for the demo now the application is created next thing I'm going to do is let me just let the docker installation be done okay so container is created successfully now actually if we go back here we can see our today's demo docker is also created by the asp.net core web application now next thing I'm going to do is I'm going to install then you get package I'm going to search for release and here I have couple of option first is using the stack exchange dot release which has been downloaded 20 2.8 million times and then Microsoft extension for release and that one also internally uses stack exchange tour trees for the time being I'm just going to use the stack exchange or to reduce because has a bunch of API and I'm using the latest version so once it is installed the next thing I'm going to do is I'm going to go into the configure services and then configure the release but before that let's first create a controller let's create a controller and let's create a rewrite action controller name it as cache controller once the controller is created we will be using a CAD and a post but we will use just a single get with a single return and a post for creating the cache so I'm going to delete rest of the method not needed for the get also will change the implementation a little bit given that we will be passing the key instead of keeping an ID construct will just read the key from the query string and then for using the reduced cash first we have to get the high data base interface of release so in the constructor I'm going to create a dependency on I database of stack exchange rotaries and I did a base is nothing but the interface which is used for accessing the in-memory distributed data of tradies which in our case we are going to use it just for cash but same thing as I mentioned earlier can be used as a data base as well if you want to okay next thing I'm going to do is I'm going to do a database start and the method is going to be string yet and as you can see if I do a door it's like all possible methods are available in this interface every single thing that is supported by Redis we are going to keep our demo limited to just cash so this is the key based on key I am returning the string so I'll implement the post later or maybe less implemented so post is also very similar it just instead of CAD it is string sad as you can see and here instead of a string I'll take a key will appear and this is as simple as that now the next thing I'll do is I'll go back to the startup class and configure the I database and register it in the dependency injection container so that this dependency can be satisfied during the runtime so for that first I'll establish a connection to readies for establishing the connection to the Redis channel I'm going to use the connection multiplexer class and the connect static method of the class provided by the reduce infrastructure and to the connect method I'm going to pass the configuration which is nothing but the IP address of the machine where the Redis is running so fast what I'm going to do is I'm going to run this application this Web API application in my PC without a docker in which case I can just use localhost and connect to the release cache because I expose the port to the localhost but if I run this application inside of docker than something else has to be done I'll come to that later but first let's run as a localhost ok once the once the connection has been established the next thing I'll do is get the database from this connection and configure it to the dependency injection container so this will provide the database at this point in time since care database is returning an interface hi database this particular reference is not needed so I can just get rid of this but this is still returning the I did a base so now at this point if I run this application I should be able to connect to reduce and should work so as I mentioned first I'll run in localhost mode and start without debugging for simplicity okay so the app is running in localhost 5,000 which is the default port for inspirited web api now if I call the API slash cache and I passed the key which I initially added which is my key I should get test value back from released and I can see it's working as expected so this shows that it works you know as expected next thing what I'm going to do is now I'm going to just stop this from running and let me run these in a docker container and see what happens so once I start this in a docker container and I'm going to be in debug mode so that we can see the exception you can see that it's saying that it's not possible to connect unable to connect from localhost it's because we are trying to access a container in this box from another container in this box now to do that the simplest solution is we can do i pecan field and from the IP config we have this Ethernet adapter for docking net if we pick up this particular IP address go back here and provide this and now run mid back in shocker mode we can see it's working now it's running in five six five nine eight which is the port used by the docker container and if I do the same thing slash API slash I should get the velum now we see that the gate is working as expected now let's try to do a post and then we'll do a get again to see if the post walked properly and also we'll get into the CLI to check the same thing so let's first do a post and for the post I'm going to use the same URL make it as post and in the header I am going to add content type and I'm going to say applications lacerations and then in the body I am going to pass the key value pair so I'll say key is key underscore one and value is really simplicity so that's the key value I'm going to pass and then I'm going to make a send request and you can see I get a 200 back now let's fast log into the log into the docker container so we'll do again okay and then let's run the ready CLI okay now if we do a CAD and we pass key underscore one we get the reduce simplicity back so the post we can verify in CLI as well we can see it's working as expected next thing we can do is we can go back to the where what would who done do it in the postman as well but let's just try it here here we can say key underscore one and we get the really simplicity and you can see the our fast it is for obvious reason because everything is in memory so this is all I had to show today how simple and easy it is to use in a container and then access it from a dotnet core web api container this application now with this database I'm just doing string get sting string sad but you can use you know there are plethora of methods which are available in it and you can use all of those like if you want to set a hash or you want to you know deal with geospatial data and of course you can do things like publish subscribe which I have not even tried and but this is a handy feature if you want to keep your ecosystem small then you know reduce is pretty good with lot of thing we can use it for in memory cache distributed no sequel database we can use it as a pops up engine as well as a stream so you get four major feature which are used today in any distributed system in one application or one ecosystem which is I think very useful that's it for today if you have not subscribed to my channel please subscribe and if you like this video please give me a thumbs up and also I wanted to know from you what do you think about you know using the features like pops up which technology do you use today do you think it's what getting a deep dive into 3ds pop sub mechanism similarly for stream what do you use today and is it worth exploring the stream feature of radius so please provide your comment and feedback I'll see you next time thanks so much for watching
Info
Channel: DotNet Core Central
Views: 22,161
Rating: undefined out of 5
Keywords: .net core, asp.net core, redis, redis cache, distributed cache, in-memory cache, aspnet core, distributed in-memory cache, inmemory cache, asp.net core 3.0, .net core 3.0, c#, c# redis
Id: qYBaMFHeom8
Channel Id: undefined
Length: 20min 8sec (1208 seconds)
Published: Thu Nov 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.