Cache with Spring Boot in Simple Example | @Cacheable | @Cacheput | @CacheEvict

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to my channel i am razni khan and today's video i'm going to discuss cassay what is a cassay and how to implement cassay in spring boot no so if you have not subscribed my channel yet please do subscribe it and hit the bell icon to get all the notifications from my side so that you will not miss anything and ultimately you will be get benefited out of it right so let's quickly begin so what do you mean by exactly this cassette no so cassay means a temporary storage suppose it's a temporary storage case that means it's a temporary storage right and it will helps to perform the application right it will improve that applications performance right so how it will improve the application performance and what exactly that one so let me tell you one scenario okay suppose you know that uh we have zip code so city we for that city we have different zip code right we have different zip code so suppose you have an application suppose this is one ui and you are searching with some city for some city with searching some city and here is this submit button submit button okay so this is the ui and here is that middleware and maybe either you can search it from api maybe you are calling some api to paste that one or maybe you are calling some database call where you have stored all those cities those things city with zip code you have stored right so while searching this city you are hitting that either api or maybe database right so for a city name that zip code is constant so it will never gonna change it's a fixed one right so maybe it's a rare case that it will change i mean i don't know maybe it will not change also it's a fixed one right suppose you are searching some uh here's some mother motherfury are searching mother poor okay so for madhapur maybe uh what is that maybe let's see what is that pin okay suppose it's five triple zero eight one no five triple zero eight one right so we are searching madapur then it's hitting to that either api based on your implementation maybe you are fetching those uh zip codes from api then that time it will hit that api call or maybe you have stored those uh city with zip code in db maybe that time it will go to db call right but you can see here that zip code is fixed suppose one time you search it will hit to api or db second time you hit with same motherboard it will hit the db or api right so here a round network roundtp is more but the data is fixed right so what you can do because the data will not gonna change you can implement a casey or temporary storage kind of thing right so here the temperature storage is there and here it's suppose it's madaboor and pin is five triple zero eight one okay so that time if you implement this case a what will happen whenever you will search fast it will check in that case a and if the if that value is present suppose for madhupur one entry is there in that case a then that time it will return from here itself right if that is present in case it will return from here itself right so that time just you can see we have re reduce those network round trips we are not calling to api or we are not calling to dv we are not calling to api or we are not calling to dv to paste the data instead we are returning the data from ksa memory right so that that time will get more perform performance why means we are not hitting db or we are not hitting api which will take more time right so if you are searching something suppose i am searching the chivoli the chivoli right but the data is not there in cases so that time what it will happen it will go to either api or it will go to db then it will paste that one and it will store to here gachiboli and some five triple zero triple zero eight two maybe then from here again it will return to your application right so this is that entire ksa mechanism how will achieve performance right because here those datas are fixed they're never gonna change right so instead of heating multiple time to database or api we can implement this case like this right so hope you got it now let's see how we can implement different cases right implementation now we'll see implementation we got what is cassay and why it will increase that performance we got that one right now let's see implementation right so in implementation how we can implement cassay by default not by default by your own you can implement maybe you you took some hash map and uh storing those fast checking in that has map those values are not values are there or not if it's not there then you are hitting 2db and then you are storing that result into that map it's a map right and storing into here then again you are returning from here so this is custom your own custom or your own implementation right but this is very overhead you need to write some map kind of kind of thing and you need to manage everything right so why should you worry about this one right already spring supports gaseo mechanism spring supports casse right so all those bilan plate logics will be taken care of by spring right so we we know we need not to worry about these things at all right so spring already supports casa mechanisms right so this is the entire thing so now let's see in working example so how will do in a spring right so let me open my intellij idea and we'll start working on cassie you can choose any ide based on your preferences so we'll gonna create a spring boot starter project so let me create some existing project this one let me quickly create file new project springboard assistant so jdk version whatever you have installed so i have both java 8 and java 15 recently i have installed java 15. you can go and here we can choose group id com dot com.java shtra dot c-s-e-h-e can say artifact id here it should be project name what project name will give springboard csesc jdk why means jdk today gonna see jdk say not in case we do have also different different implementation you can go for guava cassette you can go for eh cassay and we can go for radius cases so today we will see jdk jdk means it's a hash map best implementation then snapshot let it be and java jar packaging style and java version let me take 15 i have installed java 15 based on your installation java in your system you can choose that one see a cassette demo and here let me take one package and here okay here we go uh now let's see what are the dependencies we need to choose right so here we'll do some web application development also so let me check spring web and we'll get rid of getter status all those things so we'll let me take lombok as well lmb lombok i have selected then the main thing uh what is that one spring essay abstraction this is that main one we need to take spring assay abstraction this three is enough so let's next and next so this window let me take it will take some time to load that project resolve those dependencies yeah i have done so let me uh quickly walk you around through that bomb.xml java version 15 and you can see springboot starter web is there and spring boot starter web spring boot starter can say these two things we required so now let's see now let's develop quickly and packaging structure so that will quickly start so that same example also we will take based on the city name will return the zip code right so for that one let me take one controller package where is package c o n t r o l e r controller then let's take one more package for service a c r v i c e service and let's take one more package for entity entity here city that class name will take city okay sorry package in the city two attributes will be there city name and zip code entity will take enti ty entity here we go so fast what is that application and based on the city name will redrive zip code right so for that one we need city right one class city and here we can paste pri v80 private string i have some already code written what is that on city name and zip code because you know that if i'll type everything it will take lot of time still i need to type so here we took lombok so let's uh take the advantage of that one so add the data at the red data this sim this single annotation will solve setters getters and uh what else to string s code equals everything it will take care of that one and one more let me take this one as a builder builder also so that instead of creating uh by constructor that observes directly within the builder design pattern we can create that object quickly so we have done this one now let's take a service class where we'll implement the entire cassette thing so let me take this one as a case service right so here let me annotate with at the right component or at the right service those are stereotypes annotations which will make this class as a bin in that ioc container hope you know those things now here what we can do let me copy this one that's why i took this one so that i will directly paste alt enter already i have implemented that ct no or i have not saved okay city it's crying alt enter make public public sorry we should take the private one alt enter map and hash map alternator i don't know what is the problem okay so what i am doing because it's a dummy implementation i am doing here right dummy implementation means instead of storing into the db here i am taking and storing those things in a map right so if you see here here i am creating a constructor and in that in that no parameter constructor i am creating a hash map with key as a string and value as a city and in that map i am storing the city with their respective values so for high-tech city here i am creating a builder design pattern and creating that object with builder design pattern so city.build.city name i am passing it city and zip code i am passing that appropriate zip code here and string and city object i am putting here like exactly like here snagger is that key and in that value i am putting a city object so here nothing is there here i am creating a object with builder design pattern you need not to worry about this one city.build.cityname.zipcode.build so it will return a city object exactly like these three four more objects i am setting here setting it here so mia for the respective zip code and city name like this i am printing like this i'm taking right so now let's move to next now let's move to next what we can do here so here let's take two three right so what is that i don't know what happened okay my bad okay so now let's take two three methods right so first method is public will return the city right city get zip code and here what we'll do [Music] the parameters would be what city name so city name is what string city name you should follow those appropriate naming conventions so city name you can see it should be camel case and will return that city zip code city details now so here dot get with that city name okay so here i am returning that one return so if you are thinking where exactly we are doing doing that what uh casey thing right so here we can do that casey thing so on what method you want to cassay so at the red casable one annotation is there in spring right so what exactly this cassava will do first it will search so whenever you are searching with a city name right suppose i am searching with high-tech city name so if with that city name in case a is that city name is there right suppose one case a is there let me move back to that this one so this is that case a right suppose this is that cass a and in that case a case a values will be stored in key value pay right so if it's high tech city high tech city and this is that object right if it is there then directly it will return from cassay the same thing we are doing it here if you are annotating with address case label and if that parameter is city name the city name will act as a key right whatever the parameters we are passing that parameter will act as a key and it will check in that case say if with that city name any case is there right then it will return from itself it and then it will return from the case a it will not invoke that method at all right it will not invoke that method at all so this is the caseable annotation working principle so again let me explain so at that case level what it will do so suppose you are calling a method with a parameter so with that parameter it will check in that case a if for this name is there anything in that case a if it is there then directly it will return from itself directly to written from the cassay right if it is not there then it will go to the database or whatever that api method it will face that one from there and it will store it here that case a and again it will return from that guess a that value right so the same thing at that case a bill it's doing so that caseable what exactly it will take first it will take a case a name right so it will take a name or value maybe that one right value so casey name itself indicates uh what is this one city zip csc city zip caci right so this case a name what exactly this one so casey name indicates so casey name indicates one one has map so suppose city zip code ct zip code you have taken this name right so if you see here i am taking city zip code this case a name this means here one map will be there with that name right one map will be there with that name it it's indicate one map kind of thing you just think the thing in that way suppose suppose different methods are there and you have multiple city multiple case a name so that time multiple map kind of thing you can imagine right suppose maybe it's some other case suppose state city can say right you can imagine in like this way so one case a name itself one one map kind of thing right one one case map right so this is got that name now let's see what are the other things right so how it so how it will define those keys right so how it will define that key suppose here high-tech city high tech city right maybe swelling ground so high tech city so how it will identify that key right so that key can be identified from this parameter so based on your parameter it will identify that key right on what key basis it will store into that map in that case a right so if you have a single single parameter means that time this single parameter will act as a key itself but suppose if you have multiple parameters maybe suppose string some name i am passing or some string some something string name two right so that time how it will identify that key so spring itself it will take this three parameter on based on this three parameter it will identify a key for that cass a right so this is key concept so if you have only single parameter then that time this one will act as a directly key so if you see here if you are not passing anything default if you are not passing anything that time this one will take a key of that cassette so suppose if you see here key so let me take this city name as a key this is a expression el expression if you have single parameter then it's fine you don't need to define if you want also you can define like this so as this is a single parameter i am not taking this as a key let's remove this one yeah here we go right so this is that cascable right so first time it will come to here and from this zip code map it will return that that city city object will return but second time onwards with that city name high tech city in that map or in that case a those things will be present right so that time it even it will not hit to this method so let me give some system.out.println to check whether that method is invoking or not get zip code method called see yeah led okay here we go now let's implement something okay here we have done caseable now let's move to that our controller part right so let's write that controller here we go new java class cassette controller and let me annotate with this one at the red rest controller and let's take a get method public [Music] city get alt enter get city by name so it will return that jeep code right at the rate get mapping because it's a and here we can pass that url and here we want casa service right so cassay sorry okay service okay service and i need to auto wire that one at the red auto wired ctrl alt l and here i need to call that method zip code and here we want at a request param or query parameter at the red request param and we can pass that city name or name directly it's fine so string name and we can pass this name here okay we have done return okay so completely we have done so now let's see that casey configuration how we can configure that casey right so there are different implementations are there i already told so eh casa will be there guaba casey will be there redis case they will be there but today i am explaining here it's a jdk based map concept based cassay right so simply for that one what you can do we can do here at the red enable caching right with this simply annotation it will set up everything right so internally how it will work let me explain you here okay sorry so whenever you will implement this case say internally what will happen one case a manager object will be created can say manager right one case a manager object will be created and this will maintain everything so if you see a case a manager means so here the entire thing will be there right so you have created multiple cases right so this this entire thing will be there those cases will be presented in case manager and case the manager will handle everything so whenever you are enabling cassay so internally spring boot is uh pretty much intelligent enough it will create those beans right so cassie manage will be created by spring itself so based on that implementation so here we are doing the jdk best so that we don't need to do uh specific configurations because here we already using the jdk by itself it will create that cassette manager else we if you are using some other cases like go up a ksa or you can say yes casey that time explicitly we need to create those cassette manager or we need to do some little bit configuration to do those stuffs right so simply i will upload uh those videos also either es casa and guava casa gradually but today i'm gonna see that jdk best case right so for that one we don't need to do anything simply add that enable caching if you want to explicitly create that manager control hotel that case a manager you can do that one so let me auto wire that case manager sorry guess a manager manager ksm manager and auto wire at the red auto wired so with this case manager what we can access here if you see let me give it here you can see get cassay right get cassay means in that method you can pass that case a name then that case a will return suppose you want explicitly do that one you can do case names right so uh let's implement that that one as well so i want what are the cases available based on that name i want that casey so public can say alt enter get cassay by name so i want to see with that case what are all those elements are there right so that time you can explicitly also you can do like this case a manager okay manager dot get sorry here we'll receive that guess a name string case a name and let's pass this name here okay so let's return ctrl alt l now let's move to controller and in that controller we'll return that on public cse cassay get cassay details and here we can have that case a name one query parameter at the red red request at the request param and we can pass that cassay we can pass that name and here we can string string name and in that case service i can call dot get okay say by name and i can pass that name here no so here we have done and here let me return and we need to annotate with this one at the red gate mapping with some url what is the url we can take can say csch okay so we have done now let's check what else we have missed i think it's pretty much clear controller is there and an enable caching is there pretty clear and in service i have given that where is that service package here we go service package i have given that system.out.println that is also fine pretty good so now let's run that application and i'll quickly test in postman it says jdk based implementation application is starting okay good so application is running on 8080p let me move to postman okay so here let me take localhost some eight zero eight zero and here what is that one uh okay say what is that name let me go to controller and here we need to search ct no so here city and it's a get so we can pass parameters parameters we can pass so here high-tech city first will pass okay name and high tech city so first time it will hit 2 first time it will hit that service means it will go to this method and it will return second time onwards it will not invoke this method right so it will not invoke this method so first time we'll get this system.out.println get zip code method called but second time onwards will not get so let's check tech city high tech city five triple zero eight one i am getting that object so it's you can see get zip code method called now second time i am hitting again if you see again i am meeting but that time you can see it is no it's not calling that method right it's not invoking that method let me call it again just see it's not invoking this method so this system.out.println is not coming means it's not invoking that method and it's returning from that cassette right now let's see what is i mean this is that name of that case you know so what is there inside that case a we can see with that name c-s-e-s-e and we can pass that case a name so with that case a name what are the elements are there we can check if you see let's click it so you can see here native case a so high tech city is that key and value is one object you can see city name zip code high text it is that key so whatever that parameter will pass that parameter directly it will act as a key now let's again let me search with another name or another name sr nagar so name sr nagaram passing so here also sr nagar we have not called i mean sr number is not there in that case you know so first time it will go to this method to return that object and second time onwards it will not go it will not invoke that method from that case directly it will come so let me hit sr knockers we are getting that object if you see here we are getting that object let's see it's executed right get zip code method executed let me clear that console and let's call it again okay before calling that one let me check in that case say that object is there or not with sn occur cac and what is this case a name city zip code cassie right so let me pass and we can you can see with city name sr nagar one entry will be there you can see here srnagar with that key and value as that object as another and zip code right so we are getting it perfectly so next time onwards it will not go to it will not invoke that method if you see here again i am invoking just see it's not calling so the cassie is working perfectly right so now let's see another what are the other annotations present in that spring boot right so one more annotation is there at the red cassette put and at the right case epic so let's discuss that at that case ksa put fast so suppose you are inserting on record to one db right you are inserting a record to the db but you want whatever that result you are getting from the db you need to put in into that case a so that time you can use at the red casa port so let me return city put or add add city so here what i am doing i am taking this and where is that one here i am taking one city and city and i am putting this city into that map dot port here what will that get string dot string dot get city name and what will be that value that entire city object right and let's return this city so casey put what exactly it will do suppose you are in suppose you have another method which will insert some caseable data to database right so that time what instead of how we'll load that case right so if you want to that time itself that same object it should store in that case a then you can put at the red gas simple not guessable cache put so what exactly case a put will do whatever the data it will return it will store into that case a whatever the data will return from that method it will store into that case a so let me keep here value value is you should give that same case case a name right whatever that case say in which case you want to store that same name you you can give here so here is the trick here now so earlier we do have city name as a single parameter right but now we have a city as an object so that time how we can do we can refer like city dot city name you you are referring that why it is crying okay key will that key right so key here you are referring city object dot city name so city name here here you are referring right because we are storing cases as a city name as a key no so we are doing like this so this is casey put one more let me try one one more annotation so already cases are there in that case right but we want sometimes we want to repress that case delete that cassie entirely right suppose sometimes we want some press or some dv update happened some new values inserted into the database right so that time those old cases we are not required right so that time how you will delete that case a right so remove that case a entirely from that case a case a memory right so again firstly we will hit the database or api it will get those results and it will store the database right so how will delete those cases right so we can provide an api and we can implement that that one so suppose or you can do a scheduler if you want each day suppose maybe a bi-weekly or weekly or each day you want to delete that casey right so that time you can implement either scheduler that in that way also it's possible or you can expose an end point if you hit that end point then delete that cassette right so that is very flexible it's better we can provide an uh api so that whenever you want we can delete that ksa right this is a very important while testing also in test environment we want to replace that case right so that time how will do it's better we can expose an api if you hit that api then it will replace that case a or delete that case entirely right so let me do that one public world refresh essay remove cassie remove cassay here we not required anything and we can it's not void let me give a string okay refresh successfully so at the red evict and here you can give that name so what can say you want to remove and one more here we can pass remove all attribute or what is that and what is that attribute remove all entries or something all entries equal to true so remove all entries right and here let me return some dummy return removed guess removed successfully okay here we go so if you call this method then automatically it will remove that entire cassette right here all entries equal to true no and this is the name whatever case a name you want to remove that one pretty good now let's accommodate the same thing in this controller public string remove can say and let's annotate with at the red gate get mapping and cse ch cassie it's better we can put a delete mapping no because we are removing uh casey the led delete mapping and here we can call that cassay dot remove cassie and let me return this one okay here you go anything missing no everything is good so let's run this one red let's rerun this one and we will see right so two things we will see one is kasi put for kc put already have written one right oh for kc put also i need to write on uh end point so public city and here we can take a city city this one i want to put so at the red request body where is that one here we go and here let's return casey service dot what is not an ad city and here let me pass this city here we go and and let's annotate this one at the rate of post mapping and some same thing city no ctrl alt l everything is good so so now we'll test these two things right this post mapping and this delete mapping we need to test right if you are adding something if you are adding something into the database then that time whatever the result will return it will store into that case a right this one and this one will see so let's run this application meanwhile i will open that postman and here let's keep this one and let me take one more tab here let's type post and here we'll do that city and in body we can pass this uh some some more attribute some more object we can pass so it's a raw type it's a json where is that json here we'll go and instead of sr nagar let's put some city what is that we can take what city we can take miyamou is there in our system jd happy happy spit spelling wrong i don't know okay so let's take this uh suppose three nine so let's get create a create with this one and we'll hit enter it's a post mapping code we get a success response 200 no so we have added this one to map so if you see here we have added this into map right so now we can see the same key name with happy spate that entries would be there in that case a as the case a map case a put will work in this way right whatever the result will get that result will store into that casey right so now let's check in that case say what is present so let me take one more tab to check in that case right i have already implemented that one csesc case a name this one so let's hit enter you can see now in that case a one entry is there with happy speed and that object will be stored in that case now let's see no other objects are here no other entries are here now let's hit enter sr nagar sr nagar 308 right if you see earlier only single entry was there with this this is the key this is the value now that sr nugget will also will be there you can see that ksa is present right hope you got these two annotations now let's see we want to remove that cassette right so how will do let me take one more tab that one that is delete mapping is delete mapping this one and here what is that endpoint address it's a case only okay c-s-e-c so if you delete entire thing it that casey will be empty so let's hit enter cassay removed successfully now if you hit that case a name right get that casey things it will return empty just see native can say empty so it's plus the entire thing it's deleted everything right whatever those entries are there it's got deleted entire thing right so hope you got a pretty clear about that uh spring boot cassette so what are those annotations are there if you see here those uh casa port casa epic and casable these three annotations i have explained clearly and hope you got this one right so all other implementation implementation also same other implementation like guava casey or it's the esk everything is like same way same annotation same only just we need to change configurations little bit here and there we need to change that configurations that's all right so hope you got a clear picture so if this video is helpful to you people please do like share and comment and if you have any questions please do in do a comment in the comment section definitely definitely i will help you out to resolve if any issues you have right so please do considering subscribe my channel also thank you for watching
Info
Channel: Java Shastra
Views: 10,767
Rating: undefined out of 5
Keywords: cache, Spring boot cache, cache with spring boot, spring cache, spring boot cache example, caching in spring boot, cache spring boot, @Cacheable, @Cacheput, @CacheEvict .
Id: C9guc_x-QIw
Channel Id: undefined
Length: 50min 33sec (3033 seconds)
Published: Sun Nov 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.