Caching Data For Performance
-
Upload
dave-ross -
Category
Technology
-
view
6.350 -
download
2
description
Transcript of Caching Data For Performance
![Page 1: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/1.jpg)
Caching Data For Performance
Dave RossWest Suburban Chicago PHP Meetup
February 7, 2008
![Page 2: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/2.jpg)
Caching
It's faster to get informationfrom somewhere close...
![Page 3: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/3.jpg)
Caching
Than somewherefar away.
![Page 4: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/4.jpg)
It's all about scalability.
(seriously)
![Page 5: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/5.jpg)
Where?
Generally speaking...
Local Database faster than Shared DatabaseLocal Disk faster than Database
RAM faster than Disk
![Page 6: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/6.jpg)
What?
Anything you don't want tofetch or compute
every time your code runs.
Anything that isn't going to changevery often.
![Page 7: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/7.jpg)
Everybody's Doing It!
WP-Cache (Wordpress)
Drupal has it built-in
memcached (LiveJournal, Slashdot, WikiPedia)
![Page 8: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/8.jpg)
Even PHP Itself Is Getting Cached
PHP opcode caches compile your scripts and run these pre-parsed versions instead.
Zend Optimizer APC (Alternative PHP Cache) Xcache eAccelerator ionCube
![Page 9: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/9.jpg)
A Few Things I Cached Recently
Access Control List
Page metadata
Entire CMS pages
![Page 10: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/10.jpg)
Caching On Disk
Make a big array of the data you need.
// At the start of your script$array = unserialize(file_get_contents($fileName));
// If the data isn't already there, put it thereif(array_key_exists('key', $array)){ $value = $array['key'];}else{ // Retrieve or compute $value $value = “Hello, World!”; $array['key'] = $value;}
// At the end of your scriptfile_put_contents($fileName, serialize($array));
![Page 11: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/11.jpg)
Of Course, the OS Caches Files
Modern operating systems use all your free RAM as a disk cache.
If you read the same file over & over, it's probably being read out of RAM the second time on.
So, it's usually ok to read a small file over & over.
![Page 12: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/12.jpg)
memcached
“memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web
applications by alleviating database load.”
![Page 13: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/13.jpg)
memcached
(It's a big array that doesn't go away when your PHP program ends.)
![Page 14: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/14.jpg)
memcached
<?php
$memcache = new Memcache;$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;$tmp_object->str_attr = 'test';$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>
![Page 15: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/15.jpg)
Caveats
Be careful caching anything where security matters.
Don't cache credit card info (PCI compliance)
Keep cache up-to-date (when data changes)
![Page 16: Caching Data For Performance](https://reader033.fdocuments.us/reader033/viewer/2022061217/54b527d14a7959cf308b4652/html5/thumbnails/16.jpg)
For More Information
Caching In General http://en.wikipedia.org/wiki/Cache
PHP Accelerators/Opcode Caches http://en.wikipedia.org/wiki/PHP_accelerator http://www.zend.com/en/products/guard/optimizer
memcached http://www.danga.com/memcached/