Cache

Learn what cache is and how to use it in PHP.

Although PHP unsets all the variables after processing the request, we often need to persist the data to use it in the future. For example, an e-commerce site must save the data about products it sells, the cart states, and orders users make.

This chapter covers data storage mechanisms that don’t involve database engines. Let’s start with caching. It’s not a long-term persistence mechanism, but it manages to save the data between requests.

What’s caching?

Caching means saving some of our results so we don’t have to recalculate them again. Imagine we have a long-running task—finding prime factors of a big number. If we need to use the results multiple times, we save the calculation result (cache it) to avoid repeating expensive calculations. A task doesn’t have to be computation-intensive to make us cache it. Imagine we’re getting a news feed from an external service that will be displayed on our page. Calls to external services are slow. Also, providers might not ...

Access this course and 1400+ top-rated courses and projects.