Caching Data For Performance Dave Ross West Suburban Chicago PHP Meetup February 7, 2008
Caching It's faster to get information from somewhere close...
Caching Than somewhere far away.
It's all about scalability. (seriously)
Where? Generally speaking... Local Database faster than Shared Database Local Disk faster than Database RAM faster than Disk
What? Anything you don't want to fetch or compute every time your code runs. Anything that isn't going to change very often.
Everybody's Doing It! <ul><li>WP-Cache (Wordpress) </li></ul><ul><li>Drupal has it built-in </li></ul><ul><li>memcached (L...
Even PHP Itself Is Getting Cached <ul><li>PHP opcode caches compile your scripts and run these pre-parsed versions instead...
A Few Things I Cached Recently <ul><li>Access Control List </li></ul><ul><li>Page metadata </li></ul><ul><li>Entire CMS pa...
Caching On Disk Make a big array of the data you need. // At the start of your script $array = unserialize(file_get_conten...
Of Course, the OS Caches Files Modern operating systems use all your free RAM as a disk cache. If you read the same file o...
memcached “ memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for...
memcached (It's a big array that doesn't go away when your PHP program ends.)
memcached <?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die (&quot;Could not connect&quot;); $...
Caveats Be careful caching anything where security matters. Don't cache credit card info (PCI compliance) Keep cache up-to...
For More Information <ul><li>Caching In General </li></ul><ul><li>http://en.wikipedia.org/wiki/Cache </li></ul><ul><li>PHP...
Upcoming SlideShare
Loading in...5
×

Caching Data For Performance

6,481

Published on

Information on how PHP developers can implement data caching to improve performance and scalability. Presented at the West Suburban Chicago PHP Meetup on February 7, 2008.

Published in: Technology
2 Comments
8 Likes
Statistics
Notes
  • Great material.. thanks for sharing!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thanks. Very informative.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
6,481
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
251
Comments
2
Likes
8
Embeds 0
No embeds

No notes for slide

Caching Data For Performance

  1. 1. Caching Data For Performance Dave Ross West Suburban Chicago PHP Meetup February 7, 2008
  2. 2. Caching It's faster to get information from somewhere close...
  3. 3. Caching Than somewhere far away.
  4. 4. It's all about scalability. (seriously)
  5. 5. Where? Generally speaking... Local Database faster than Shared Database Local Disk faster than Database RAM faster than Disk
  6. 6. What? Anything you don't want to fetch or compute every time your code runs. Anything that isn't going to change very often.
  7. 7. Everybody's Doing It! <ul><li>WP-Cache (Wordpress) </li></ul><ul><li>Drupal has it built-in </li></ul><ul><li>memcached (LiveJournal, Slashdot, WikiPedia) </li></ul>
  8. 8. Even PHP Itself Is Getting Cached <ul><li>PHP opcode caches compile your scripts and run these pre-parsed versions instead. </li></ul><ul><li>Zend Optimizer </li></ul><ul><li>APC (Alternative PHP Cache) </li></ul><ul><li>Xcache </li></ul><ul><li>eAccelerator </li></ul><ul><li>ionCube </li></ul>
  9. 9. A Few Things I Cached Recently <ul><li>Access Control List </li></ul><ul><li>Page metadata </li></ul><ul><li>Entire CMS pages </li></ul>
  10. 10. 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 there if(array_key_exists('key', $array)) { $value = $array['key']; } else { // Retrieve or compute $value $value = “Hello, World!”; $array['key'] = $value; } // At the end of your script file_put_contents($fileName, serialize($array));
  11. 11. 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.
  12. 12. 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.”
  13. 13. memcached (It's a big array that doesn't go away when your PHP program ends.)
  14. 14. memcached <?php $memcache = new Memcache; $memcache->connect('localhost', 11211) or die (&quot;Could not connect&quot;); $version = $memcache->getVersion(); echo &quot;Server's version: &quot;.$version.&quot;<br/> &quot;; $tmp_object = new stdClass; $tmp_object->str_attr = 'test'; $tmp_object->int_attr = 123; $memcache->set('key', $tmp_object, false, 10) or die (&quot;Failed to save data at the server&quot;); echo &quot;Store data in the cache (data will expire in 10 seconds)<br/> &quot;; $get_result = $memcache->get('key'); echo &quot;Data from the cache:<br/> &quot;; var_dump($get_result); ?>
  15. 15. Caveats Be careful caching anything where security matters. Don't cache credit card info (PCI compliance) Keep cache up-to-date (when data changes)
  16. 16. For More Information <ul><li>Caching In General </li></ul><ul><li>http://en.wikipedia.org/wiki/Cache </li></ul><ul><li>PHP Accelerators/Opcode Caches </li></ul><ul><li>http://en.wikipedia.org/wiki/PHP_accelerator </li></ul><ul><li>http://www.zend.com/en/products/guard/optimizer </li></ul><ul><li>memcached </li></ul><ul><li>http://www.danga.com/memcached/ </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×