Incorporating a Lazy Loading Design Pattern with cp.cache

This tutorial shows you how to us cp.cache to implement a Lazy Loading design pattern. (wiki Lazy Loading)

What is Lazy Loading

Lazy loading is a programming technique where you deffer data loading until it is needed. When needed, it is loading into cache for use in the future. This tutorial also covers suggested ways to flush the cache.

Caching the Composed Layout

The simplest example is to cache the final results created from a database read. For example if you read all the people records in the database whose name include 'dw'. You may want to present these records as a list of names.

Read the value from cache. If the cache is not empty, exit and use the results

If the cache is empty, read the data from the database and construct your output. When done, save the result to the cache, exit and use the results.

When you save the result to cache, you may want to include a flush date/time and/or a list of content tables to flush the cache. For instance, in this example if you include "people", if anyone edits a record in people, the cache will clear.

Caching the Intermediate Data

Your results may include just data and not an html layout. This pattern works just as well caching the serialization of an ajax call.