Successfully reported this slideshow.

Using memcache to improve php performance

4

Share

Upcoming SlideShare
Memcache
Memcache
Loading in …3
×
1 of 15
1 of 15

Using memcache to improve php performance

4

Share

Download to read offline

Find out how to improve the performance of your PHP applications using memcache. Presented at Bangalore PHP user group meetup on 29-May-2010

Find out how to improve the performance of your PHP applications using memcache. Presented at Bangalore PHP user group meetup on 29-May-2010

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Using memcache to improve php performance

  1. 1. Using Memcache to improve PHP performance<br />Sudar Muthu (@sudarmuthu)<br />http://sudarmuthu.com<br />Research Engineer<br />Yahoo Research<br />Bangalore PHP user group<br />29-May-2010<br />
  2. 2. Agenda<br />Overview<br />Memcache Principles<br />How Memcache works<br />General Usage<br />Memcache Limits<br />Client Libraries<br />Common Use cases<br />Tips for optimization<br />Using Memcache in WordPress<br />
  3. 3. Overview<br />A high-performance Distributed Memory Object Caching System<br />Intended for speeding up dynamic web applications by alleviating database load.<br />Open source & distributed under a “permissive free software licence”.<br />Uses RAM for storage<br /> Acts as a dictionary of stored data with key /value pairs <br />
  4. 4. Memcache Principles<br />It is not a database<br />It does not provide redundancy<br />It doesn't handle failover<br />It does not provide authentication<br />It does not allow locks<br />No direct dump or backup possible<br />
  5. 5. How Memcache works<br />Two stage hash, like a giant hash table looking up key=value pairs<br />Client hashes the key against a list of servers <br />When the server is identified, the client sends its request <br />Server performs a hash key lookup for the actual data <br />
  6. 6. General Usage<br />First look up in memcache before querying the database.<br />If present, return it.<br />Else query the database, store it in memcache and return it.<br />If the data changes, delete it from cache.<br />
  7. 7. Memcache Limits<br />A single value cannot contain more than 1 MB of data.<br />A single key cannot be more than 250 characters in length.<br />Normally a single memcache instance cannot be more than 2 GB in size.<br />
  8. 8. Client Libraries<br />Perl, Python, Ruby, Java, C#, C<br />PostgreSQL (access memcached from procs and triggers)<br />MySQL (adds memcache_engine storage engine)<br />PHP (pecl/memcache)<br />
  9. 9. Client Libraries – PHP API<br />Key Methods<br /><ul><li>boolMemcache::connect ( string $host [, int $port [, int $timeout ]] )
  10. 10. boolMemcache::set( string $key , mixed $var [, int $flag [, int $expire ]] )
  11. 11. string Memcache::get ( string $key [, int &$flags ] )
  12. 12. boolMemcache::delete ( string $key [, int $timeout ] )</li></ul>More: http://www.php.net/memcache<br />
  13. 13. Common Use cases - 1<br />Simple query result caching<br />$key = ”some:unique:key";  <br />if ($memcache‐>get($key)) {    <br /> return $memcache‐>get($key);  <br />}  else {    <br /> // Run the query get the result data $result = $db‐>query(“query”);      $memcache‐>set($key, $result, TRUE, 300); <br /> // Store the result of the query for 5 minutes      return $result;  <br />} <br />
  14. 14. Common Use cases - 2<br />Caching network/webservice calls<br />$key = ”some:unique:key"; <br />if ($memcache->get($key)) {<br /> return $memcache->get($key); <br />} else { <br /> // Download the page <br /> $url = ”Your webserviceurl";<br /> $result = fetch($url); <br /> $memcache->set($key, $result, false, 86400); <br /> // Store the result of the query for a day <br /> return $result; <br />} <br />
  15. 15. Common Use cases - 3<br />Throttle user activity <br />$key = “some:unique:key”; <br />if ($memcache->get($key)) {<br /> //Deny access <br /> return false; <br />} else { <br /> $memcache->set($key, “1”, false, 60); <br /> //Allow access <br /> return true; <br />} <br />
  16. 16. Tips for Optimization<br />Pre warm your cache using scripts <br />Batch your requests using multi-get <br />values= get(Array(“Foo”,”Bar”,”Baz”))<br />Cache things other than SQL data! <br />XML, HTML, Page fragments<br />Replicate data across clusters<br />Do set/delete in multiple clusters  <br />Read from either cluster<br />Run multiple instances on the same server<br />If you know the data, implement a simple encoding scheme<br />
  17. 17. Using Memcache in WordPress<br />Download Memcached Object Cache Plugin from http://wordpress.org/extend/plugins/memcached/<br />Set up memcached servers<br />Install peclmemcacheextention<br />Copy object-cache.php to wp-content directory (not the Pluing’s directory)<br />You are done <br />
  18. 18. Questions?<br />Thank you<br />

×