Getting Started with Caching in Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome back to the channel my name is alex and in this video we're going to talk about caching in spring boot before we dive in let's quickly talk about what caching is and what it's good for so the idea is usually when you have an expensive operation so calling a slow api invoking expensive database queries or or really just talking to a system that you have to pay for because you're charged on a per api call basis like these kind of things um then it might make sense to to cache a copy of the data somewhere locally where you can access it much much faster and without the the cost associated and while this is nice and sounds straightforward it's not as easy actually because you really have to know the requirements for the underlying data like do you always have to have the the most recent version of the data available that's important and you have to think about when once you have the cache like when do you remove entries from the cache and how can you make sure that you reconcile with the underlying um actual data so spring provides us with a cache abstraction that we can use and there are multiple implementations like we could use reddit we could use ecash or hazelcast but in this video we just stick with the onboard caching system which really is just a concurrent hashmap but it's enough to just see how the cache works and how we can just validate its functionality so with that out of the way let's code so let's quickly take a look at the dependencies as we usually do so i'm using springboot272 and the relevant dependency that we have to use here is the spring bootstrata cache so that's pretty much all there is to it but we have to make sure that we have this here so before we forget about it let's make sure that we enable caching because that's one of the preconditions to make sure that this works so what i'm going to do now is come up with a service that has all the underlying requirements so we have some data that we can add we can remove it and we want to see how the cache actually works so before we turn to the cache let's start with that service and just bidding on one of the previous videos let's call that a newsletter service so newsletter service that's the service stereotype so what we want to do here is we want to support a couple of functions first one is subscribe so we want to make sure that people can subscribe and let's just also create a data class for an actual subscriber so subscriber has an email and has also a name we just keep it simple there's no first and last name it's just the name so we want to make sure that we have a subscriber that can just subscribe and i also want to make sure that there's some unsubscribe function so let's turn to that so there's unsubscribe we pass it the same subscriber and we can unsubscribe and i also want to have a look up functionality which is what we're going to cache and we just call it lookup and what we want to do is we pass in an email address and we want to return a subscriber so these are the three functions that we are going to support in this video so let's quickly add the implementations and then enable caching for them so our data store is also just a concurrent hashmap so well we just map the email address which is the string to the actual subscriber so with the storage in place let's start the implementation of the lookup functionality so what we're going to do here is really just return storage at the email address so that should give us the subscriber and that may be null because we might not have a subscriber with that email address so we just throw an illegal state exception in that case uh subscriber email not subscribed simple as that right so we we try to look it up and if it doesn't exist we just throw the exception and for the fun of it let's also delay that service we just pretend that the lookup functionality takes a lot of time so we're using time unit seconds sleep five so we're sleeping for five seconds just pretending that this takes a lot of time and then we look up the um the subscriber and return it or throw the exception so how do we actually subscribe so for the subscription we just add to the storage subscriber email equals subscriber so that's all right this is the subscription and likewise if we just want to unsubscribe somewhere we just go to storage uh remove subscriber email and that's about it all right since that service is now in place um let's just write a quick test to just make sure that it works as expected so let's go to the test directory and this is our default test and we're just um caching works it doesn't really matter here so we need the service here to test this so i'm using underwired and this has to be laid in it or service that's the newsletter service so and then um let's actually create a subscriber subscriber uh that would be me so this is my email address say hi if you want um that's also me um and we're using assertions um no hang on we first subscribe so we're using the the service and subscribe the subscriber alright so once that's added let's make sure that it's actually subscribed as it equals we expect the subscriber when we just invoke service lookup subscriber email so we're using the newsletter service we look up the email address and we want to just make sure that we find the current subscriber right so this this should already work so let's quickly test that just pulling this up here and as you would expect it takes a lot of time because um the lookup operation is rather expensive right so we we subscribe the user the lookup takes five seconds because we pretended it's super expensive and the test succeeds right and then likewise we can just make sure that we um that we unsubscribe the user unsubscribe subscriber and now we want to make sure that whenever we look up that there is an illegal state exception that is thrown so um that needs to be java whenever we call look up not service look up so look up subscriber email so let's also run this and then we finally get to adding the cache so if i run this right uh it's also going to take a couple of seconds because we have to still do the whole lookup once the user is subscribed but eventually um that succeeds we unsubscribe the user and then we expect the exception to be thrown and that all works as expected right so uh we still have 10 seconds now accumulating because that lookup itself also takes five seconds so with that out of the way let's go back to the newsletter service and actually start caching something so the first thing that we want to change is the lookup functionality so we're adding the cachable annotation and it looks like this and now we can also specify a name for the cache because we might maintain different caches per collection resource whatever we have there to organize the data so i can just pass in a value and call this subscribers because this is this is how we refer to this cache so right now this is already being cached and before we test this um let's also add a few things that we need to take care of because right now the thing is if the if the underlying storage changes this is not reflected here until we tell spring hey there is a there is an update to the cache please put this new element into the cache so we're using cache put here and then we also have to use the same the same cache name so it's subscribers and we have to provide the key because here i'm passing in a subscriber object so we have to tell spring what the key should be so i'm using a spring expression language we can just refer to the to the parameter of the function and use subscriber email so this will make sure that we add something to the cache and likewise there's unsubscribe so what we're going to do here is this is cache eviction so we want to make sure that whenever a user um is re uh removed from the storage it is also at the same time removed from the cache so this is what we're doing here so we again have to pass the name of the cache and again have to declare the key that we're using here so i'm using subscriber email alright so how do we actually test this right now so let's go back to the test and this is this one and now we want to make sure that the cache actually holds the ways that we expect so how do we access the cache so that's pretty straightforward as well so we can just inject private plate in our uh the cache manager and with that we can access our actual cache so let's go there so our actual cache is manager get cache and now we have to provide the name that we've been using so it's subscribers because there may be multiple caches that we that we have in the application right and right now let's make sure that the cache is empty so i'm asserting null that cache cache get um subscriber email so we want to make sure um yeah that may be null but we know that this is not not in that case so i'm just overwriting this so um we want to make sure that the cache is empty initially and then we have the subscribe operation and we want to make sure that now the cache holds one of the values so assert not null um cash get subscriber email so all we've done here is we're making sure the cache is empty then we subscribe a new subscriber we've already tested that the service already has it but we also want to make sure that the cache has a subscriber so and finally we also test the unsubscribe flow so we unsubscribe the user and we're making sure that the cache again is empty so let's briefly run this and see what happens so as before we can see that it's not green so let's let's see what has happened here um so we had expected a subscriber but it's null so let's see where this is um we can just go to the respective test okay so let's see what happens here we had subscribed the subscriber um and now uh it somehow it happened that we get null when we just invoke um the cache and the thing that has happened here is um the cache actually doesn't have the value that we would expect and here's why and this is one of the pitfalls when when pitfalls when using um using caching so let me go there so we can see that here is the subscribe operation and i'm adding a new subscriber to the storage and i'm also using cash put but the thing that i've been missing is that i have to return the actual value otherwise spring will not pick it up and add it to the cache so here i have to return the actual subscriber so just make sure that we now return [Music] um storage let's go there subscriber email and that should be not null so i can just enforce this right so when we have cash put and it's on a function we have to make sure that we return the actual value so it's added to the cash because you first might have to compute the value and we can set it here but we still have to return the value that should be added to the cache otherwise it's not going to happen so let's go back to the test and run this all again so i'm going here run this test and now we still have to wait for the initial five seconds because we cannot remove that um so it takes some time to to run the initial computation but the next look up is much faster right so we can see all the assertions are complete at this point that has all worked uh the cache works as expected and we still have the second lookup at the end of this test but we can see the test time is only five seconds um so that already means that the lookup hasn't incurred any additional cost at this point so this is a quick way of adding a cache and making sure that it works um again there are multiple implementations that we can use and that expose their specifics like expiration times and whatnot but for simple use cases the the onboard cache just just works nicely so we can go with that so that wraps it up for this video let me know if you have any questions in the comments down below please subscribe and i'll see you in the next one [Music] [Applause]
Info
Channel: Alex Gutjahr | Tech Tutorials
Views: 878
Rating: undefined out of 5
Keywords: Spring Boot, Spring Boot 2, Spring Framework, Spring Framework 5, Spring Boot Tutorial, Kotlin, Spring Boot Kotlin, Web Development, Learn to Code, Software Development, Software Engineering, Programming Spring Boot Interview Questions, Learn Spring Boot, How To, Caching, spring boot cache, spring boot cache example, spring cache, caching in spring boot, spring boot cache tutorial
Id: lCOn2ZrDyqs
Channel Id: undefined
Length: 13min 35sec (815 seconds)
Published: Tue Aug 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.