Cache
Explore how caching helps persist data across requests in PHP applications to enhance performance. Understand cache invalidation techniques like TTL, tagging, and explicit invalidation, and learn about common caching tools such as APCu, Memcached, and Redis. Discover best practices and security considerations for implementing caching effectively.
We'll cover the following...
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 ...