Caching Basics in PHP Presented at The beauty of Web phpXperts seminar 2010
May 10, 2015
Caching Basics in PHP
Presented at
The beauty of WebphpXperts seminar 2010
What is Caching?
“A cache is a temporary storage area where frequently accessed data can be stored for rapid access.”
Why do we need it?
● To reduce the number or retrieval queries made to a database
● To reduce the number of requests made to external services
● To reduce the time spent computing data● To reduce filesystem access
A basic example
● A Post
● 23 comments
● Sidebar Contents
● Navigations
● Categories
● Much more...
We have to load -
1. Check if already cached if(the resource is cached) {
Just display it.}
// TOP of your script$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
// Serve from the cache if existif (file_exists($cachefile)) { include($cachefile); exit;}
2. Generate content and cacheelse{
Get the resource, Display it and Cache.}
ob_start(); // start the output buffer
// – Your normal PHP script and HTML content here –
// BOTTOM of your script
// save the contents of output buffer to the file$fp = fopen($cachefile, 'w');fwrite($fp, ob_get_contents()); fclose($fp);
ob_end_flush(); // Send the output to the browser
3. Expire the cache
● Event/Trigger
● Setting a time
if(new comment || post edited) {Expire cache for this post
}
$cachetime = 120 * 60; // 2 hours
// Serve from the cache if it is younger than $cachetimeif (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { include($cachefile); Exit; }
So, It was simple. right?
Request
Output
CacheCompute
Miss
Hit
Store
1
2
3
4
Request
Your question is ...
My pages always have an ever changing content and cannot be cached in their entirety
Different Caching Approaches
● Full page caching● Granular page caching● Content pregeneration● Opcode caching● SQL query caching● Browser caching● Object/Variable caching
Everybody is doing it!
● Zend Framework● CakePHP● Kohana● Symfony● ...
● Drupal (modules)
● Wordpress (wpcache)
● Joomla (modules)
● PhpBB● Magento● ...
Frameworks CMSs
What to cache?
Anything that...● you don't want to fetch or compute every time your code runs.● isn't going to change very often
To find them, monitor ● Queries● Page loads● Web Analytics
Where to Cache?
● Local Database faster than Remote Database *● Local Disk faster than Database*● RAM faster than Disk*
* Some conditions apply
APC
● APC: Alternative PHP Cache● Free, Open Source Opcode Cache● Content / Object caching● Simple installation● Stores to local, shared memory● Going to be released as part of PHP6
Installation
Basic PECL (http://pecl.php.net)
pecl install apc
Ubuntu/LinuxMintaptget install php5apc
Windows(http://pecl4win.php.net )
extension=php_apc.dll
A Quick BenchmarkPHP Native PHP w/APC
Concurrency Level 10
Time taken for tests 60 seconds
Complete requests 298 914
Total transferred 643,149 bytes 1,962,675 bytes
HTML transferred 516,971 bytes 1,582,035 bytes
Requests per second 4.91 [#/sec] 15.21 [#/sec]
Time per request 2035.405 [ms] 657.623 [ms]
Time per request(mean, across all concurrent requests)
203.541 [ms] 65.762 [ms]
Transfer rate 10.35 [KB/s] received 31.88 [KB/s] received
How? Opcode caching
Scanning Lexing Parsing Compilation
Object caching
● apc_add() / apc_store() Cache a variable● apc_fetch() Fetch a stored variable from the cache ● apc_delete() Removes a stored variable from the cache
Monitor apc.php
I am...http://ajaxray.com | http://www.facebook.com/ajaxray | http://twitter.com/ajaxray
Anis uddin AhmadCoFounder, WNeeds
http://ajaxray.com | [email protected]
Thank you!
?APC Developers
● George Schlossnagle
● Daniel Cowgill
● Rasmus Lerdorf
● Gopal Vijayaraghavan
● Edin Kadribasic
● Ilia Alshanetsky
● Marcus Börger
● Sara Golemon