Back to results for “Caching”
PHP › Caching Strategies in PHP

What is the difference between page caching, query caching, and object caching, and when should each be used?

Intermediate

Answer

Page caching stores an entire rendered HTML page and serves it directly for future identical requests, avoiding any PHP execution at all, query caching stores the results of specific expensive database queries, and object caching stores the results of computationally expensive operations or serialized objects, with page caching offering the biggest performance boost for content that rarely changes, while query and object caching are more appropriate when specific parts of a page vary per user but other parts remain expensive to compute repeatedly.
// Page caching example concept
// Cache the entire rendered homepage HTML for 10 minutes

// Query caching example
$cacheKey = 'user_orders_' . $userId;
$orders = $cache->remember($cacheKey, 600, fn() => $database->getUserOrders($userId));
Real-world example A news website uses full page caching for its rarely changing homepage, while using query caching specifically for each logged in user's personalized recommendation list, which changes more frequently and depends on the specific visitor.

Common follow-ups: How do you handle cache invalidation when the underlying content actually changes?;What is the risk of caching a page that contains user specific or sensitive information?