// Connect to Memcached server $memcache = new Memcache; $memcache->connect('localhost', 11211); // Check if data is present in cache $result = $memcache->get('query_results'); if (!isset($result)) { // Data is not present in cache, retrieve from database $query = "SELECT * FROM users"; $result = fetch_query_results($query); // Store query results in cache for future use $memcache->set('query_results', $result, 0, 60); // 60 second expiration } // Use cached data for page rendering render_page($result);
// Check if data is present in APCu cache if (apcu_exists('function_output')) { // Use cached data for page rendering render_page(apcu_fetch('function_output')); } else { // Data is not present in cache, execute function $output = expensive_function(); // Store function output in cache for future use apcu_store('function_output', $output, 60); // 60 second expiration // Use function output for page rendering render_page($output); }