Implementation

We can summarize the steps needed when implementing the Cache-Aside pattern, involving a database and a cache, as follows:

  • Case 1: When we want to fetch a data item: return the item from cache if found in it. If not found in cache, read the data from the database. Put the read item in the cache and return it.
  • Case 2: When we want to update a data item: write the item in the database and remove the corresponding entry from the cache.

Let's try a simple implementation with a database of quotes from which the user can ask to retrieve some quotes via an application. Our focus here will be implementing the Case 1 part.

Here are our choices for the additional software dependencies we need to install on the machine for this implementation: ...

Get Mastering Python Design Patterns - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.