Project Setup & Practice [AspNetCore, C#, Redis, Distributed Caching]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everybody in this video we're going to take an overview of a web app essentially in this asp.net core api that will be interacting with redis instance in the cloud once we publish it we're just going to take a quick overview what the setup is in order for it to run in the cloud in the next video i'm going to show you how to get it all there otherwise i also have a couple of other videos that explain more or less similar things so you should go ahead and watch that because in the next video i'm going to be explaining everything i'm getting pretty tired of saying the same thing over and over so go and watch those videos on docker etc let's go ahead and get started for the program cs just a little amendment of the urls so it actually works in the docker pre-standard docker file i'll just run the net publish manually and then execute the docker build manual as well i'm not going to set up any ci cd pipelines startup pretty self-explanatory we're just adding controllers we have the stack exchange redis we get the distributed cache interface if we are in development so if i'm locally i want to target the local instance and because i don't have the whole devops thing set up as i'm going to be spinning up or creating my instances i will set the variable in the google cloud console and i also have some three services registered here we will find out what they are as we go through them yeah they will just they're just there to illustrate some examples and some ideas that i want to relay other than that we have the default controller where we have endpoints for interacting with our services the services themselves we can take a look and none of them are too big all of them are very small and again as i said they're just to highlight ideas and then we have the ad cache extension static class and this is where we will do most of the coding because a distributed cache interface is not very rich and but you can create a lot of bulk around it let's take a look service one will be the main service that we're using in order to test if the cache is working or not uh we will do a little bit of a delay before creating the car and i'm essentially simulating read from a database but a very very long read okay so let's take a look let's do a little bit of an a side reading we're not going to do a lazy read because a lazy read implies that the caching service is going to read from the database and backfill itself aside we hit the cache nothing's there we hit the database we store it in the cache okay so we have the cache dependency injected again it comes from the startup i watched the second episode if you are not sure how that works in the cache we want to go ahead and get async and to string so it's an integer we'll just have something at one and we're going to have the value we'll evade await this and now remember that all of these return byte arrays and for this i'm going to do get string async and we're going to use json serialization it just makes storing objects like cars so for example in my case i have a car storing a car object and cache a lot easier then we want to check if string is no longer empty we'll check the value that means at this point it's not inside the cache we want to preload it from the database before that let's go ahead return a new we'll say that it's cached equals true and let's say the data is json serializer d serialize r and value here and no semicolon then we take it from service one get car async we just supply the id we await on it and we have the car we then want to jason serialize this and store this inside the cache so let's grab the cache set string async json serializer serialize r and obviously the key goes first so and to string again we'll await on this and this is essentially saving it in the database we can then just go ahead and return a new okay object here we'll say that it wasn't cached so we'll just indicate false but a semicolon here or actually i'm forgetting a race and actually i just spotted that one too many braces there and for this i don't need to deserialize the car i already actually have one there and yeah once we return this object that's when that when it's going to research realize it we've got that let's come back to this end point and let's refresh you will see it's kind of spinning it's waiting and that's the five second delay that i have in service one and once five seconds pass we essentially get cached false and then we have the data we refresh and then it will basically flip to cache true and yeah i mean that's it if i have a little bit of a from a query you can supply into n if you want to play around with it with it so if we want to try a value that hasn't been stored there before we kind of we just bump up the number okay so we'll have number two here we refresh this again this is cached and every next time we get it it's instant now coming around to the cli let's just go ahead and refresh our memories on how this works we can use keys to see all the keys in the redis store right now you can see two and one let's go ahead and get the value behind one and two if you remember it was an h map when we took a look at the implementation in episode two so h get one and if you remember the field you can pause the video here and try to remember it otherwise it is data and you can see that the quotes are escaped this is essentially a string that we are storing here we can also do the same thing with the number two so we have volvo one volvo 2 and then i'm gonna bring up a point about again i'm going to remind you about stack exchange dot redis we can use the dell command and you can see the first thing we have to specify is a key and square braces generally means optional here we can specify many keys so in one request we can go ahead and delete two things and this is why maybe if you want the more performance option you cannot do this with this cache here you would have to execute two requests to a remote instance so just keep in mind again if we just look at all the keys the other two are gone so one command we clear two instead of doing two api calls and just to take this a little bit further again we'll take a look at the implementation so we are going to go inside stack exchange redis we'll take a look at the redis cache and taking a look at the constructor the connection multiplexer isn't an instance that is getting injected into this library so it's not like we can have something that we can extract from there the connection multiplayer multiplexer and the cache the i database these things are maintained through these connect methods and connect async methods as well and this is where the cache gets edit etc right so just as a point you do not have access to that flexible tool that is a little bit harder to use but can't do anything you have this very simple interface that you can only interact with the a distributed cache okay so with that in mind let's look at this a little bit further what we're essentially doing is we're doing get or add get a value from the cache if it's not there get it from the database and get it from the database is a little bit specific uh we what we what we're essentially doing is we have something like a factory that creates a value just so happens that the value is being created from by getting it from the database what we're getting here with the service too is the same thing we want to get something from the database and the operation here will be much the same so we will try to get this string from the cache first and it will fail this right here is going to be very repetitive this is something that we want to abstract so let's just go ahead and copy this into the i distributed cache extension class and oh let's actually just put it inside the body and let's clean this up so the cache the underscore cache let's first replace it with just the cache instance that we're going to be extending the next thing that we need is the key and the key is a string so let's say key we can say key here replace the next thing here key and then we are git car async we're executing this method realistically as i said this is a factory it just needs to create this method so get or add async let's create a function it can accept a key and because we already sort of have the key let's go ahead and pass it into the function okay it doesn't hurt anybody otherwise it's an asynchronous function which can go ahead and create a type generic type okay we want to be able to create a car we want to be able to create a string we can we want to create be able to create whatever so this has to be a generic method let's go ahead and spread this out a little bit we'll say that this is a factory and then we can go ahead and do this factory pass the key into it and then we want to rename some of the values because this is no longer car this is an actual value and this is rather a value string or we can actually say that this is a json value right you can call it json string value for me json value is fine let's import json serializer we want to serialize the value here we don't have the okay method i do still want an object to represent the information if it was cached or not because at an example purpose uh i would say if it wasn't this example that i'm building go ahead and just return the value you don't need to know if it was cached or not okay so we're going to create a public record and we'll just say get cached value it will be bull cached and t value so this is generic and then we can replace this with new false and value remove the rest and to actually make this compile let's go ahead and make this task return like this of type t so this is fine then and we want to do the same thing again this is going to be true though remove the return here and the value json we just want to deserialize this to a t and adjacent value there we go the rest of this can go and there we go so we've ended up with something like this it's not perfect we can do things to it to amend it and i mean some of the things will become apparent but essentially all of this will result in being something like this so cache get or add async we pass the key so end to string we can't really use the string value because the service one requires an integer so we discard the key it's not too bad we get car async and we just pass the end so we we use a closure right this is going to have a result and then we just return the result okay so there are ways that you can do to clean this up let's pass this here we're gonna have two we're gonna get name async it looks uh something similar here we have kilo and get name async accepts a string id okay so the key will pass it here and we can actually just use it here right and pass key and key to here and because we the lambda function is essentially the same we can just pass the call to the function rather than executing a function within the lambda and at the end just return the result you will see this this is nice this is a little bit like what can we do to make this a little bit simpler and i know this is not necessary it's necessarily speaking distributed cache tutorial however when working with distributed cache you're going to work with a distributed cache interface and 90 of the cases and i'm gonna show you how to work with it okay so we have a type key which is a string so we're forcing this to a string how about if we don't force this to a string we say that the key can be generic so we'll have a key okay we can't use it but um that's not really a problem right so this can be any key and then the actual thing that we want is a key which is a string we can take the any key and we can do some uh switch statements right or you can do if statements uh what whatever you find easier we can say that this is a string k and if it is a string k return the string k right and that's fine otherwise take the any key and to string it this no longer needs to be a string it can be a var a compiler will figure out the keys on their own basically means now we can be a little bit more relaxed on the keys and we can just go ahead and say pass this key in here and we're still a little bit like okay we we have the key here that's being passed in if we hover over it it's still a string we put a number do we want to use into parse here not really so you know we just take it a little bit further the function that we have here just pass the actual key that we've been passing into it because i mean that just makes sense at this point right so any key let's come back here and it no longer makes sense to have something like this we can again simplify it to the same form that we have here get really async get the key or use this function in order to create it okay it can be a function on a service and these are the kind of methods that you can start building up and i would recommend don't try to create these methods straight away take steps and implement them twice once you see the repetition i'd say three times that's when you extract them i say and i'd say if you see the repetition twice uh hold off a little bit because it just might be a one-off unless you can use your expertise and say oh yeah i can actually see ourselves using this all over the place then just do it just do it right let's go ahead and double check that it all still works we'll refresh the number two it takes a little bit of time to load but there it is cached false refresh again we have cached true cool so it is all still working now let's go ahead again into get or add async and let's separate this out a little bit because what we're doing here is we have a couple of things mixed in the getting of a value and the adjacent serialization stuff that is a little bit mixed in into here okay storing objects using the distributed cache interface is something that you're going to do if you want to implement c sharp object serialization to binaries go ahead and do that i'm not going to stop you i'm not going to show you how that's done because json is a lot more simpler so we're going to have the async task i don't know if we will have a task but let's first do get a sync and we'll say get a type t and we don't actually need that we can have a string key and we can actually do something like this right so we have the cache with adjacent value let's just grab this interface over here this is small enough to have it on one line so we have the json value and we can still do this if statement here we will remove this thing we'll just return null here and otherwise we can go ahead and json deserialize the t with json value right this is not astronomically hard stuff again these are just to give you ideas with what you would do with the distributed cache or because you know once you have it set up in the cloud that's cool now you actually have to build up the tools for yourself to work with that infrastructure the null here uh the t can sometimes well we don't know if it's going to be an integer an integer cannot be an also we just say where t is a class right so it's something that actually can be null if you want you can specify a default value but make sure your method indicates that default value is not the same as null null in this case would mean it's missing from the database or sorry from the cache right if you get a null it's missing from the cache so now we can do something like get async and specify the type that we're trying to get which is the team so now we actually have the value in our case if the value is null we go ahead and recreate that value we also need the same constraint for the t to be null so where team class and then the same thing here we just want the value here so you slowly work up to having methods like these if you're thinking about yeah what about integer there are things that you can do with integers alongs and date times i'm going to show you with the daytime one so let's say and well the set hopefully you can imagine what the set will look like i don't want to make this video longer than it needs to be so public static async let's say task will say get along async again we'll just copy the same thing we will make this return along we can do actually get do the same here although we will go to bytes just get a sync key and we can say that these are bytes you can call this the this value as well anyway we get the value and at the end what you do is you have the bit converter and you can convert this to int 64 which is a long and put a value here and this goes both ways so if you want to set long async we'll have your long value set async and then let's remove this this isn't going to return anything and again we use the bit converter with the get bytes and we just put the value in there and super simple we don't even probably yeah we don't need the async here no state machine just return the task and now we can do the same for date time so let's copy this put this here we'll say get date time async say that this is date time we get a sync we get a value we still get the int 64. oh uh didn't mean to insert or this is going to be tick the converter ticks and then just return new pass text into the constructor and there you have it now if you go ahead and say set date time async long value value of a date time uh we kind of do the same but convert get bytes we want ticks and var ticks if you want you can put this out on one line but basically you do something like this you got ticks and then you set async and uh all is good in the world right so you got your primitives you got your objects this is how you store them and get them how you can have a little bit of repeatability of get or add you can imagine get or default the imagination is your limit here uh one more thing we're gonna take a look at service three i mean that's more of a simpler one again if we have the i cache key interface what can we do with it well if we have something like let's say i'll just grab the set and we have set async and we have it for t and we don't need the key here because it's an i cash key and i prefer here because this because i think i'm not sure don't quote me on this but i think this is no boxing and if you have an interface it does box we return cache set async and we can have value cache key and then just a value and we didn't actually create this interface for this part here so anyway we would do something like json serialize the value here and call set string right so hopefully you get the idea there if you implement something like a cache key and you're basically wouldn't maybe tie your business logic to it but you're kind of saying okay these data types that we're using here they are cacheable okay and making it declared in your code base is fine so the dude is cachable we can cache the dude let's take a look at what a save function because these are like get our ad what about write a site when we write to cache and then write to the database what would that look like right so we went away await cache and yeah i don't have the away here so let's just cache uh save async set i think i think that's what it was we'll just say get our dude here new i'll say one what is it that i had just name and i'll say bob yeah bob is good i'll say it set the dude async right so just because we say this is something that we can cache just favorite but put the dude in there right and that will figure out the key on its own i'm gonna wait on it and let's make this async how does the service look i mean the server is safe do they sync when we just save it as well and weight on this uh so this looks really simple now when you put it in your application it may not look like that again if you are kind of like how should i put it take a look at the previous episode where we take a look at the strategies and how the compute components communicate that might give you an idea of what's the best scenario for you nevertheless uh this will be it for this video thank you very much for watching hopefully you've enjoyed it in the next video we're going to be setting it up all up in the cloud don't forget to check the description for all the links if you have any questions ask them on the discord server or in the comments section have a good day
Info
Channel: Raw Coding
Views: 2,110
Rating: undefined out of 5
Keywords: c#, aspnetcore, dotnet, redis, caching, distributed, distributed caching, tutorial, guide, example, IDistributedCaching, strategy, strategies, theory, project, setup, production, hosting
Id: eNEAdd8J-WY
Channel Id: undefined
Length: 22min 18sec (1338 seconds)
Published: Wed Sep 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.