To ensure that we keep in mind the pattern we target when we put caching in place, let's use a simple example taken from our quote manager application. Our goal will be to make our find by symbol endpoint go faster. The current logic looks like this pseudo code snippet:
Quote quote = database.find(symbol);
if (quote == null) {
throw NotFoundException();
}
return convertToJson(quote);
We only have two operations in this code snippet (find it in the database and convert the database model into a JSON model). Wonder what you're caching: the database lookup result, the JSON conversion, or both?
We will come back to this part later, but to keep it simple, here, we will just cache the database lookup. Therefore, our new pseudo code can look like the following:
Quote quote = cache.get(symbol);
if (quote == null) {
quote = database.find(symbol);
if (quote...