So when trying to solve a problem its important to apply scope orrectly. Don’t try to solve everything. Just solve the problem at hand and maybe follow the motto: Fail fast and break things. (maybe not breaking too many things!)
Here is the problem:
You have to make http calls from an Ionic app and the data received doesn’t change that often, so this is a case that is a good candidate for caching.
So here are some questions we should be asking:
- Does the cache expire? If so when.
- Can you force the cache to expire?
- How is the cache implemented? memory, browser storage, db?
- What are the cache keys?
- Since http calls are async, the cache interface needs to be async?
Maybe thats a good start?
Lets discuss some of these questions in detail:
- Cache expiration. This is important. This has to configurable. The cache class should read this from a config or the time can be passed into the constructor. We’ll do both.
- Force cache expiration. This is important too. Maybe we need certain keys to expire or maybe the whole cache. We’ll implement methods for both.
- Cache backing. This can be important, but shouldn’t affect everyday usage of the cache. Memory would be fastest. We’ll keep it simple and we’ll use memory.
- Cache keys. Since these are
httpmethods, can we use a combination of thehttpverb (GET, POST, etc) and the address? Lets go with this and see if any problems crop up. - Async calls. Now this we have to support. We don’t want the clients of the class to change the behavior.
With these in mind, the next article will describe an implementation.