Instructor: We've got another problem here, if I click on Pikachu, it's going to go and load all Pikachu's data. Then I click on Charizard and that will load all Charizard's data. If I go back to Pikachu, it actually loads all Pikachu's data again, but none of that data changed. It would be nice if I could just cache that on the client.
Cache invalidation is a really difficult problem, but I wanted to show you a quick and simple example of how you might start out caching resources. What we're going to do here first is I'm going to take this function call here and I'm going to cut it out.
I'm going to create a new function called get Pokémon resource with a new Pokémon name. Right up here, I'm going to make that get Pokémon resource. It's going to accept a name and then we're going to return the create Pokémon resource with that name.
So far, we haven't made any changes to our behavior. I'm going to go ahead and get our resource here and we'll return the resource. I'm going to restore that resource somewhere so I can retrieve it later. Where I'm going to store it is just in a variable right here, Pokémon resource cache.
With my Pokémon resource cache, I'm going to take the name of the Pokémon and assign that to the resource. Then, when we call get Pokémon resource, the first thing I'm going to do is we'll say if that resource already exist, so I'll grab this, if that already exist in here, then we'll just simple return that.
That is actually enough to get us going. If I click on Pikachu and then Charizard and then Pikachu again and then Charizard and back and forth. I'm going to insert a really serious warning right here, cache invalidation is not something to be trifled with.
Maybe you're OK with users leaving their browsers open on your application for weeks on end. Keeping that cached version of all the data around for that period of time, but if you're not OK with that, then you should probably implement some sort of cache invalidation strategy.
I wanted to show you how you could get this caching working for the simple case, so that you could extrapolate from this and use a cache invalidation strategy or even NPM module that works well for your particular use case.
I'm going to refactor just a few things here because if I were to type in Pikachu with a capital P and submit that, then we're actually going to get a loading state here. What I'm going to do is we're going to get a lower name, name.toLowerCase. We're going to use the lower name instead for all of this because our API is case insensitive.
Now if I go Pikachu, and then Charizard, and then Pikachu with a capital P, that loads instantly. I'm also going to refactor this a little bit so I don't have so much repetition. I'm going to get my resource from the cache.
Then I'll say if there's not a resource, then I'll go ahead and do all of these stuff, which is reassign resource here. Then in any case, we're going to return that resource. Our caching strategy seems to be working for the simple case.
In review, all that we did here was we took the call here where we were creating a new resource, and we put it into a getPokémonResource function. Then we took that name and look that name up in our cache, which is just an object in our module. If that resource does not exist, then we'll create a new one, put that resource into our cache. Whether it exists or not, we're going to return the resource.
The reason all of this is working is because the resource maintains the state of the data that was returned. If we go to this createResource function, that state lives right here in this result. When we rerender with the resource that is already in the cache, the status of that resource is success and it can return the result immediately.
That's why we can switch between these two so quickly, because the status of that resource doesn't change because we're not creating new resources. We're using cached resources.
I'll just echo that warning one more time. Cache invalidation is a difficult problem, and you should definitely consider adding cache invalidation to any caching strategy that you implement.