Redis as a Primary DB using a .NET 6 API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one characteristic of a good api is one that is highly performant and responds well to our customers demands so as developers what can we do to ensure that we supercharge the response times of our apis well in today's video we take a look at one hypothesis that might just hold the answer [Music] well hello wherever you are whenever you are where am i melbourne australia as usual and when is it february 2022. so what exactly is it that we're looking at in today's video well today we're going to be taking a look at whether not the use of the word weather whether we should be using redis as a primary database with a.net api with a view to improving performance and really supercharging those response times interesting interesting question indeed now if you've not used redis before that's okay we go through everything including all the code completely step by step as usual if you have used redis before then i'm sure you've got a lot of questions for me and i'm hoping we answer all of those as we move through today's video before we get started just the usual housekeeping stuff uh the code is on github i'll put the links below and you can download the code if you want but i do recommend following along there is also a blog article a companion blog article so if you want to check out that instead uh go and take a look at that if you like the video please give it a like that just helps me out with analytics and all that kind of boring stuff and if you haven't done so already maybe consider subscribing to the channel thing the bell and you'll get notified of any updates this year 2022 i'm really really making a big effort to produce a lot more content last year maybe for obvious reasons it was a bit of a crazy year didn't make that many videos i'm hoping to change that this year so a lot more content is going to be coming out and you know if you want to be notified of that yeah just remember to you know subscribe for notifications other than that there's not much else to be said except i hope you really enjoy today's video and um yeah let's get started so yes hello and welcome to episode two of season five and yet today we're going to be looking at using redis as a primary database with a.net 6 rest api and really we're exploring the question whether this is a good idea or should we do it can we but we can do it that should be do is a good idea so that's really what we're exploring in today's video so this is what we're going to cover we're going to look at caching to begin with and why redis is used so much in that space it's synonymous with caching so while we're not implementing a caching solution today in code we're going to go over the theory and i feel that will give you a good background as to why we're attempting to do what we're doing today which is using it as a full-on primary database we'll cover red as data types a bit fluffy to begin with just kind of cursory to begin with but then as we move into the video and we start coding we'll look at them in much more depth some of them anyway not all of them we will set up redis in a container in docker now that is totally optional if you have another instance of redis running in some other way that's fine you don't need to use docker i'm just choosing to use it because it's a nice ubiquitous way to set up something and it's really simple as well and then the main part today's video which is the building of an api in.net using c-sharp with redis as a database so if you want to follow along with today's video you'll need the.net 6 sdk which is free you'll need a text editor i'm using yes code and i recommend vs code which is also free we're api client so we're building an api and we want to obviously test that api the two main ones or the two ones i'm most familiar with are insomnia and postman you can use either i'm going to use insomnia in today's video and then this is optional if you have some other way of running redis that's fine but if you don't and you want to follow along then you'll need docker or docker desktop and both of those are free and this is totally totally optional i am just using this tool as a teaching aid it's a graphical user interface for redis and if you've not used red as much before it it's a good way of representing how data is stored in redis i quite like it for that respect in that respect should i say it was called redis desktop manager i think they've renamed it recently to resp um i did pay for my version i just went onto the windows store it was like 15 or something but i think you can still get it free but again totally optional not recommending that you necessarily install it we'll be doing most things at the redis command line or in code so what is redis good question if you've not used it before so at its heart and this is this is an over simplification don't get me wrong it's a key value store okay you store data based on a key and and you store a value and you can retrieve data based on the key as well very much an over simplification when you start looking at the different data types it does get more complex but fundamentally that's what it is it's somewhat synonymous with caching because it's so fast and yes it has a reputation for being incredibly fast due to the fact that redis itself as an application is very lightweight it's very lean it's not a big bloated massive applications very light key value retrieval which is fairly simplistic you're not doing massive long sql queries with multiple table joins and all that stuff it's you know fairly lightweight key value retrieval and most importantly everything is stored in memory so retrieval is super fast let's take a quick look at some red's data types just to start to get you familiar we're not going to cover them in depth here we'll cover them in much more detail later so strings the simplest form of data that you can store in red is so you provide a key which is an identifier and then just a string of data and we'll work with strings in today's video any of the data types have an asterisk next to them are data types we're going to be working with so strings we'll look at you will hear about lists and list is a concept of a list for getting redis is a very common uh data structure within computer science and you'll i'm sure you've heard about popping things off this and putting things into this redis has a list data type that we're not using today but worth mentioning hashes we're going to work with a bit later i'm not going to say any more about hashes for now we'll cover those later sets similar ish similar wish to lists but the sets are basically unordered lists of strings and you can do all sort of set based not surprisingly operations on sets so again you know a very fundamental concept in computer science and we will be using sets and you have sorted set so again the basic set is unordered sorted sets are ordered and you might use those uh in a different scenario different use case we're not covering those today though more information here on data types and it's i'll say it a few times throughout this video the redis documentation i know people go i don't want to read documentation reddest documentation is excellent it's straightforward easy to read it covers everything it's not confusing awesome awesome documentation i definitely suggest you take a look at this um this page here this document here to look at the intro to data types really good i'll put a link below so redis keys are such a fundamental part of read this it's probably worth just covering them at a high level here but don't worry if you don't follow this it's not that complex but you'll see how they work in when we come on to coding so the binary safe which basically means you can use any binary sequence as a key you can store lots of data as a key but very long keys are not a great idea in fact the maximum size i think is 512 megabytes and that would obviously have a memory impact if you're using keys that were that size so red is don't recommend you use the maximum size and there may be some performance issues if you use keys that were that big very short keys are not a good idea either you want to make your keys balanced so that when you look at the key you kind of understand what it's representing fundamentally that's what you want to do you want them to be kind of self-describing so a good recommendation from redis is that they should be readable with some kind of structured schema so very common and this is the pattern we'll be using today is the object type that you are working with so books or users or in our case platforms call on followed by some kind of unique identifier so in this case on screen you can see user colon and the number so you can look at that and go i can understand what that's representing and you'll see how all that manifests as we move through today's video all right so let's come on and take a quick look at caching because to discuss what caching is in the context of red this is actually a good introduction to asking the question could we use it as a primary database as well so let's go through a few scenarios where caching is not and is involved so let's take a look at a very simple topology where there is no caching involved and you'd probably be familiar with this topology if you've watched any of my videos it's kind of what i usually do so we'll have a client app in the context of today that would be insomnia or postman or even a web browser we'll have our api which we will be building step by step today and we have a database so think of a traditional sql database like sql server or postgres or something like that so the client will reach into the api or reach out to the api to fetch some data the api in turn will then go to the database to retrieve that data there will be some database fetch time while the sql query executes and the data is retrieved from possibly on disk possibly not but you know while the data is retrieved from the database that data will be returned to the api and then the api will in turn return it to the client app so we have a database fetch time and we have also a total request time that you take into account network latency and all that sort of stuff very simple very straightforward what's wrong with it nothing nothing's wrong with that in the context of a demo application or a very small application this will work fine in the real world in production environments as your data sets get larger and larger and larger and you get more and more users this is possibly going to start to break down a little bit and slow down a bit and so what you will want to do in terms of one of your optimizations is introduce a cache and the point of cash is to store data frequently frequently use data by the application that doesn't change that much so the data itself doesn't change a lot so it might be like a list of countries or something like that that is still frequently used by the application and so cache can serve up data much much quicker than the database can so let's take a look at that so we've got the same three actors this is our no hit cache example and i'll explain what that is by going through the example so let's introduce our fourth actor or our cache in this case redis and so same as before the client talks to the api to get some data the api in this case attempts to get the data from the cache as in the first instance because it wants to do that because it believes it's going to be a quicker response time in this case we don't get hit the date at once is provided a key to get the value back it's not there there's no hit so it has to resort to the same cycle that they used in the last last slide where it reaches out to the database there's a database fetch time the date is returned we'll probably want to cache that data for future use in case we get another request for it and then the data is returned to the client app and of course we have a total request time now these timelines are slightly exaggerated to be honest with you the total request time on this slide versus the previous slide is probably not going to be that different all other things being equal because the interactions with the cache are really so very fast that it's going to be quite negligible i think the total the response times will request time will be slightly longer though so let's move on to our last example where we get lucky and that is our cache hit so client app talks to the api api goes to the cache in this case this scenario hooray we get a cache hit there's this catch cache fetch time which is really small the data is returned to the api and then the data is returned to the client and then we have our total request time so very similar interaction to what we've seen before the only difference is that the amount of time it takes for the cash to respond is greatly greatly reduced all right so it does beg the question do we need a database and we asked that question because if we take a look at our very very first example where we only had a database you'll see the interactions that you're very familiar with and we look at our example where we had a cache hit and let's say the database played no part in it you'll actually see the interactions are exactly the same the only difference is that on one's example we have the word database and on the other we have the word cache so it starts to get us thinking could we just use the cache as a database so let's take a look at that so red this is a primary database which is what this whole video is about so redis does more than act as a cache it can also operate as a database which is effectively what it is doing it's a key value store again i'll keep coming back to that it's just a key value store and so it can work as a cache it can work as a database it can even work as a message broker although we won't be exploring that today it does have a reputation and rightly so as an in-memory store but that can mislead people into thinking that they would not want to use it as a primary database finds a cache because if you lose the data in your cache well you just go back through that cycle and go to the database because that has got persistent data the thought of using redis as a database is a bit worrying for some people because they believe that they will lose data should the redis instance reboot or something like that happens to it the main point i really want to get across in today's video is that redis does offer a number of approaches to persisting data that we're not actually going to cover today we're just going to use the automatic setup that it provides out the box when it does persist data down to disk as well but that does not change the fact that the data is predominantly stored in memory still and it is still super fast but what it does mean is if we do reboot the instance you have not lost your data and therefore it's a bit of a tick in the box to say yes it probably is at least worth exploring it as a candidate to use it as a primary database and we'll explore that theme throughout the rest of the video so yes as it says there you don't lose your data if redis restarts now i do want to just paraphrase chris rock a little bit um and he has said something along the lines although there's a few bits of profanity in there that i won't replicate here and he said something along the lines of just because you can drive a car with your knees or steer a car with your knees doesn't make it a good idea so again just because we maybe could use redis as a primary database does it make it a good idea you might technically be able to wrangle it to do it doesn't make it a good idea we'll answer that question at the end of the video as we move through and maybe the answer is yes maybe the answer is no but again just want to bear in mind that just because something is possible may not make it a good idea but we'll come back to this question as we move through the rest of the video now just to round up before we move on to actually starting coding i just want to go over the application architecture very very quickly we have our client which is external to our application domain we will have our dotnet six api service and moving top to bottom we will have a controller we'll house a number of actions and i'll show you those on the next slide they'll be the api endpoints that we're going to implement we're going to have a repository which is basically an abstraction of our data if you like we will have a very simple model which is an internal representation of our data basically and then we are going to have a something called an eye connection multiplexer and we'll come on to that when we actually move on to coding we're going to leave it there for the moment but i just wanted to put it on this slide and then we'll have our redis database now one thing i want to call out that i would usually put in a video like this is i would usually make use of something called dtos or data transfer objects and if you're interested in what those are then take a look at my other video on rest apis basically they are an external representation of our model so we have a model there models really should just be used internally within an app when we want to serve data out to the great wide world we should basically use our external representation of that and that's what a detail is in today's video we're just going to be serving our model which is not really best practice but in the interest of time we're going to do that i just want to flag it that we're not using details as it says there we are going to be using the i connection multiplexer we could use something else called the i distributed cache but i'll cover that in more detail when we come on to coding a bit later and just before we move on with coding i did want to just cover off the three end points that we're going to implement today now before i even do that i want to just cover off what our api is going to allow us to do and that is work with platforms what's a platform a platform can be something like sql server or redis or docker it's really just a list of platforms it's like an application register that's all super simple could be anything could be books could be cars again doesn't really matter but yeah that's what a platform in the context of these endpoints relates to and we are going to have three endpoints the first one as you can see on the screen there is the ability to retrieve an individual resource or platform based on its unique id or key second endpoint is creating a resource in our database and then the third endpoint is retrieving all our endpoints with all our platforms everyone that's in the database now you'll probably immediately think there's a couple endpoints that you could have also put in there which is deleting a resource and updating a resource and you're absolutely right could have done that reason decided not to two reasons first one is time but more importantly the point of today's video is attempting to use redis as a database and these three endpoints that i've selected really help us to exercise uh or you know test out that hypothesis because we're going to have to use different data types to kind of achieve these api endpoints here by adding update and delete we're not really going to be investigating any more around reader's data types and if that doesn't really make sense wait till we get on to these three endpoints and we implement them and we'll hit a bit of a brick wall and we have to reverse a bit and re-engineer it you'll then come to realize why i've decided not to do update and delete it's not going to add anything to our conversation if you want to though and by the end of this video you should absolutely be able to implement those endpoints yourself if you want just not going to do them in today's video and so with that the introduction is over still going to do lots of talking but it's now time to start coding all right so let's begin coding so the first thing i'm going to do is just open a new instance of visual studio code which will open on my other screen so let me just pull that on to my screen that i'm recording on and there we go and i'm going to use the integrated terminal in visual studio code so control back tick that's the backtick key is the one under well on the us keyboard anyway the key under the escape key so i will move into my working directory to begin with which just happens to be on my d drive so let's our d drive season five and this is episode two now you can obviously put your project anywhere that you choose i'm just putting mine there so before we even do any coding let's just make sure that we have the dotnet framework installed so net dash version and you can see that i have version 6 of the framework installed and again for this video i strongly recommend that is the version that you use just to keep things simple we can then do a dot net new dash dash list to see a list of the possible templates we can use when we want to scaffold up our app and we are going to be building a an api web-based api so no surprises this is the template we're going to use now in all honesty to be really truthful with you we could just as easily go for the empty template because we're going to delete most of the scaffolded code but look it's a very lightweight template anyway so we'll just go with that so clear the screen bring the terminal back down and we will do a dot net new template then sorry template name web api and we'll give it a name um let's call it redis api and hit enter and that was incredibly quick so we do a directory listing you can see we have a folder in there so if we just type code redis api and use the r flag and that will recursively open that project in our visual studio code instance so i'm just going to trust this and we'll probably get a pop-up down here where that resolves some dependencies we'll just wait for that to happen and that's happened already that's fine i thought it would take a bit longer than that i want to be go it did take a bit longer um yeah just say yes to that fantastic so moving into our terminal we will just run this up just to make sure it's all working it would be highly unlikely that it shouldn't be working but you never you never know always worth checking these things and what you get out the box is a very simple weather forecast api so here you can see the model the model class that we have which just has some temperature attributes and a data attribute and we have one controller the weather forecast controller and we're going to delete all this stuff in a bit of a later section we'll leave it here just now and yeah we just have one controller action weather forecast so get action and yeah we just get some randomized weather data so let's just use a web browser for the moment just to make sure that's working we will move on to using postman when we move through the majority of the course so let's just take a look at the https endpoint and just a point of note i've mentioned this before i'll mention it again in dot net six the port numbers for https and http are slightly randomized most actually all https ports begin with a civil now and all http ports begin with a five but they are different every time you scaffold up in your project so just copy that and we'll paste it into our browser edge in this case and again i'm just doing this to make sure it's all working and we'll just hit the controller and then there we go we get some adjacent data back so just doing that to check that it's all working which of course it should be there's no reason why it shouldn't okay so we'll just do a control c on that now that's really the our project scaffolding up again there's no startup class because we are using.net six everything is in the program class and same principles though you set up your and register your services up here and then you configure your request pipeline down here so a bit more simplified without having a separate startup class all right now we're going to obviously obviously we're going to come back and work work exclusively within this project but the next thing i want to do is set up redis in docker and in order to do that we're going to use e docker compose file and we're going to put that docker compose file into this project so we'll park this project kind of for now and we'll come on to standing up our redis instance in docker so we'll do that next all right so we're going to come on now and use docker to run up an instance of redis that we can use throughout the rest of the video so you'll obviously have to install docker i'm not going to take you through how to do that it's fairly straightforward at least on windows and mac os you'll install docker desktop linux users i'm not going to tell you how to install docker because you're so intelligent if you're choosing to use linux i'm sure you know what you're doing you don't need me to tell you how to install docker and yeah docker desktop on windows and mac is very very easy so assuming you've done that the best way to check to see whether it's running or one of the ways you can check to see that it's running raw is at a command prompt just type docker dash dash version and you should get the version returned back to you now just on the subject of installing it i will make a special call out to windows users and my recommendation in fact you need to do this if you want to follow today's video is when you come to install docker desktop for windows and you get a choice of possibly using windows based containers or linux based containers make sure you go with the linux based containers it's just a much better install pattern and it yeah you'll save yourself a lot of problems believe me so go with linux based install and if you're anything like me you will use the windows subsystem for linux as your kind of linux backend that would be my high high recommendation for all windows users and in fact windows home users you'll have to go down that path anyway but for me it's still the best option so that's the only points i'll make about installing mac os and linux users you will be fine cool so we have docker up and running that is looking good so the next thing we need to do is create what's called a docker compose file and docker compose is really it's a nice way of scripting your containers you're running containers if you don't use docker compose you'd have to do everything fully at the command line and some of these command line prompts can get quite unwieldy subject to error not really repeatable so docker compose is a great way of basically scripting all that so in our project right click new file and we just call it docker dash compose.emil and you should see a little pink wheel next to the docker file fantastic so the first thing we need to specify is the specification that we're using and anything above three should be fine and i think at the time of writing on the time of making this the latest version was 3.8 it doesn't really make any difference to be honest with you certainly not for the way we are using it then we want to specify our services and our services are basically just a list of the containers effectively that we want to run now you could have multiple services in here you could have redis sql server postgres rabbit we are just having one service which is redis now you give it a name can be anything you like i'm just going to give it the obvious and call it redis and then we specify the image that we want and this is the image that contains our redis application and if you've not done much with docker basically an image is like a blueprint for what you want to run and yeah that's basically the power of docker you know you've got these pre-canned images you just ask for one that you want spin it up and dockable just run it and you know because it's running on docker you you very very very rarely get any issues with just spinning up it's awesome docker is one of my absolute favorite favorite tools as a developer so you just specify the name in this case redis now what will happen here is if i have the redis image in my local machine that will just be used and this will be super quick if i don't have it then docker will go to the docker hub which is just like an online repository of images and pull it down for us now i can't remember if i have read this in on my local machine we'll find out when we run it up if it runs really quickly then i do if it takes a while then we know it's pulling it down but you'll see you'll see that happening at the command prompt anyway you can give the container a name and that can come in a bit useful later so let's just give it a name and we'll call it redis api it can be anything again and then finally we're going to specify some ports and ports are really important because in order for us to connect into this running container from our api or from some other tool we need to have an external port mapping that's mapped through into an internal port and that's really what we're doing here an internal to external port mapping and the internal port is the standard port that's used for most redis installations which is switches 6379 and just to keep things nice and simple i'm going to map that through to the same port allocation externally and that's the port we'll use externally when we come to connect in so we'll save that off and that's it very simple just be careful with your indentation it can get quite permickety about that if you have problems the code is on github you can just pull down my docker compose file and use that if you have any issues but you should be fine it's pretty straightforward fantastic so make sure that's saved off and now we're going to do is run up and this is where we find out whether we've got the redis image on my machine so just before we do that do a directory listing and make sure you can see the docker compose file in your local scope as it were and then at command prompt just type docker compose up to spin up our containers and then optional but highly recommended dash d and what that means is we're running this in a detached mode so when the container runs up it returns back as you can see here returns back the command prompt to us immediately if we didn't have the dash d flag you'd have all sorts of output from red as red just keeps control of our command prompt but we've not done that now you can see that was super quick you saw how quick that was so that obviously means that we have the red this image on our local machine if you do this and it's the first time you're using docker well absolutely guarantee it will take a while for docker to pull the image down and it shouldn't take too long okay couple of minutes something like that fantastic so although it's saying our container has created and started i'm going to show you a couple of commands and a couple of tools you can use just to validate that so quick clear the screen and if you type docker ps that will give you a list of running containers running containers and you can see here that we have one and you can see you know a lot of stuff that looks familiar to us if you don't particularly want to do a lot with the command line that's fine what i would recommend you do is install this plugin for vs code docker plugin so if you come over to your extensions tab i've already got it highlighted here just type in docker it's from microsoft it's got 13 million downloads it's a really nice v tool i use it all the time and it just puts it in your toolbar here what you will see are the running containers and you can see here we have one running container and then you can also see all the images that we have now you can see i've got a postgres image a rabbit nq image and probably a whole heck of other stuff as well and that's why it was so quick i already had the image there um that's cool so it gives you a nice overview of what's happening with docker now we can stop this at the using the gui so right click stop and that will stop that running container and if we go back to our command line and do a docker ps you'll see nothing is running so we can either go here and run it here again so docker start or we can come back here and we can do a docker compose up and it will spin up our container again it's running again you can see the green arrow here now just a couple of command docker compose prompts for you if you type docker compose stop any guesses for that it will of course stop the container and you can see there the container is still there it's stopped now let's run up again there's another command i probably even shouldn't show it to you but i've fallen into this trap colleagues of mine have fallen into this trap because you use the docker compose up command to spin containers up just in your mind you kind of think quite naturally that the opposite of up is down so if you type docker compose down totally valid command but if you keep an eye on what's happening in our container section there you'll see what actually happens here and you can see not only does it stop it it completely removes it okay so that's fine if you want to do that but my recommendation to you is to use docker compose up and docker compose stop and the reason for that is well if you are the main reason for it is you don't want to destroy your running container so you might and again we'll do this have data stored within your container and so if you do a docker compose down it completely blows all that way because docker compose um stop uh it retains it and you just need to restart it now those are docker compose commands you could also use a native docker commands i'm not going to go into today my other videos do cover that but i think between those docker compose commands and the tool if you're using bs code i think i think we've got it covered fantastic cool so docker compose is done we've got a running version of redis which is fantastic so just before we come back to our project and start coding what i want to do is we're going to dip our tool very slightly in using a couple of redis commands and those commands allow us to add data or set data and also retrieve data we'll do a couple of commands super simple just to get you across the concept and then we'll move into coding and we'll actually make use of those commands in our code all right so what we're going to come on and do now is use a command line tool a redis command line tool or the redis command line tool to issue some commands to create and retrieve data just using the command line so we're not going to be using code or anything like that just yet now in order to use the redis command line we could install it locally that is one option certainly but i'm going to take a slightly different approach and that is effectively to connect into our running redis container and basically attach a command prompt to it so we can just run commands directly inside that container and obviously because we have redis running in that container we also have the redis cli tool installed as well so it just means we don't have to install anything locally so in order to get going with that let's just get our containers id and the way you can do that there's a command we have already used which is docker ps to get a list of running containers and we have our container here and this is what we want this container id so we'll just copy that and then we type the following command docker exec we want an interactive session we paste in the id of our running container and then we just specify the path that we want to kind of start our command prompt draw effectively or command prompt in and you don't need to be a linux expert to appreciate that the path or space specifying there is a linux based path no surprise because it's a linux based container so let's say enter and we have now attached into that running container so we're now really running in a linux container effectively and all we need to do is just start the redis cli command line tool and all we do to start that is type in red as cli and it would help if you spelt it correctly there is cli there we go and the way you can kind of test and that is working apart from the fact that the command prompt has changed if you just type ping we'll get a pong message back so that's all looking pretty good now just to label the point if i just bring over our command prompt this is like a windows command prompt powershell command profile type redis cli it won't be recognized because i don't have it running locally so we're running in our container all right fantastic now before we move any further with doing anything else i want to just come back to the concept of red as data types because it's worth just mentioning that we are going to be working with the arguably the simplest data type in this exercise and that is string so let's just have a quick look at what we mean by strings so it's the simplest type of value that can be associated to a key and it's really just a one-to-one mapping between a key and a value couldn't really be much simpler so if you're used to using json actually json is more complex this is even this is even simpler it's literally a key and then a value a one-to-one mapping there's no nesting or anything like that so jason forget jason that's a bad use case it's literally just a yeah a mapping that you can see there on screen useful for a wide range of generic use cases i'm sure you can imagine and this is really what we want to get onto you can set data using the set command and that's the the format that we will run in just a second we will issue the set command at the redis cli with the key the redefine and some value and then you can return data using the get command and you just say get and specify the key and that will be returned back the value will be returned back if there is one of course and just we'll come on and do it actually rather than looking at slide well let's get going with that and we will start specifying some data so let's set our first value and the good thing with the redis cli is that it kind of gives you this help along the way so we're issuing a set command we are working with platforms so again if you remember from the slide where the typical schema that you would use for your key is the name of the object or entity that you're working with in this case a platform and colon and then the actual kind of numeric key value and you just give it anything like here and then a value let's make our first platform as docker enter be getting okay come back and then to return that piece of information we issue the get command and just type in the key and we get returned docker fantastic now before we go any further i just want to show you some more excellent documentation from the people at redis and they have provided let's just bring it back they have provided a whole list of all the commands that you can issue at the redis cli and you can search for the ones we've just used here but what you'll notice is there's hundreds and hundreds of them and as we come on to working with different data types you'll see that we use slightly different commands for setting data when we are working with sets and when as opposed to working with strings and we'll use a different command to set data when we're working with hashes but they all follow a similar kind of pattern but we'll stick up in that for the moment but again i'll put a link to this documentation below it's very very good very very thorough that's really about it i mean the only other thing i'll probably mention here because we're going to cover this in more detail a bit later is if we run the set command again and we issue the same a key just make sure i spell it correctly plot i've spelt it wrong there you go i always do that from form that's the only one and we give it another value of redis it does okay says okay and basically if we do a get it just returns radius so there's no the point i'm making there is there's no it doesn't complain by the fact you're trying to reset the data just overwrites it so again very very simple and then probably the only other thing going to cover here is the dell delete command and we just again type in the correct key value and we get an integer result one saying that we have deleted one key with that value and we tend to get it you get nil returned so that's about it that's uh yeah that's our first set of string commands now what we're going to do next is we're going to come on i'm going to start shaping our api project and then we're going to come on to actually using these commands but from from within our c-sharp application so let's go on and start doing that now okay now actually just before we move on to coding i didn't mention how you can escape or exit from this command prompt scenario we've got here so we're still connected in to our container and we're still using the redis cli so quite simply i'm sure you would have figured out yourself you just exit that will exit you from the redis command line prompt and then if you just type exit again that will exit you out of the attached container and we're now back in our local command prompt so i'll clear the screen and again just repeating myself here but double double super check we just issue red as cli we'll get an error as we would expect fantastic cool so let's draw a line under that for the moment and come back over into our file explorer and what we want to do now is to start to really think about building our api application and as usual in projects of this nature the thing you want to probably start to think about are any external references that you want to bring in and this is no exception so looking at our cs proj file we're basically going to add a package reference that will allow us to work with redis from within our application now the question becomes what package reference which package do we install and it's a little bit ambiguous if certainly if you've not used redis before so i want to just kind of clarify that for you now so you may see this package here microsoft extensions caching redis you may see that in online tutorials and documentation all that kind of stuff it is essentially deprecated so don't use that one it's no longer being supported so i'm putting up there purely just to get you to ignore it okay the next one you'll see is microsoft extensions caching stack exchange redis and the other one you'll see is stack exchange redis so which one of those two number two and three which one of those should you use well it really boils down to whether you want to use high distributed cache to act as your layer of abstraction between your application and your redis instance i distributed cache sets in the middle or whether you want to use i connection multiplexer again it sits kind of in the middle between your code and radius and you can use either one why would you choose one over the other well i distributed cash uh it's arguably although i'm not sure i fully agree with this well it is technically it's a bit simpler to use it's a bit quicker to get up and running with but the price you have to pay for that is that you are restricted effectively just to one data type okay it's a lot simpler you don't get the whole rich range of data types that redis provides you are restricted to one if you want to use eye connection multiplexer is arguably more complex to set up although i would say it's not by a huge margin at all uh but you have all the data types available for you to use now you know in my mind it's a no-brainer we're going to go with eye connection multiplexer so that's ticking the box for that now actually doesn't really go back and answer a question to be totally honest with you because both package number two and package number three both of those support eye connection multiplex so really which one you want to use you can use either of those if you wanted to use i distributed cache certainly at the time of writing when i was looking at this tutorial when i was making it only package number two supports the i distributed cache the stack exchange redis package does not support it so if you want to go down that path which we're not in this video then you would need to use number two now as it happens we're going to go with package number two anyway and there's it's really just a matter of personal preference the reason i am opting for that package is because i just like to use the official microsoft packages really that's about it and in fact if you come over to a nougat site we just search for redis you'll see stack exchange redis and it is definitely the most popular package so feel free to use that it's a 121 million downloads and then we have the microsoft extensions caching stack exchange redis which has 19 million downloads and this is the one we're going to use here if we click on that and we look at the dependencies you will see actually makes use of stack exchange rates anyway okay so we're going to go with this one here so i'm just going to copy that.net cli tab selected because that's what we're going to be working with i'll just click copy and we'll move back over into our terminal i'd like to have the cs proj file open and we'll just paste that in i'm going to remove the version specification just because you know if you take use this format it will go away and get the latest version anyway run that and we should see package reference being added to our cs proj file which indeed we do so let's just clear this up and the other thing i want to do just while we're at it is remove the scaffolded stuff that we don't need so the weather forecast model i'm going to get rid of that and delete that yep and although we will be writing our own controller i'm going to write it from scratch so we're just going to delete the weather forecast controller as well i'll move that to the recycling bin too so what we now want to think about doing is we actually want to connect into redis and if you've done anything with sql server or any database for that matter it's not going to come as a huge surprise that we use a connection string to connect in to our redis instance so let's set up our connection stream first and when i'm going to do that is just an app settings.development.json i'm going to come in here i'm going to put a comma after this block of objects and i'm going to specify the connection strings parameter and then in connection strings i'm just going to specify our redis connection string i'm just going to call it red disconnection i think that's a decent enough name that can be absolutely anything you want though but just remember what it is because we'll be coming on to using it a bit later and then colon and then the value of that particular connection string now in this case it's going to be a localhost but we're just going to use the loopback address so 1270.0.1 and then the external port mapping that we had created in our docker file a rock and compose file which is 6379 so we come back over here it was uh this port here okay so we're just specifying that's the port we want to connect into important to use and you even saw when i was typing out visual studio code suggested that this was what i was wanting to use and indeed it is and we will make use of that a bit later so save that off that's our effectively our connection string config done and then the next thing we want to do by way of setting up connection is we want to make use of eye connection multiplex so so the way we do that is by registering it in our program class as a service effectively as a registered service so i'm just going to add it as a top level item here and so let's begin i'm going to make access a unique make use of builder to get access to our services and we're going to add a singleton of what type we're going to add a single of i connection multiplexer and i'm hoping i've spelt that correct now it wouldn't know what that is i think i need to put one more n in there i connection multiplexer we do a control period uh i think i probably spelt that wrong so let me just double check yeah multiplexer that's better so control period there we go and we get this using statement so we want to we can remediate this error a number of ways the way i'm going to select is this introduction of a using statement and you can even see the name space that's being used it's just using the stack exchange redis namespace anyway okay so that's control periods to to get that obviously i had spelt that incorrectly and when i tried to get uh code resolutions it didn't find any appropriate ones to solve our given problem so fantastic and then we're just going to use a lambda expression just to configure some options namely the connection string that we want to use to configure our connection to our redis instance so i'm just going to take a new line and the old tool tips are great but can be very annoying and i'm going to make use of connection multiplexer there we go and connect and then we want to supply the connection string into this method here so let me just i want to just round out this line and then here we want to feed it the connection string basically so i'm going to do a slightly long-winded way i'm going to make use of builder again and then we're going to access the configuration object on that and then on that i'm going to make use of the get connection string method and then to into that finally we pass the name of our connection string so we come back to our app settings development json file and we want to supply this here so we'll just copy that and really importantly you can store your connection strings in any way you like in this file if you elect to use this method here using the actual connection strings attribute name then it means that you can make use of this get connection string method as we are doing now if you choose not to use it then you would have to access the the relevant configuration in a slightly different way so i recommend this is the way you do it okay so that is really the redis stuff uh by way of connecting and all that kind of stuff connecting in and configuration that's it really done although it's not working yet we haven't done anything with it but we're going to take a bit of a pause here before we move on to the next section which is creating our model but yes for now we're kind of done with an initial setup and then next yeah we're going to start building out the actual features of our api starting with our model all right so just before we move on to coding actually it might be worth just doing a bit of a sense check to see where we're at in terms of progress so working from the bottom up on the diagram on screen there our redis database has been set up in docker using docker compose and it's all up and running and we've even used it so that's tick in the box the eye connection multiplexer as you saw in the last section was there really wasn't that much to setting up package reference connection string and then registering it in our dependency injection container pretty straightforward so unless we've made some weird mistake in the connection string or something we shouldn't have to touch that again so that really just leaves us with our model which we're going to come on to next our repository and our controller now in terms of novelty and in terms of redis interesting red is code most of that if not all of it is going to sit within a repository implementation because the repository is actually going to be using the connection multiplexer to talk to redis fundamentally and the whole point of a repository pattern is that the controller the consumer in this case of the repository is absolutely oblivious to the fact that it's using redis as a database it does not care it's just going to use an interface that we're going to write ourselves and it could be using sql server it could be using redis it doesn't know it doesn't care and that's the beauty of the repository pattern so anyway we still have model repository and controller to do and so let's move on to looking at our model next all right so let's come on and define our model back in our project so in our file explorer right click new folder and we'll create a folder called models and now we're only going to have one model but i'd like to just keep things organized in my code unlike in my real life which is a lot less structured than this so um in the models folder right click new file may create our platform model so just platform cs and again i keep mentioning this but i have a tendency to misspell the word platform so just double checking that that's correct then as usual namespace which in this case is redis api and then we're in the models uh space mod dells cool and then we used to specify a public class called platform all right and our platform class is going to be very simple it's just going to have two properties an id which is really going to be our key in redis terms and maybe more traditional relational database terms would be our primary key and the second property is going to just be the name the name of our platform and we'll just call that name so let's define those two properties in here and short way of defining properties is if you type prop and then tab you get this kind of template for a property so the first one is going to be our id now i'm going to define this as a string and i'm going to call it id now straight away you'll see we get a little warning we'll come back to that in just a second let's just define our second property so again prop tab it's also going to be a string and we'll just call that name and that's again going to be the name of our platform super simple okay so i don't want to spend too much time on this but i do want to just cover why we're getting warnings here and for those of you who've used earlier versions of the.net framework if you define this class in exactly this way you wouldn't have got these warnings so what is going on is there something new in dot net six yes and no to that so what is new is if you come over to our cs proj class what is new is the inclusion of this here this nullable enable and really what that is doing is enabling warnings like we're seeing in our platform class it's enabling them it's enforcing them and what that means is the compiler does what's called static code analysis and it looks at our code and yeah basically it tries to pre-warn us when we may in this instance get null reference type exceptions potentially and so it's picking up and saying look the way you've defined these two strings which are reference types in c sharp may return null at some point and you may get some kind of error now what you can do is define some strategies here to deal with these warnings now one strategy would be to remove that bit of config from the cs proj file not going to do that i'm going to leave it in because i think they're actually although they can be a bit annoying probably valuable to have them there so what can we do well in their infinite wisdom microsoft have actually provided a number of techniques for dealing with these nullable warnings and there's also an article on nullable reference types which where as it says they are they were introduced in c sharp eight so nullable reference types aren't a new concept they've been around for a while but what is new is that enforcement of warnings in our code so again down here how do you resolve nullable warnings i'll let you read all about that fantastic so we're going to resolve these warnings in a number of different ways so the way we're going to do it is by basically assigning values default values to these two properties so we'll deal with the the first one or the the second property but we'll do it first because it's a simple use case and all we're simply going to say is it's going to be set to an empty string and the warning should dissipate as you can see there now again there's lots of strategies for dealing with this i'm just going to take this approach because it's simple and relatively straightforward you could um let's see if we took that out we'll get the warning back you could specify this as nullable if you put a question mark in there that will take get rid of that i'm not going to do that i'm going to use this approach here and make it equal to string empty all right cool now for this one and again just to label the difference between reference types and value types if we define this as an integer and not a string you'll see the warning goes away because integers can never be null okay they are value types whereas string the reference type in this case could potentially be null and if you don't know what the difference between value types and reference types are do about googling again kind of outside the scope of this video so we're going to do exactly use the same technique here we're basically going to assign a value to this now we actually want to do this anyway and this is a really important point it's a bit of a design decision when we're looking at using redis so taking redis aside for the moment in my other videos when i've defined a class like this i actually have defined this property the id property as an integer and then when you ultimately migrate that class down into sql server for example what will happen is that that id will manifest as a integer data type in a sql table it will be set as a primary key so it's enforced as being unique and also certainly if you're using sql server the id column will be set as an incremented primary key so actually sql server pre-populates the id column with a value that's guaranteed to be unique so you don't even need to pass an id value with your code it's done by done at the database level redis doesn't do anything like that for you it doesn't generate keys for you you have to supply the key so we actually are going to have to do that here anyway we're going to have to supply a value to this property anyway and then double so it's got a double benefit number one it's generating our our key for us and also it's going to get rid of this null reference exception so the way we're going to do it is we're just going to say equals it's a string but we're going to use string interpolation so put the dollar sign in there i'll just terminate with a semicolon and our key is going to follow the format that i've already talked about we're going to declare i just hit what key did i hit f11 there um we're going to declare our key as having the prefix of platform uh colon and then the numerical component of that key so the platform colon is still very much part of the key it's still part of the key but the additional numerical unique part is going to be just a good that we generate on the fly so curly brackets to work with some more dynamic code so we're going to use the guide class and we're going to say new good and then we're just going to set that to a string and i think that should be good okay so we're going to end up with a key of the format platform colon and then a good which is i'm sure you're aware of a big long string of random digits and numbers guaranteed to be globally unique okay so that is our class actually we've got one more thing to do but that's basically the main part of our class defined so we've got another null reference type warnings and we've defined our key which is really important the only other thing i want to do from an api perspective is decorate these properties with required so that when we come on to um yeah defining these or creating these platforms via our api we know what items are actually required to be provided so we need to bring in the namespace for that so control period and it's system component data annotations and the same for this as well fantastic okay so we'll save that off and we'll do a quick.net build just to check that we haven't still get any warnings anywhere and then we can move on to our repository assuming everything is okay which it looks like it is let me just make this a bit bigger yep zero zero errors build succeeded one of the nicest things you can see is a developer isn't it build succeeded with zero warnings and zero errors and i what it is i actually i hate warnings i actually hate warnings almost as much as i hate arrows that's why i like to get rid of them cool so that's our model class completed and now we're going to move on to working with a repository which is arguably the most interesting part of today's project because that's where all our redis stuff where this code is going to go so let's get started with that now all right so we're going to move on to creating our repository so back over in our project the first thing we're going to do is create another top level folder so right click folder and we're going to call that data next up we're going to create an interface so new file and we'll call that i platform repo dot cs now if you don't know what interface is an interface is really just a list of the methods that will be available to you as a consumer of this interface if you choose to use it there's no implementation there it's literally just a blueprint or a template or an interface and then the idea is that we can then write an implementation class that implements this interface and when we then ask for this interface as a consumer we don't really get given the interface well so what we do we actually get given the class that implements it and you'll see all that how all that ties together so the interface is really just a spec really for the methods that this interface exposes so we'll start with the namespace as as usual and it's redis api and we're in data and new line curly brackets and then it's just the public interface and we'll call it i plat born repo i'm just double checking my spelling of platform as as usual all right fantastic so let me just create our method signatures now before we do that i'll just quickly remind you these are obviously our endpoints that we're going to implement now the method signatures we're going to specify in our interface are very very closely tied to these what these endpoints are doing actually so that's not always the case by the way you're in your repository interface maybe many many more methods um but in our instance yes there is actually going to be a one-to-one mapping so just start call it out but don't think that there was always a one-to-one mapping just in this instance there happens to be so so the first method signature i'm going to specify is going to return a void nothing and we're going to call that create platform and as the name would suggest is this a method that will allow us to no surprises create a platform okay getting a bit tongue-tied void create platform perfect and into that we expect the platform model and we'll call that flat fantastic now we'll complain about knowing what not knowing what platform is so control period and we'll bring in the relevant name space fantastic so that's our first method signature the next one is going to be a get platform by id so we're going to provide a key and expect back one platform if there is one we might we might get nothing back so it's going to expect a platform again and we're going to call that get platform by id and into that we're going to expect a key which is a string in this case we'll call that id and then the final one is our enumeration of all platforms in the in the in the repository in the database and so we'll use an iron numeral and what type is it it's of type platform again and we'll just call that get all platforms and we don't need to pass anything into that we're just saying give me all the platforms so that's our three method signatures so that all looks good again this is just an interface there's no implementation which is what we're going to do next so um that's that's our methods specified so back over and data if you right click folder new file we're going to create a concrete implementation class for redis that's going to implement these methods so we'll call it redis platform repo that's yes and the idea here is we could create another concrete implementation class for sql server or postgres or something else and you can swap those implementation details out the consumer again you'll see this our controller is only interested in the interface so it doesn't really care what's implementing it it just knows that those methods will work for it so that's the idea behind this very cool concept now this class here is actually where all the interesting redis code is going to sit the the point of making about the controller being blind to the implementation class means that our controller is just going to look like an absolutely totally standard net api controller it's going to look there's nothing reddish about it okay all the redis specific stuff is in here and we'll spend most of our time here and we'll revisit here as we move through the rest of the video so again specify namespace of redis api and again we're in data and it's time to say public class and it's called redis platform repo now we're going to want to implement or say that we're going to attempt to implement the interface we've just defined so we do colon and then we find the interface specification which is i platform repo and well we're not quite good yet because it's complaining and the reason it's complaining is because we've defined the class we said we're going to implement this interface but in fact we haven't done that yet so what you can do cursor in the offending article control period and select implement interface now it's not it's pretty good but it's not clever enough to completely implement it for you as you can see here it's all it's really done is create the method signature templates and then put a not implemented exception inside there but it's still pretty good we still need to go in and actually do the implementational detail now just before we do that into our class we want to make use of our eye connection multiplexer which we set up in the previous section because that's really the glue between this effectively and our redis instance so we need an instance of that and the way we're going to get that is through constructor dependency injection so first thing we're going to do before we implement these methods is set that up so we're going to create a constructor in our class and the way we do that is by typing ctor and then tab and it gives us a kind of yeah again another placeholder templated kind of constructor and so into that we're going to expect an instance of i connection multiplexer i spelt that correctly hopefully i have and we'll just call that let's just call it redis so i think we need to do at least a capital m spell correctly hopefully and then control period to bring in stack exchange redis so we're going to do use a very standard pattern we're going to assign whatever is injected in through the constructor ie this thing here we're going to assign it to a private read only field called underscore radius as so and if you've seen any of my videos before this will be a very common pattern by the way i didn't invent this it's a pattern that's out there everywhere if you work with constructor dependency injection i'm not going to clay my invented dependency changing all right so again it's complaining so control period and we want this generate read only field and that places that up here for us so again just to come back to our program class we did wire up our i connection multiplex so it's registered in our dependency injection container and so when we ask for an instance of it here we get given it from our container and it's assigned to this initially and then we assign it to our private read-only field and we can then make use of that underscore radius eye connection multiplexer through the rest of our class fantastic all right so let's move on and start actually implementing some of these methods so the first one we'll have a crack at is creating a platform in redis and you'll remember when we did the command line stuff we used the set command to set string data type so we're going to basically do that but via code all right so let's just quickly do a check first to see if the platform object that is passed in is null or not so if it is null then we'll just throw uh argument out of range exception and we will specify a name of plat okay fantastic so assuming it's not null we want to continue on now maybe before we continue any further what i'm going to do is i'm going to make use of that desktop desktop tool for redis that i was talking about and the reason i'm going to sort of have a look at this is because it gives us a nice view of what looks huge it gives us a nice view of the kind of not the internals of radius but some some of what how it's structured so if i just pull this onto my screen oh my goodness it's going absolutely massive um let's see if i can make it a bit smaller no anyway but hopefully you can kind of see that i've got the screen magnified um so this is our docker instance the one we have running in docker and you can see that's basically what i'd call the docker server right and then by default in the docker server and again unfortunately you can't see all the way down there are 16 of these databases numbered 0 all the way up to 15 and you can see that currently everything is empty there's no key value pairs or anything in any of our databases all right so why they chose 16 databases i'm not really too sure probably should have done that research but by and large they are effectively different name spaces and by default we're just going to put everything into db0 you can change that of course but that's what we're going to do and as we put data in here you'll see it kind of manifesting now unfortunately yeah this is looking a little bit a little bit mad it's absolutely huge i don't know if i can make it a bit smaller and unfortunately i can't but um yeah you get the idea there anyway so where are we yeah we're coming back here we're going to create our platform in code so that's cool let's get on with that the first thing we need to specify is we want to kind of create a reference to one of those databases the default database and when we do that is by specifying a variable and we just make that equal to our redis instance and we just say get database and that will get as a reference to db0 and you could select any one of the other databases if you wanted but we're not going to do that and then we're going to take our object and we are going to serialize it into a string because we're using a string data type so in order to do that we'll create another variable and we'll call it serial plats and we'll make that equal to json we'll use the json serializer i'm going to bring that namespace in which is control periods and using using system text json and we want to serialize our platform that was passed in and that will then get assigned to serial plat variable and then finally super simple we make use of our db again and this actually gives us access to a whole range of redis commands on that db and the one we want is string set and then we pass in the key that we want so in this case we're going to use the id of the platform object that was passed in makes sense and then just the entire serialized platform object and that's it now what you'll see here when we use the set command at the command line it was just set the kind of get rid of that tool tip the kind of standard that the stack exchange redis package uses is to kind of name the command set but prefix it with the data type that it's relevant to so it's actually quite nice so you can tell just by looking at that we're setting a string and the rest is fairly straightforward fantastic okay so we'll save that off and then we're only going to implement one other one at the moment and the next one we're going to implement because we're going to come back to the other one we will implement all three we're going to skip over the get all platforms one for the moment we're going to leave that as not implemented and we're just going to implement our get platform by id so let's remove our exception and let's get going with that so very similar first thing we want to do is define a db and we just made that equal to underscore redis get database now we could probably um me this is obviously so duplicated code somewhat so we could probably re-factor our class to just have one db instance but we'll just keep it simple for the moment and just call it again and then we will define a platform object because that's what we are hopefully expecting to get back but again we could get a null back we may pass in an id and we might get nothing's but anyway we'll cover that scenario in a second make use of our db object again and we're going to use the get command and following the kind of syntax of the stack exchange redis package it will be string get there we go and you simply just pass in the key or the in this case the id that was passed in in the method that's basically our key all right excellent so at this point we may or may not have a platform object so we'll just do that check so we'll say if not string is null or empty plot so if it's not empty then we want to return and again we're going to deserialize the object because it's stored as a serialized string we're going to deserialize it into a platform object so again we make use of the json serializer this time deserialize method what are we deserializing to what type would be serializing to a platform there we go and what we're providing to that we are providing our plant object which we know is not null so somewhat safe and otherwise if you know if this condition here is not true then we're just going to return a null and it's going to cause us all sorts of problems again based on our wonderful friend null reference type so we need to change this slightly to get rid of these warnings we could leave it this code will still run i just don't like warnings so the way we solve that in this instance we have to do something in two places is up here on our we're specifying we want a platform back we're going to tell the compiler the the through static code analysis and say that we can expect this to be null we can safely expect that to be null that's okay and that's cool so that's kind of resolved that but it's just pushed the issue further up the food chain because we're now not really this method signature does not match exactly the method signature that we have here so all we need to do is just update this and save it and hopefully or not hopefully we shouldn't be hoping we should be knowing what we're doing uh you can see the warnings have dissipated so again it's just a good point this would still have run it's just warnings but i don't like warnings so we're going to get rid of those and so we just need to use these techniques to kind of get rid of those warnings fantastic so you might just do a dotnet builds well actually we won't the one thing i always forget to do and have remembered this time is we need to register this repository in our program class that's the last thing we need to do so underneath our connection multiplexer you don't really put it anywhere really we'll make use of builder again services and this time we're going to add a scope and quite simply we say if someone asks for an i plat form repo i'll bring in that namespace using redis api data someone asks for this what we're going to give them we are going to give them this instead redis platform repo okay and it's this is the line here that you would change so if you had an implementation class for sql server and you wanted to swap it out this is where you would swap out and you'll see when we come on to doing our constructor or controller it's just making use of i platform people doesn't care what the implementation class is so it's very very cool all right awesome so we'll save that off i think we'll do a dot net build it's looking good but we'll just do it anyway um just to check before we move on and we should see zero zero that wonderful beautiful green build succeeded zero zero fantastic so that's our repository kind of done we are going to revisit this of course because we're not quite finished but we're done for now in terms of moving on to our next step and that next step is uh wiring up our controller so we'll move on to that now all right so we're on to creating our controller now so back in our project right click and we're going to create a new folder in the root called controls and into our controllers folder we're going to put our controller class so plat forms controller.cs and just double check yep that's spelt correctly so start starting from the top down namespace first redis api controllers and then public class platforms and we're going to make use of a controller base class i'm going to bring in the namespace for that so control period using microsoft asp.net coordinates fantastic and then we just want to decorate this with api controller and that just gives us access to a range of default behaviors for an api controller that you're probably going to want to use or have about next thing we're going to determine is the route to the controller itself so the kind of base controller route now actually before we do that maybe if we just pop over to our controller actions when we talk about the controller route we're really in the context of what we're looking at here we're looking at the part of the route that is common to all end points so you can see there the route that's common to all endpoints is api forward slash platforms okay and so that's basically our base route we can define that at the class level and then when we start getting down into the individual endpoints if they differ from that then we will deal with each of those cases in turn and really the only one we need to actually deal with is this first one here getting a single resource which expects an additional uh id or key as part of the route okay so api platforms is our basically our controller level route so just specify the keyword open round brackets double quotes api forward slash now we could type platforms hardcoded or what i'm going to do instead square brackets and use the make sure i put two else in and put the controller keyword in here and all that does is it looks at the name of our controller and it just pulls out this part of the name of the controller and puts it in there as part of our route so actually is api forward slash platforms okay you can hard code if you want to it's entirely up to you all right now what i've said a few times now already is that our controller is going to want to make use of our repository and all it's going to be interested in asking for is our interface the interface to the repository because all it's interested in are what can you do for me it doesn't care how we implemented it so maybe do that again it's through constructor dependency injection so we're going to define our constructor so again ctor do you remember that from last time come across and we're going to inject in an i platform repo okay and we'll just call that people now we don't know what this is so control period to bring in the right namespace and then as before we're going to assign whatever was injected in here to a private read-only field we'll call it underscore repo fantastic and we'll just control periods to generate a read-only field that looks good and just to remind you although i'm sure you remember back over here this is where we registered our repository and what we're saying here is if you ask us for this which is what our controller is doing then we are going to give you this and of course we can swap that out as much as we like and the controller or any consumer for that matter doesn't know doesn't care the end result might be quite different but it doesn't know where it doesn't care all it cares about is what can it do so let's just save that store.net build and that's kind of um that's kind of the the shell of our controller set up that looks good so let's take a quick pause here and then in the next section we'll move on to creating or to control our actions which is where it gets really exciting we actually start using redis all right we're moving on to our api action results now so let's just take a new line and the first action result we're going to define is the ability to get an individual resource now maybe before we even start that let's take a quick reminder look at our action results here and so yes it's this first one we're going to define okay so it's a get get result get verb that we're going to specify the controller level route is going to be fine but we do need to specify the additional need to use our an id type parameter in the route as well so we'll come on and do that now so let's see public action result of type platform and again this is where i think i mentioned dtos before i did at the start actually this is where you would use data transfer objects you wouldn't be returning back your internal model class directly or you shouldn't really just because you're tying your implementational internal implementational detail to an external contract but if you are interested in that though my best mvc api video uh i can put a link up there and you can take a look at the dto section for our purposes though it's fine we don't really need to worry too much about it so we'll call this action get platform by id and we'll expect to pass in string that is basically our id and you need to specify the verb as i've just said so it's going to be a http http get and then in addition we want to specify that we expect to get a parameter as part of our route so that's open round brackets double quotes curly brackets and then we'll just specify that we're expecting an id and the other thing i'm going to do is i'm going to specify the name for this as well and i'm just going to make it exactly equal to the method name you're going why are you doing that why do we need that i'm not going to explain that right now it would be too confusing but i will explain it in a bit don't worry i'll come back we'll circle back to it platform we need to bring in the namespace so control periods so rest api models that's cool fantastic and then yeah we just basically need to go into our repository and see if we have a platform that matches this id so the first thing we'll do is specify a platform variable and we'll just make that equal to our repo and we'll call the get platform by id method and we'll just pass in the id simple as that now bear in mind this could return all remember we engineered that that way so let's just check to see if it's not null so if not platform equals null and that we don't need to do that platform doesn't equal null then we simply return back and okay so http 200 and along with that we'll pass back the payload of our object all right fantastic otherwise we'll just return a not found 404 error 404 response okay so fairly straightforward nothing too controversial there probably the only thing that might be slightly confusing is why i did this but we'll answer that in the next section when we do our next action result so quick pause and let's come back and do our second action result which is creating a resource all right so creating a resource let's just take a new line a couple of new lines and again it's a public action result and we are also wanting to pass back the platform so the idea is we're going to provide some json to our api and it's going to return back that newly created platform with the relevant json as well as a platform object all right so we want to return back a platform as well and we're going to call this create something very original like create platform and we are actually going to expect in as well a platform object so we're going to specify that as our inbound payload and again you would use a dto here as well usually like a dto create detail to specify your inbound payload which you'd pass in here and then you'd pass back like a read to dto we're not doing that we're just directly using the model all right i won't mention dtos again and the other thing we want to do here is just decorate that quite simply with a http post method and again although we did just look at it just to clarify our results or action results we're doing the second second one here we just need to specify the verbs post we don't need to do anything specific around the route because it's just going to inherit the controller level route that we've already specified okay and then in terms of our repository repo all we need to do is just call the create platform method on that and we just pass in not a zero that wouldn't make any sense at all we are going to pass in the platform object okay and then the second line and this is probably the most complex line in this whole controller code we're going to return not the return state and that's not complex at all but we're going to make use of a method called created at route and it's just created at route method that's going to make use of the name of this action result and what this is doing as i've said already is when we successfully create a platform in our redis database and we want to pass it back so we want to pass back the json payload but as per the rest specification we also want to pass back the uri the resource locator for that individual resource as well so we use this created that route method to do that and we pass it number one the name of the route that we can use to retrieve our resource along with the id which is the unique component and as well as the object itself so we'll do that now so created that route so the first parameter we want to pass in is where the uri effectively of where we can get to this resource so we're going to use name of and then we just pass in the name of this method here okay that's basically telling us where we can sort of get it we then need to specify the id that we're going to use as part of that route so new and we'll just specify id equals and we'll use the id on the platform object that's being created and then finally we just pass back the entire object itself okay so get a few questions about this hopefully i've explained it well enough you'll see when we actually come to creating it how it manifests it might make a bit more sense then and that's it so i mean obviously we're going to come back to our controller code we will revisit it because we're not completely finished but these two action results there's some wrong are pretty much done we don't need to change these we will be changing our repository a bit later but we don't need to change these these stay the same that's the beauty of using an interface anyway we are going to now come on to testing this and see if it all works and seeing where if any mistakes that we've made hopefully not fingers crossed so come on test this and then we'll move on to the next section so do that next all right so the moment of truth we're going to test our api so net run to run up fantastic and i'm just going to copy this base url here and i'm going to use insomnia to test our api now i think i mentioned in the introduction i might use i'm maybe going to use postman that was just a slip of the tongue for most cases i will use insomnia i just like its interface more it's a bit cleaner it's a bit simpler postman is excellent by the way but it's probably got too many features for me in its interface can be a bit confusing i like this it's just a lot cleaner all right so the first thing i think we should do because our redis instance should be empty and if we if i show you my this tool i use it's going to look fairly massive on screen but it doesn't matter our db 0 is empty if we refresh that there's nothing in it okay so we're starting from a clean slate so what we should do i think first is create a resource in here so let's say create platform and it's a post request obviously and the body is json all right so that should uh hit our route so let's just paste that in here and we need to specify this api and plot performance so yes post request this is our route should hit that create action result now we really need to do is just pass in some json that we want to you know use to create our object so we specify the name and let's christen our api by saying redis as our first platform now you'll notice we're not specifying an id here and you'll remember that is because we come over to our platform object we are generating a unique id or key let's you try and remember to use the redis terminology we are generating a unique key ourselves here nothing to stop you not doing that here and passing it in via the api you could do that but we are yeah we're choosing not to do that all right so let's see if this works let's click send and there we go we got a platform back so that's looking very exciting so we get the full json body including the id which you can see has been constructed for us in the header you'll see we have this location attribute and that is the route to this unique resource and again coming back to our code this this created that route method this is really what this part is doing here it's returning us back where we can get the resource and of course the id of it so we can uniquely get back to it so what we can do actually is take a bit of a shortcut and we'll create a new request and we'll just call it get platform get request create and we'll come back here and we'll just copy this here straight in to here just to test out there we go all works fantastic now the only thing you might notice is that it's done some sort of string encoding it's removed the call will not remove the colon it's re-encoded the colon as whatever that was let's just try it again yeah it's fine so what was the code it was uh yeah percent sign 3a okay so it's kind of url safe doesn't matter you can use either here it works fine so you can see yeah it's all looking pretty good you can see it's pretty damn fast you know look at that it's fairly fast now you'd expect it to be there's only one thing in the instance and it's running locally but it's still it's very very fast all right so what we might do is if you come over to redis in our gui instance i'll just refresh it and you can see here we have in our dbo 0 instance we have one key value pair and you can see here it's got a full name and then this might be a little bit difficult to see excuse me move this over you can kind of see what it's telling us it's saying it's a string and i believe that will be the key in here against the key and then the actual value so the string value no string key and the actual value that it contains all right so let's add one more because i want to just show you another thing about this tool although it's probably a bit difficult to see let's create another platform and we'll call this one docker just create a few send let's create another one we'll call it kubernetes and let's create another one and we'll call it what we call it i don't say terraform it doesn't really matter just trying to think of things i've used in my other videos all right so that's all looking good we don't have a give me all these platform endpoints set we'll come on to doing that next and we'll have a bit of a discussion around that but what i wanted to show you just before we move on to that section is if we come back here and again this is this is very unique to this tool but what you can see is done is it's kind of grouped because we have four platforms now it's kind of said ah okay these are four platforms kind of of the name same so i'm getting kind of of the same name space platform everything's prefixed with platform colon so it groups it under basically a namespace okay so i like that about this tool that it does that and you can just navigate through and look at the values of it but the point i'm really making here is that's kind of why we structured our keys in such a way because other tools and other platforms will do something similar to this they will the use of the colon in the prefix kind of allows you to organize your keys in a bit more of a structured way okay so that's really the point i wanted to make here and hopefully this is now starting to you know percolate in your mind how this is structured but again this is purely a user user interface rendering the key itself is still absolutely platform colon good it's just rendering it in a slightly nicer way and you'll remember actually when we just had one uh platform in here it had just rendered it as the one thing all right cool so very good um we are we can start to get to a point where we're saying yeah you could absolutely use redis as a primary db so far it's all working brilliant but what happens if we want to return all our string based keys back i wonder if we're going to run into any potential roadblocks maybe we will so quick pause here and we'll come on and we'll talk about how we can achieve that endpoint in the next section all right so we're going to come on and look at implementing our third end point which is retrieving all our resources so the third one that you can see on screen there now just in the last section i was saying we might run into some problems yes and no really so what i was alluding to was the fact that um we are currently using strings so when we create a resource we're using the string data type and when we're retrieving the resource we're using a string data type and that all works really nicely when it comes on to retrieving all resources stored as strings you can do it you can absolutely do it and in fact the command that you would want to use let me just bring on the reddest documentation on screen is something called scan okay so this command absolutely does exist and it'll actually show you it working we're not going to implement it in code but it does exist and you can scan for all your keys basically and it'll pull back your resources and i actually did play with this i did implement it in code it does work but for a couple of reasons i didn't really like it and the first one was well i actually wanted to explore other data types the second one was i felt that it wasn't particularly optimized for speed the way scan works it's a cursor-based function and you may have to start dealing with cursor positions and it doesn't guarantee how many results you're going to get it all works and everything as everything in redis does it works really nicely but i just felt it was a bit clunky i didn't really like it it didn't resonate with me so that was the two main reasons i wanted to use some other data types anyway and it felt a bit clunky does work though it's fine so if you want to go with a pure string based approach this is perfectly acceptable the other point i'll make is that it kind of works at the server level so in fact there's another article here from the guys who created the stack exchange readers package and they actually start start talking about this um when you compare running stuff at the database level versus running stuff at the server level and it's all a little bit the pattern's a bit different as well when you start coding up this scan command it's a little bit different and i didn't really like it so that brings us on to thinking about some other ways of doing it now just before we do that i'm going to demonstrate the scan command to you just so you can see it working and there's another reason i want to show you the scan command as well so back over in our project if we come back into our docker instance now if you don't have this running that's okay you'll just have to do some stuff at the command line but i'm going to attach to a shell using the gui using the interface uh attach shell and you can see all it's actually done is just done the command that we've run before which is that docker exec dash i t and they're using a slightly different it for the container but you remember we did earlier you you would have to run that same command again if you don't have this tool so we're attached to our docker container so all we have to do is run up the redis cli and we're at the red is cli so let's do a quick ping you know that's going to work can we get a pawn back brilliant so the scan command you type scan and then the cursor that you want to start from so it's a zero based cursor so we'll say give us this is really a trying really attempting to give us everything and it brings back our four keys anyway and you can build the scan command out so you actually get your values as well so it does work it's okay um i just didn't really want to use it so uh yeah i did write the code for that and if i remember i'll put that code up on github as well now the other thing i wanted to cover and i'm just going to use the scan command to demonstrate it is that one of the reasons again the whole reason for this video is should we be using or could we use red is legitimately as a primary database and one of the major concerns people have or misconceptions people have is the fact that as read this is an in-memory data store if it rebooted you would lose all your data and that really doesn't lend itself well to being a primary data store that's not true i mean it isn't it's in a memory data store and it is super fast but it also does persist data down to the disk so if it reboots the data is still there it'll just be reloaded into memory so i just want to demonstrate that just to show you that because we're not really covered that off as a point of note so again i'm just going to stop this container using the gui if you wanted to use docker compose commands it'd be docker compose stop to stop it um now we've stopped it you can see that it stopped and we've lost our connection to our terminal which is actually a good thing so we'll just restart it so stopped it and we'll restart it again and then we'll just reattach shell fantastic going to the redis cli and we'll do scan zero again and our four elements are still there and i could bring up that other tool where we could run our api and those four elements are still there so just wanted to cover that off as a kind of tick yeah is the data is persistent you don't lose it even in the event of a reboot cool so what we're going to come on to next is we're going to use a different data type to attempt to implement or get all commands so just take a quick pause here because i've thrown a lot of information at you there and we'll come on and take a look at what that approach is um it's not our final approach but we're going to do it anyway just to exercise some different data types and we'll see how that works and then in the last section we'll do our final implementation where you just use the one data type to do everything anyway quick pause here and we'll come on to our next approach to dealing with retrieving all our platforms all right so we're going to yeah keep going with attempting to get all the resources implement or get all resources endpoint so this is what i've tried so far i've talked about the scan command which we're not going to implement but if you are interested i will run and i remember i'll put the code on github and you can take a look and you might want to use this approach it's up to you what we're going to do next though is use sets as well as strings so we're going to take a bit of a hybrid approach to working with or to building an api so what that means in practice is that in terms of our repository methods the existing get platform by id method which currently uses uses a string data type that's going to continue to work exactly as is we're not touching that but we are going to implement a new repository method get all platforms and that method is going to return an entire list of all members of a set okay and so what that means in order for that to work is our create method is going to have to create two things number one as as it's doing just now it's going to have to create a string number two though and this is what we're going to introduce now it's going to have to add that platform object to a new set data type and that means we can then implement this third method now straight away you're probably going to be going that sounds really horrible do you know what yeah it is but again it's just part of this exploration of using different data types to see what they can do so again we're not settling on this but we're going to have a look at it see how it could work and then we're going to move on to the final section and come on to our final implementation right so cool i just thought i'd explain that and we have a third option here but i'm not going to reveal that just yet but what i will do is just clarify and go back over the string data type which we've already done just to refresh your memory so it's the simplest type of value to be associated to a key one-to-one mapping useful for a number of use cases we've gone through this already i'm using set and get so this is what we already have and you can see it's a one-to-one mapping to talk about sets which is really what i want to talk about now it's an unordered collection of strings so it's a one to many mapping between a key and a value so we're going to create a set next and it's just going to have a key and then the values inside of that are going to be our individual platform objects so this lends itself very well to just going give me all members of that set now it can do a lot more than the way how we're going to use it but i just again it's part of exploring what these data types can do yeah so you add members to set using sadd so for example you could add the elements one two three to a set called my set and then you can return all members of a set using s members my set and you can also check to see if things are members of a set and all that kind of stuff so sets are good for kind of almost constructing relations between other entities they can be used for that kind of thing and yeah you might ask could we not use a set um could we not use this pattern to get an individual resource no you can't you can't actually use it to do that because there is only one key now you can start dipping into the the members and stuff like that the values but it doesn't really work at least not as far as i could tell so that's why we have this hybrid approach of maintaining strings for individual uh responses and this method for every response it would be lovely if you could um you know use this to get an individual elements you can't based on a key as far as i can tell anyway all right so on to the code so back over in our project i'm going to disconnect from this first example of that and return back to the command prompt and come back over into our code editor and then we just want to ensure that we are in our redis platform repo because that's where we're going to be making the change and we're going to come in first of all to the create platform method and we're already as you know creating a string here so all we need to do here to update this is again make use of our database and instead of string set we're using sets now we're going to use set add so it's not set set set would be a bit but weird it's set add because we're adding a member to a set contain again i remember lots and lots of unordered strings so it requires a key all right so one set will have one key so we can basically give this any key name we want all so we'll call it um platforms set maybe take out this platform sets good enough and then all we pass in here is again the serial plats object is that simple and that will then add i mean anytime you add a platform it will add to a set as well so let's just test this okay so i'm going to save that off now again with dot net 6 you can actually do a net watch to do a hot reload i haven't been i'm just not getting to the practice of using it so i'll stop our server and we'll do a dot net run dot net run but yes dot net watch allows you to do this kind of reload stuff but um i'm not really into using that just yet but it is definitely available okay so we've applied our changes to our redis platform repo class so what we'll do is we'll come back over to insomnia and we'll create another platform okay so here's our insomnia instance oh my goodness um i think i've said i've got the screen magnified and i just find the magnification screws up anyway this i'm not going to bother doing that i'm trying to resize this but it's not really working for me and i think because yeah the screen's magnified so let's create another platform i don't know outlook not look exchange microsoft exchange doesn't really matter so we'll create a new platform cool we've got our platform created as before if we come back over to our rdm tool and we take a look at what we've got what you can see here is we have our list of platforms as strings as before and this was probably latinos nothing's particularly ordered kubernetes okay yeah it's ordered to no looks like it's completely randomized that's fine maybe it's based on the keys or something um so you can see here's the string data type that we're using in existing and that's all fine that's going to work but we now have our set platform set so we click on that you can see how that looks it's a set data type called platform set and we have one object in there which is our exchange object now straight away you can see well these are now out of sync you know we've got five and one and four in the other now you'd have to um clear down everything and re-sync them that's not a big deal um so let's add another one we can just see see this again see this working exchange oh i don't know let's just say xml doesn't really matter xml is not really a platform but no it doesn't matter right pull it over refresh this you can see again we get six in here and we will now have two items in our set all right so again it's not working exactly as we wanted but again we just um reset everything now it's worth noting and i've not implemented it because yeah we're not going to go with this approach in the final analysis you can in red is kind of and contain things in a transaction so you can ensure that both of these commands would run together so you're not going to get things are out of sync you know because the last thing you'd want in this approach is you have yeah as we're seeing at the moment things out of sync you want to have exactly the same stuff in each one it's not a nice approach and i'm not advocating as an approach but again it's really just to show you the use of sets working all right now what we really want to do is we want to implement returning all items in our set so we'll come on and do that now and come down to our get all platforms function and we'll write some code to pull back all the platforms in our set now it's only going to return to for obvious reasons but again we'll just have a go and see how that all works so let's take this out and we're going to again create a db instance where db equals underscore redis get database as before you probably want to deduplicate that code anyway we're going to pull back our complete set so we'll save our complete set equals db and this time the command we want is set members okay and then we just supply the name of the set key and i can't remember what we call the platform set there we go we'll just copy that in here so it says give us everything in that set all right so we'll do a quick check to see if the complete set length is greater than zero which means we have some members if not we will just return a null all right now again this is warning us because we had said that this cannot return anything null so make use of our friend the uh it's going to say exclamation mark that's not an exclamation mark what's that's a question mark and then we have to come back into our uh repository interface and do likewise up here and we'll see that often that should get rid of that warning hopefully i actually think we need to double up here try that and then do that as well that's better okay so double double double up all right so assuming it's uh and again you know that to my point about having these warnings they're good but they can be a bit annoying as well anyway um assuming we do have something and we don't have null then what we want to do is we want to kind of iterate around our result set and return it back as our collection or enumeration of platforms this next line is a little bit convoluted i'm going to type it out and hopefully it makes sense and we'll go through it so we're going to use our an array object to convert all and we're going to use our completed set and i'm going to use a lambda expression and we're going to de-serialize json serializer and what i might do is do a control b to take down our file explorer so we can see everything on screen we're going to d serialize to what platform pass in val and then we'll mark that to list so a bit of a monstrous line but basically um the this set members if we actually come over and we take a look at what it returns we just hover over yeah you can see what it returns returns an array basically so we need to convert that array to a list effectively is what we're doing here which you know works with our enumerable and we'll return it return obj look on that object all right cool save and start and stop and restart our api and actually that's not going to work because we've only done our repository haven't we we've not actually done we've not implemented our action results so actually let's come on and do that before we forget so um maybe we'll stop this i'll bring up our file explorer by doing a control b and yeah we need to come back into our platform's controller because we have we've only implemented two endpoints so let's do our third one now so a new line uh we'll do the method first which is just a http get and coming back to our list of end points again you're pretty sick of seeing this now we're just doing this last one we're just using the controller level route and it's a get verb so we don't really need to specify her out so http gets fine and then it's a public action result and what is it returning it is returning and i immoral of platforms don't pluralize it platform objects and we'll close that off and we'll just call it get platforms get all platforms make it a bit more meaningful and we don't need to pass anything in and it's pretty simple we simply return okay on our repo calling into our get all platforms method it's a pretty simple one okay fantastic save that off let's restart this and it should should be working sort of so in insomnia i'm going to right click new request and i'm going to say get all platforms get quest create and i'm just going to copy okay let me go i can make this a bit bigger here that's better i'm just going to copy this because we don't want a uri we don't want our id and that should pull back here we go to objects from our set so it's not pulling back six objects six platforms it's just pulling back the two that exist in the set so it does work it's very quick you know and i actually i've not done it in this video but i actually pumped thousands and thousands and thousands of randomized um platforms into this and it's still mega quick and maybe i'll do another video on how i did that how i used um how about you pump stuff into it um it's still mega quick okay so actually it's not the best implementation by a long stretch but it's set so really quick really liked it anyway that sets we're not settling on sets we're going to move on in the next section in the last section to re-factoring your entire repository to use just one data type so we'll come on we'll do that next all right so let's just move on to the final part of implementing get all and we'll take a quick look at what we've done so far so well we haven't really done the scan command but we've given an overview of it and why we're not using it we've just used sets fantastic now we're going to move on to refactoring our entire repository using only hashes okay so we're going to move to one data type the hash which sounds amazing so let's just take a quick look at what hashes actually are so i need to be careful with my language here because it gets a little bit more complex so it stores values as field value pairs okay so we have a key that we've been talking to date about key value pairs the key is still there in hash but just forget that for a minute but looking just at the value component it stores it as field value pairs now all right so suitable for storing a structured object so it's kind of perfect for us so this is the kind of layout so each hash will have a key and then inside it can have multiple items each with a field and a value now the way we are going to store our platforms in a hash is we're going to have a platforms hash and i'm going to call it platformshe's called the platforms here and then in the field we're going to have the id and then the value we're going to have the whole platform object so what this means is that we can set individual items in our set in our hash using hm set so that's the if you're doing it at the command line that would be the command that you use hm set with the id of the hash the field value and the value value um you can get all items so again command line is h get all so that's good for our get all use case fantastic and that's how you would do that h get all the id of the hash and then you can also get under the individual items within the hash and you would use h get in that case and so you can then drill in to individual specific items using h get id the hash and then the field value which in this case will be the id of our platform all right so hopefully that made sense if not the code should clarify things so back over into our um project i'm going to stop the server from running now just a point of notepad we're in the controller we don't need to touch the controller again the controller code is written against our repository spec response repository interface we're now refactoring our interface to concrete implementation class of our interface but the repository does not sorry the controller does not care it's already written it doesn't care that we're doing this so again another real benefit of using the repository pattern with a nice interface so in our concrete class this is the only place we're going to be doing any changes so let's just work from the top down so in our create create method we're just going to comment out the use of strings and sets because we're going to move completely away from that and we'll make use of db hash set where we set our hash object so the first thing we need to supply is the key and i'm just going to call it i don't know hash platform and then the next thing is a hash entry which is basically going to be our field value pair so we'll say new hash entry and it might take a new line bloody tool tips and we will specify that so curly brackets new hash entry and then round brackets and the first i'm going to specify is the id which is going to be our field and that's just going to be equal to the platform id which we passed in at the top here and then the full value is just going to be the serial platform okay that's basically it for our create method so we could leave these in and we could continue to add strings and add sets entirely possible i'm just going to comment them out because it could get a bit confusing so we'll save that off we'll move down to getting all platforms now which is the next one in our list here and i'm going to copy from here and just comment this out because we're not using sets again so this is us getting everything in our hash so we're still going to put it into something called complete set just as we don't have to change the code maybe maybe we do that's maybe that's a bit confusing maybe we say complete hash which is what this video is it's a complete hash and then we'll just go through and change these here okay i think that looks better a bit more meaningful yeah that's cool and what we simply want to do is db is hash get all and all we have to pass in is the name of our hash key or our hash key and the name of our key the hash key itself perfect okay now if we save this off i think we're going to get an error down here in our kind of deserialization place here we are here for val we do just need to qualify what it is we're actually uh d serializing that is the value of our hash component so we have the field and the value so we're just deserializing the value part fantastic so that should get rid of that error wonderful and you can see we're not actually doing too much code you know the code is not changing too much and this is our individual get platform by id so again we don't need to i'm going to copy this and i'm going to comment this out and i'm just going to make that equal to uh db hash get and into that we expect the reddest key which is up here hash platform and we also want to specify the field value which is ultimately the id of our object and that is just id that's been passed in here as part of our method and the rest remains the same so not a lot of refactoring but enough so moment of truth is running up dot net run and bear in mind now we're working completely with hashes so there's effectively nothing in our redis instance although we've got stuff stored the strings we've got stuff stored as sets irrelevant now so coming back to insomnia if we do get all platforms we should get nothing we get no content fantastic this is not going to work it's just going to return or not found perfect so let's start again and we'll christen our new hash based implementation by making red as our first platform and not forget to put the double quote in so cool that's a good sign we get a created result back let us copy this and paste it into our get platform method that should pull back our platform it does and get all platforms we'll return all our platforms we just have one fantastic so let's do docker create fantastic get all platforms all platforms returns both our objects so the last thing we'll do is we'll just take a quick look in our tool here just to see how that looks so we'll do a bit of a refresh on this we have this hash platform and then you can see inside our hash platform so type hash and we have our key and then it's a bit difficult to see on this tool unfortunately i do apologize in fact maybe we change the screen resolution maybe we can do that and we can see how that looks it's doing it on my other screen but that's okay let's set the screen resolution down to 100 i think that's done it fantastic and we'll bring our tool back on there we go that's a bit better isn't it and we can see the whole thing so you can see here that we have our key i can't actually adjust that but anyway you can see it here you can see here here's our key and here's what's stored in the value which is the object so all looking pretty good so that's it that's it for the code i hope you enjoyed the video and you know in answer to the main question can you should you use redis as a primary db well like anything in the it industry the answer greatly depends i think you can but it depends on what you're trying to do now we've kind of created a very simple api here and yeah you absolutely can use it it doesn't mean to say redis is the best option in every case but in some instances it absolutely would fit really well and what maybe didn't come through in today's video is just the utter speed of this thing and it's really evident when you pump more data into it just how quickly this thing responds it is insanely fast so i really like redis by the way i'm not sponsored by redis or anything this is my own opinion i think it's a great great tool and the other just to aside a couple of things the redis documentation is excellent a lot of technical docs can be boring badly written confusing reddish documentation is awesome it's it's really easy to follow it's it's it's great so really recommend you take a look at that and the other thing with red is is it just works i mean a lot of time when you're playing with something new you try and you error and it doesn't work exactly as you think it would with redis it just works kind of almost exactly the way you expect it to work the first time which is really really real so big advocate big fan of redis so yes absolutely i think it would be a great choice for many projects as your primary database but that's not discounting all the other options that you have out there and we're very lucky these days because we've got so many things to choose from and it's really your job as a developer to pick the best tool for the job anyway hope you enjoyed today's video thank you for watching and i'll see you again next time [Music] [Music] [Music] [Music] dude [Music] [Music] me [Music] foreign [Music] [Music] foreign [Music] so [Music]
Info
Channel: Les Jackson
Views: 64,672
Rating: undefined out of 5
Keywords:
Id: GgyizgXwXAg
Channel Id: undefined
Length: 141min 21sec (8481 seconds)
Published: Mon Feb 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.