Caching Data For Performance
Upcoming SlideShare
Loading in...5
×
 

Caching Data For Performance

on

  • 8,977 views

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.

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.

Statistics

Views

Total Views
8,977
Views on SlideShare
8,933
Embed Views
44

Actions

Likes
8
Downloads
246
Comments
2

4 Embeds 44

http://www.slideshare.net 29
http://suburbanchicagophp.org 12
https://twimg0-a.akamaihd.net 2
http://www.gestiopolis.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Great material.. thanks for sharing!
    Are you sure you want to
    Your message goes here
    Processing…
  • Thanks. Very informative.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Caching Data For Performance Caching Data For Performance Presentation Transcript

  • 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!
    • WP-Cache (Wordpress)
    • Drupal has it built-in
    • memcached (LiveJournal, Slashdot, WikiPedia)
  • 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
  • A Few Things I Cached Recently
    • Access Control List
    • Page metadata
    • Entire CMS pages
  • 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));
  • 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.
  • 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.”
  • 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;); $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); ?>
  • Caveats Be careful caching anything where security matters. Don't cache credit card info (PCI compliance) Keep cache up-to-date (when data changes)
  • 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/