Spring Data Redis in Spring Boot Example | Tech Primers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] let the bell icon on the YouTube app and never miss any updates from tech timers welcome to tech primers in this video we are going to create the spring data aratus application in a spring good application right so as you shall be adding the start of spring the diode in the previous video we saw a basic introduction about what's your status and where it is can be used in this particular video we are going to see how we can write a spring both application with which we can access a ready server so I'm going to quickly create a spring boat application here so I'm going to say it comm tech primers cache and let's name it rate this example I'll just say spring rate this example right so the dependencies which we require are the web so since I want to expose this as the rest endpoint and from the rest endpoint I am going to hit some service that is going to insert the data into the database readers database and it is going to get the data back to the user so in order to use Redis we need the Redis dependency so if you see there is a writ of dependency here this is going to give spring data it is and also the jet is so Jadis is nothing but an implementation for the java so there it is client for the java so it has lots of libraries inside Jadis which with which we can connect to the ready server by default and spring has written a wrapper around spring the Jettas and we are going to see what is that so let's quickly generate the project arange a pet and I'll just quickly open it inside IntelliJ so I have the project here let me open it so main wire let the project load so let's go to the dependencies in the form so you can notice that there are only two dependencies one is called the spring boot starter data or it is the other one is called de starter web so if I go inside the starter data it is you have spring data rate is as the dependency which is coming from the spring boot from the spring data framework and also the Jade is which is coming from the Redis client so these are two different dependencies which we require in order to access their it is server so let's go and quickly create what do we need to it so in order to get started so we need to have some connection factory for the spring boat application to connect to the writer server right so what we are going to do is we are going to create some basic configuration so I will just create the configurations here so we need to have a beam which is going to have D type J disconnection factor so this is going to have the connection factory in build so this is coming from the Judas jar right so that is why we imported that particular jar so this is going to have the jatis dependency so we are just going to create this this is going to create the Jetix connection factory so we are going to use the Jettas connection factory and then just create it so if you want if you want to have a custom hostname on the port number you can go ahead and do that so you can provide the hostname and hostname here all right this is okay so in the jetties connection factor you can throw the hostname at the port number if they are running in a different host on the port number so that is why we create the jitters factor here right the next thing is that we need to create a latest template so like how we have a rest template for rest calls we have a like JMX template for JMS calls so we have a rest rest template so which are which is coming from the string q notice that it is from the string chamber so we are going to create a Redis template for type string and user so user so user in the model which we are going to create so which I will create it in a moment so I am going to create a Redis template right so I'm going to say R it is template of string comma user equal to new read this template right and I am going to set the connection factor to that particular Jadis factory so what I'm going to do is I'm just setting the jet is whatever it is function right so I'm going to set the connection factory on to the red is template which is created by spring so this J disconnection factory is created by us with a default implementation if you want to have any hostname and port number either you can add that here and this particular connection factory we are plug it into the Redis template which spring uses and we are having a model called user so let's go and create this particular class I am going to create it under the model package and I'm going to implement the serializable interface so the object is serialize now I'm going to add the variables here so I'll just say I need as ID and also I have a name and also the salary so let's say salary is long right okay and I'm going to add some methods here so I'm going to say getters and setters I'd need some critters and critters also I need some constructors right so I'll just say constructor and I regard everything of the argument to the constructor so done so our model is ready our configurations are done I think I had returned those configurations here we'll just say it is it is template and this is not required here so this removed that's it so we have added our configurations which are record for the red base to be up and running so let's go and quickly create our sources so before doing the resources we need to connect to the Redis and get it get the data right so template we have created but we need a repository isn't it so I'm going to create a user repository which is going to be a interface so unlike the other springboard projects right so Spring has not created any JPA type of implementation for it is so we have to rely on our own implementation for reddit so what I'm going to do is I'm just going to create a user repository like how we traditionally do it and I am going to have some methods like save so I just say save users either and you can save this user today so I'm going to say method called save user and I'll just say there is a third called write list of users which is going to say find all right and let's say there are methods for what do you have here update right so we need to do an update isn't it so I will just say update you're doing a user and finally delete it I think you can do a ID right now you can do PI ID okay so for the update we need the user for this we need a user and let's say we want to find by a particular user right so I'm going to add one more method find by ID so that we can find by a particular ID right so now we need to create an implementation for this particular interface so I'm going to create an interface implementation with this and I need to annotate it with a repository because we need spring needs to know that this is going to be a repository so we are going to use the typical repository implementation so that spring knows that okay this is the repository alright and what we need is a template here so if you notice here we created the register plate it previously in a configuration so I am going to add that Redis template here so that we can use that it is template for a interaction gatir it is over so this created it and I'm going to add the constructor here so there is one more object which we need because from the ready server you can get some data and then you cannot use the Redis over directly so there is something called hash operations okay so you have to use hash operations to operate on the rest template so what I'm going to do here is I'm going to use the hash operations and then I'm going to assign the writ is template dot ops for hash so we are going to use the off hash operations to access the register plate so in the save we are going to say hash Operations dot put right so put is like putting the value into the Redis cash so we are going to use some index so basically that is the key right so we have to use a common key so for a user since it is the user so what I'm going to do is I'm going to just create a common key so I'm going to say the user is the key right I'm going to have user as the key so let's make it caps so that we know and I need an ID right so I need a key in the value so I need to get a ID here so the key is the user but I need to get an ID site so I'm going to say ID and I am going to pass the user so what is going to happen is inside the user I am going to have a value which is going to be an ID which will be having types of users okay so this is what is going to happen so the hash operation dot put is going to save the tree so we have implemented the save operation we have written the implementation for it using the hash operations so for find all what we are going to do is for find all there is a know there is an operation called entries where you can find all types of objects or the value so we can just say hash operations dot entry and then you can throw it what is the key for it user is the key for it and then we will get all the list of users right so and the only thing is it it's not going to be a list of users because it's going to return our key value pair so it's going to be a map so previously we used to have list of objects but here it's going to be a map of your string and map of string and user right because that is what we have inserted so where is it I think I need to change it in the interface let's go and change in the interface so it's going to return a map of string common user okay if you notice the area yeah good so entrance is going to return a map of string come on users so for the update we just need to do the same thing so you just do a put so nothing is going to change here because it's just going to override whatever we are going to have right in fact we can even call these saves here right to save the occasion so in the delete operation we are going to do a delete so we have to do as operations or delete and we are going to provide the key which is going to be the user's right and the ID so we are going to do that that is so we have implemented the update delete find out but we have not implemented the find by ID so here we have to do a get right so we need to get the operations so we are going to do a get and we need to provide the key here so which is the user and also the ID so with which we are going to get and we need to convert this into user that's it all right so our implementation is ready so our user repository implementation is ready now we need to write a more resource so that we can use the resource to get the data so we are going to create user resource so this is going to be a typical spring MVC type so I'm just going to create a risk controller as a cost mapping and want to say a red slash user right and I'm going to create all the implementations that repository implementation rate so I'm going to say user repository it is I'm going to create this as a constructor now let's create some rest endpoints with which we can add some data so what I'm going to do is I'm going to add some get the rest endpoint so that I just say add and I'll just say what is the ID so for simplicity I'm not going to use the post as such here so also I need to use a model and also I'm going to use an ID and then I'm going to say some name right that's it so I'm going to add some default value just for our convenience so I'm going to return I'm going to return user itself so that we can show what user got added right and these are going to be part variable ID it's going to be a string in the path variable which is nothing but name now we need to use the repository to do what see right so before we do the say we need to create a new user so we are going to create the user the ID name and by default I'm going to add the salary for everyone as 20,000 right and once it is saved now I'm going to just merge the get operation also into this so we are going to use the find by ID also into this so that I can return that back to the user so I'm going to use user repository table defined by ID back to the user all right so add operation is covered here and also the Select operation is covered next I'm going to add one more as 10.22 a update operation so update is going to be the same I'll just say update and in fact it's going to be the same right so I'm just saying complain right so we are going to provide an ID with a new name so the new name will be updated and I'm gonna update this altri here to a different value right so that is the same way we are going to do a find by and we can add a find all here I just say all so we will not have anything here so let's do a map so this is going to return a map of string comma user right so that's what we saw so user repository dot so we need to do a fine PI final so fine by sorry find all right so and we don't need to provide anything we just say find all and this is going to return a map so I'm just going to say return and done so this is going to return all so let's quickly go and check this right so I'm not sure if this is going to work so I am going to use the port number 80 82 right I will just quickly start this server so before starting this we need to study our ready server so I have I'm in the reddest path so I just need to start the reddit server so I'm just going to 0 disabled so notice your how quickly the server came up right so let's quickly start this hopefully this should work now so the application is coming up hopeless it has come on yep it has come up and luckily there is no error that means this has connected successfully with the red seven right so 80 82 is the port number so let's go and do some rest call 80 82 slash red slash user slash all that's what we added right so this should not return any value right now so if you notice here there is nothing so we are going to add some user here I'm going to add an ID called one and I'm going to say Peter so this should add a new entry Peter with 20 cells yeah notice notice here that Peter and ID 1 got inserted and then it got retrieved right I'm going to add one more I'm just saying add and then say Sam this is going to add Sam and it is going to return some now if I do it all this is going to do a written off both the salmon Peter so if you notice here the key with which we stored was the ID so that is why the ID is coming up in the NAP because that is always stored it ok so now if I do an update I just say update and then I am going to say I need to update to with the name of 3m right I'm going to update Sam's name and also the Sava gets updated because I already updated this and we get the update ok now I am going to do all if you notice a Peter and Ryan is coming now with a new value so this is you notice right how quick it is comparatively to how we wrote our springboard application with JPA is another MongoDB or other database light so this is very fast because it seems a reddish cache right so that is it bottles we wrote we wrote a delete as well right so I think I didn't write it in point for delete so let us write a rest in point for delete I'll just write that in point and then I'll just summarize in finish the video so I'm just going to do a delete of the ID identity for the user so I will just say delete and here I just need to say in this return true or false or I can even return the hold list rate I can get on a map of let us return whatever is there so I'll just do a ID I don't need the names here it'll just say delete and I'll just throw out the ID that's it and when returning I'm just going to say do one all actually I can rename this as long it yeah so I will just return the data full data completely which is all there so just we sort this server so since the cache is not restarted since the written test is not restarted the data will be still available for us because we are just restarting only these thing both application not the reddish cache right so the data will still be available for us so just changing some formatting stuff so also once the video is done I am just upload these examples in the github repository as usual you can get it from there so all my projects are in the github repository you can get it from there if you are not aware you can see all the projects there are more than twenty or thirty projects which are out there with which I made the videos on ok so the server is up now so let's go and quickly do all to see if the data is still there yeah the data is still there now I am going to do a delete here so I just say delete and then I am going to delete off crime so if you notice here when I did delete off I'm Peter key I just had one more I'll just say three and then I'll add Sam right so now let's do it all and we should have Peter and Samuraizer good so that is how a dish cache works if you notice here we have added all the insert update delete find all and find by ID operations using the spring data radius so it was a very simple right so we just created our own implementation but still it was much simpler than traditional Redis usage if you are not using jatis you will have to do lots of stuff since we are using jettison spring it has removed lots of work from our in and we are directly using it why we hash operations alright so this is how we create a spring Buddha plication with spring reddish cash so we just the only thing which we did here is we create the edge it is connection repository because that is the repository the disconnection factory basically so the connection factory is the one with which you connect to an external credit server where you can give a localhost and force number basically you can give you the host name at the port number so in our case it's localhost in the default port so I didn't give anything here if you have any custom title this server you can give that here and then we are going to create a rapist template inside the radius template we are going to inject the connection factory so that way Spring knows that or the spring boot application knows that this particular this template is going to connect to this particular Redis connection factory so that is what is the additional thing which we require apart from that we created a model which we need to serialize and then we have created a depository interface we implemented the repository interface and added a atropos a tree annotation and they wrote some code here with which with the hash operations we are using the rest and player it is template to access the that is cache and then we wrote and then we finally exposed our rest endpoints using the spring MVC style in da use the auto source and then we queried the repository updated the repository deleted the repository using that using views that are positive so that's it for this particular video hope you guys liked it if you want me to do any specific topic on any any specific technology do let me know in the comment section below if you liked the video go ahead and like it if you haven't subscribed to the channel go ahead and subscribe to it meet again in the next video thank you very much [Music]
Info
Channel: Tech Primers
Views: 87,385
Rating: 4.8446217 out of 5
Keywords: techprimers, tech primers, redis cache, what is redis, redis implementation, spring data redis, redis example, what is redis cache, why redis, when to use redis cache, redis fast, cache redis, remote dictionary server, redis server, what is redisserver, redis tutorial, spring redis, spring boot redis, spring data redis example, spring boot redis example, redis in spring boot, spring boot redis tutorial, redis
Id: eYfopvusG_s
Channel Id: undefined
Length: 23min 42sec (1422 seconds)
Published: Fri Aug 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.