This last approach, also called lazy loading, is about filling the cache on-demand. In this case, data access looks as follows:
- A call to the cache is made to check whether the value is already there. If so, just return it.
- Reach the main data store or service that provides the value.
- Store the value in the cache and return it to the user.
This type of caching is often done using Memcached or Redis. It can be really fast and efficient – the cache only contains data that was requested.
However, if data that is not in the cache is often requested, the preceding three calls can increase the latency noticeably. To mitigate this for cache restarts, the cache can be primed (initialized) with selected data from the persistent store.
The items in the cache can also become stale, so it's best to set a time-to-live for each entry. If the data is to be updated, it can happen in a write-through manner by removing the record from the cache and updating it in the database...