ASP.Net Core Distributed Caching - Redis Caching - What is Caching & How to implement Caching.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to coding droplets and  thank you for watching this video   so in this video we are going to see how we  can implement radius cache in a dotnet call   application so i request you to subscribe  the channel if you have not subscribed it yet   so that you will get notified once we upload new  videos and also i request you to support us by   liking and sharing the video so now let's start  the session so first of all let's see what is   radius so this is the radius website reduce dot  io and here you can see redis is an open source   in memory data structure store used as a  database cache and message broker so we have   already done some videos based on the message  brokers like mqtt, rabbitmq etc so you can see   those videos in our channel. so redis also can  be used as a message broker but in this video   we are not going to use redis as a message  broker instead we are going to use redis as a   cache data storage so from our application a dotnet  core application we will be saving the data to   redis and from our application itself we will we  will be able to retrieve the data from the redis   so we will be using our replica oh sorry  we will be using redis to store the data   but it is not an actual database it is for it is  an in-memory database so i will explain more in   detail while we explain uh the implementation  and redis can be downloaded from this website   itself so here you can see the options for docker  and if you are using linux you can download it   like this so for db and ubuntu all these details  they have given now if you are using a windows   machine then you can use this url it's a github  url github.com slash microsoft archive slash   redis slash releases so here you can see multiple  releases of redis so this is a pre-release 3.2.100   and yeah here there's a latest release 3.0.504  so this is the latest release while i'm   recording this video okay so you can download it  from here so there is a 64-bit download option   an msi msi file you can download it from here  so you can download it and install it in your   machine if you are using windows otherwise you can  go for these options in redis website itself okay   so now let's see how we can implement uh this  radius cache functionality in our application   so just before starting the development let  us discuss about the use cases of redis cache   so you might be thinking if you are  new to redis you might be thinking what   is the use of redis cache and why i need to  implement redis cache in my application so let   me discuss about an example so just think that you  need to show some dashboard in your application so   here i'm showing a sample dashboard so here we  are showing so much uh so many values like total   revenue order status income status customer status  world sellings and the values the percentage of   sales in each countries then some summary  here also we are showing so many values   so basically if we need to show these kind of  dashboards we need to process our records in our   database so we will be having so many records  like the products uh the sales records uh then   the customer records so many kind of tables will  be there in our database and we have to process   we have to calculate the sum the count and all  those things to show these kind of dashboards   so if you are having a huge data for processing  these kind of values it will take some time so   when a customer or when a user is accessing  your dashboard they might have to wait some   time for the data to get processed so in  order to overcome this what we can do is   we can process this data and save the processed  data into reduce cash like we can save these kind   of values like total revenue or the status value  income status value all these kind of values   in redis cache so what we can do is either from  our application itself we can check whether the   data is available in redis cache and if it is  not available then we will process the data   and we will save it to the redis cache then we  will display the values to the user so next time   if some other user is coming to the dashboard  again you don't need to process the data again   we have already processed it and saved it to the  redis cache and we can directly fetch the data   from redis cache as it is an in-memory database it  is very fast to retrieve the data from redis and   we can directly show it so the performance will be  much better compared to processing the data from   the database okay so this is one use case also we  can use redis cache for saving some temporary data   so if you need to save some data temporarily for  few minutes or hours then you can use reduce cash   so these are the use cases of redis cash now let  us check how we can implement this reddish redis   cash functionality in our application so now  let us create a project a dot net co project   in order to implement produce functionality  so i'm clicking on create a new project and let us choose sp.net cool  web app in pc clicking on next   and i'm naming it as redis demo and  let me choose my okay i'll save it   in my desktop itself fine now clicking on  next okay the target framework i'm using.5   and i don't need config for https so removing  that okay fine now i'm clicking on create   so now it is creating the project you can  see a folder has been created in my desktop okay i think it got created yes so  now the project has been created   you can see the project files  in the solution explorer now the first thing what we need to do is we  need to install a library in order to implement   redis functionality in our application  so in order to install the library i am   right clicking on dependencies and  selecting manage nugget packages and now you can see there is no packages installed   in our application now i am going to the browse  tab and searching for microsoft here it is here microsoft.extensions.caching.stack exchange  redis so there are multiple libraries   for implementing redis functionality so here in  this demo we are using this particular library   for implementation so i have selected this  one and the version now i'm using 5.0.1 uh why because in 6.0 uh i have  seen some issue but i don't know   the uh whether it is a bug or not so anyway   you can try with the latest version so if some  issue is happen you can just downgrade the version   and try it so 5.0.1 is the one which i'm going to  install now so i'm clicking on the install button so it is asking for the confirmation okay i accept  okay fine now the library got installed and it is   showing that an update is available but anyway i'm  not going to update so now let us confirm it by   going to the dependencies and inside packages  we can see that the library has been added   so using this library we are going to  implement the functionality now let's see   how we can implement those so the first thing  what we are going to do is adding the dependency   so we need to open startup.cs class and  in this method configure services method   we are adding the dependency here so  services dot add stack exchange radius cache   and we can provide the options so i'm just  using uh providing options such that okay okay options dot configuration so configuration  is for connecting to the radius service   so the configuration here i am providing is local  host column 6379 so 6379 is the default port   number for radius and redis has been installed  in my local machine so i have used localhost here   okay now the next thing is options dot instance  name is equal to we can provide a name for the   instance so i'll explain how this instance  name work so here i'm providing it as radius   demo the same name of our application  okay so the instance name is used for   um while we are saving a data to redis the  radius will save the data in key value format   so there will be a key and a value so for each key  redis will add this instance name as a prefix so   that you will come to know once we implement the  radius functionality i will show you in detail   how this instance name works you can also read  these values from the application settings   so that will be the best option so that you  will be able to edit it anytime when you are   deploying this application so what  we can do is we can open the app   settings.json and i'll do one thing i'll create  a new section for radius radius connection okay now inside that i'm giving  two by two values configuration it is local host colon 6379 and instance name radius demo okay now we already know from  startup class we will read the values from   this application settings json file so for that  we can provide it like this configuration dot get section get section of radius connection  so this is the section okay now dot get value of string and the key is configuration now in the same way i'm just copying this we just need to change the key  here so the key is instance name fine now we are reading the configuration and  the instance name from the atp settings.json file so now in our application let's go to the home  controller there is a auto generated controller   class so anyway we don't need all this i am  removing everything inside home controller   and remove the unnecessary usings as well   fine and also we have some model class anyway  we don't need this one as well removing that now in home yeah there is a index some auto  generated uh view i'm removing those   shared uh yeah let layout be here but we are  making some changes we don't need all this   anyway we are not going to create any designs as  this is a demo application we just need a simple layout file okay fine now in home controller   i'm creating two different methods  the first one public action result so let's name this save redis cache okay and inside this we are just giving return   view so first we need to create  a view for save radius cache so let it be an empty view and the name is save redis cache okay fine the view has been created so here in  this view we just display message radius   cache has been created okay fine  so now first let us implement uh   this functionality save data to reduce  cache so for that i'm creating a model class and naming it as dashboard data so let's  assume we are developing an application   which is having a dashboard and in the  dashboard we have to show some values okay so   the values to be displayed in the dashboard  we will save the values or we will   yeah we will use this model class in order  to save the values in radius cache so let us create some values public int total  customers so or total customer count so   let's say it is showing a value of the  total number of customers okay now next total revenue then um what else okay uh top selling product  name so this should be a string variable   the property should be a string data type okay now let's see one more thing okay now  another thing is public string top uh selling country name so it just holds a name of the country in which  the maximum sale codes okay fine uh anyway   for this demo purpose we are showing these four  different values so now what we are going to do is   uh we will be assigning some values to this  data so in a real application what you can do is   you can fetch the data from your database the  real database and you can process the data then   you can save it to reduce cache so now for  this demo what i'm doing is i'm creating for dashboard data is equal to new dashboard  data and i'm assigning some hard-coded values   so for example total custom  account is some hundred thousand 450 okay then so in real application  you have to count the number of customers and   assign the value to this property  total customer count of dashboard data   and our total revenue is say  some i'm giving some value okay fine now top selling country united states okay  now top selling product okay some macbook pro fine   so we are showing these four values we need to  show these four values in our dashboard so first   what we are going to do is we are saving this  data in our radius cache then in our dashboard   we will retrieve the data from redis cache  and display it this is what we are going to do so in order to do that first i am  creating a constructor of home controller and as we have added the dependency  injection now what we can do is here i can   give i distributed cache so for that we need  to use microsoft extensions.caching.distributor okay now distributed cache and i'm declaring  a private variable same i distributed cache okay undiscovered underscore distributed cash so underscore distributed cash is equal to  distributed cash okay so now we will get the object here and we are saving  it to this private variable and   now next thing what we are  going to do is for saving   a reduced cash first we need to declare a  distributed cash entry option so distributed   cash entry option options is equal  to new distributed cash entry options now in this options we can assign some  values the first one i need to explain   is about absolute expiration relative to  now so here what we are going to do is   we are going to assign a time span of a particular  period for this radius for this particular   record in redis so for an example in our dashboard  data will get okay in a daily basis we need to   update so tomorrow our dashboard data uh data will  get changed so we need to include uh today's data   and we need to show the uh new data tomorrow  okay so in that case we only need uh or we need   to we need to make this record expire once  uh we complete the current day so next day   early morning itself this data should  expire so for that first we need to get the total number of seconds  okay so war total seconds   total seconds or minutes whatever you can  but here i'm using seconds okay is equal to   or else uh just before that so i'm getting  tomorrow's date so tomorrow is equal to date time dot now dot date we are just removing the  however minutes and seconds everything from the   date we are just taking the date today's date here  now dot add days of one so automatically it will   now this variable will get tomorrow's gate  okay now the next thing what we need to do   is we need to calculate the total seconds so  total seconds is equal to tomorrow dot subtract   daytime dot now okay dot total  seconds we will get in total seconds   five so now we got how many seconds we need this  data to be valid or after this many seconds the   data should expire okay now we are assigning  this to absolute expiration relative to now   is equal to time span dot from seconds so if you  are calculating minutes you can use from minutes   here so as now i have taken the seconds i have  um uh i have given from seconds of total seconds okay so from now once this command got executed  so the option will be having this cache will be   having an option assigned that this particular  record should expire within this mini seconds   okay now the next thing what i need to  explain is distributed cache entry options   dot sliding expiration so sliding expiration means  if this particular data is not used for some time   say so for uh for example if this particular  record is not used for another one hour then   if we need to automatically expire this data  then we can provide that uh time span dot from   hours what so now what will happen is after one  hour if uh none of the application is using this   particular record then it will get expired  but here in our scenario we don't need   this to happen so i'm just providing it as null  okay so now we have uh declared the distributed   cash entry options now the next thing what we  need to do is we need to convert this particular data of this particular object to a  json string so we are going to save   this data as a json string in redis cache so in  order to do that there are so many libraries to   convert an object or a list to json so i'm using  i'm installing one library named newtonsoft json okay here it is i'm installing  it this is a very famous library to serialize or deserialize json objects   okay now what i am doing is for json string  or json data is equal to json convert so here we need to use newtonsoft.json dot serialize object of dashboard data so  now you can see this json data is a string   data type so this object will be converted to a  json string and now we are going to save this json   string to our radius cache so for that distributed  sorry our this object distributed cache object   dot sit okay we can use one uh asynchronous  method so set string sync so in order to use that   we can make this method asynchronous testing task  of action result fine now that is asynchronous   so here we can have weight this okay in order to use task we need using  system.threading.tasks okay i'm adding it okay so redis will save the data in a key value format  so we need to provide a key say i am providing   the key as dashboard data so using the same key  we will be able to retrieve the data okay now   the next thing we need to provide is the next  parameter is the value so that is the json data   and the third parameter it can be the distributed  cache entry option fine this is enough so now   once we run this or once we open this  particular view save ready cache view   then it will save the data in redis cache so let's  see how it works uh let me run the application it is getting compiled sorry some error is  there somewhere oh okay uh that is because   we have some error model uh error view  here we don't need this we can remove it okay uh those views are auto automatically  created while we create the application   create the project okay so i have removed it now  i think it should work okay fine it is working   so now uh let me check our url it  is home slash save radius cache   okay so just before running it i need to introduce  you one more application that is redis inside   this is an application which we can  download from radius official website itself   so you can just google for radius insight and  you will be able to find out this application   so once we run this application it will  automatically run an application in the local host   and we can add a radius database so for adding  a reduced database we can collect your radius   database using hostname we can provide localhost  here then the port number and we can name provide   some name and save it that is enough so i  have already saved it and it is showing here   so now we can see we have zero keys so we have  not created any keys yet and one connected client   so the connected client is the same uh this  application this radius insight is connected to   the redis service so that is the one application  now our application is not yet connected   so now what i'm doing is here i'm giving home  slash uh what what is the name save radius cache it is cache yeah now you can see uh  in as we have given in our view it   is showing radius cache has been created  okay now let me open this on redis inside   okay now you can see there  is one key has been saved   okay we can see this key here if we click on  the browser here now you can see the the name   of the key is radius demo dashboard data  but we here we have given the name as   dashboard data the key name is red dashboard  data so this is what i have explained uh   in the previous uh sorry explained before while we  pro if we provide an instance name this instance   name will be used as a preview prefix for each  keys which we create from this application   okay so this is the instance name redis demo  and dashboard data is the key name actually   so the application will create  will concat both instance name and   cache key name then it will create the key  okay fine now the key has been created and um now the next thing what we need to do is  we are creating one more view public async   task of action result uh say dashboard  i'm not about giving any uh i'm not   designing anything just showing the data  as it is a demo so written house return   view now we need to create a view for this  okay the name of the view is dashboard fine so now we need to show what  are the values we need to show   okay total customer account so let's say  this total customers is something okay   here ur dashboard data is equal  to view bag dot dashboard data okay and the type is dashboard data  file so we will be sending from the   backend we will be sending using view back  so that we have not yet implemented it but we   will implement it soon so first let me provide the  design so um total customers is dashboard data dot   total customer account then the next line  what is the next data total revenue okay total   revenue is dashboard data.total revenue then next  line what's next um top selling country okay top   selling product and country we have both yes  uh top selling product and top selling country just stop selling country okay now it is  dashboard data dot top selling product name   here we have to provide dashboard  data dot top selling country name okay fine now what we have to do here is we need to  retrieve the data from radius first so to do that   i'm just okay well json data is equal to  distributed cache dot get string async   now here we have to provide the key so we we don't  need to provide the instance name it it will get   automatically added we can just provide the normal  key name here okay that is enough now next thing   we need to convert it to the dashboard data object  so for dashboard data is equal to json convert   dot d serialize object now here i am giving  the data type it is dashboard data okay now the   string which we need to convert is the json  data okay now in this variable we will get uh   the dashboard data here it is showing  oh okay here i have not given the await   keyword okay now it is fine now the next thing  what we need to do is we need to assign the value   to the view back so view back dot dashboard data  is equal to dashboard data fine now i'm running it so now our we need to open home slash dashboard   and here you can see now it is showing the  total customers total revenue top selling   product all these things so now all these values  have been retrieved from our redis cash from you can open the radius inside so  here we can see we have a key here   but this particular key or this particular  record will get expired uh the very next day   so after that after the sec we have calculated  the seconds uh for expiration so after the   completing those seconds it will get automatically  expired so again the next day we have to calculate   the values or process the data from our database  in a real database we have to process the data   from the database and save it to the redis cache  so in a real example what you can do is you can   create some service application that is one  option uh or the the best i think that is the   best option if you are having a lot of big  data so using a service application you can   fetch the records and calculate or you  can completely do the calculations from a   service application and save the processed data  in redis and in dashboard you can just display the   data by retrieving it from the redis cache so hope  you enjoyed the video and you are clear about the   concept so if you have any doubts please comment  to the video i will reply as soon as possible   and please support us by liking  the video and sharing the video   hope you enjoyed the video so see  you in the next video thank you all
Info
Channel: Coding Droplets
Views: 4,189
Rating: undefined out of 5
Keywords: aspnet core caching, aspnet core redis, aspnet core distributed cache, aspnet core save data in cache, aspnet core redis distributed cache, .net core redis distributed chache, .net core distributed chache, dotnet core distributed cache, aspnet core temporary data cache, aspnet core cache data temporary, aspnet core how to distributed cache, how to distributed cache dotnet core, aspnet core distributed caching, dotnet core distributed caching, how to redis in .net core
Id: 4Br-QnBo6Yw
Channel Id: undefined
Length: 37min 59sec (2279 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.