SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
2.
Agenda<br />Overview<br />Memcache is not<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.
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 license”.<br />Uses RAM for storage<br /> Acts as a dictionary of stored data with key /value pairs <br />
4.
Memcache is not..<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.
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.
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.
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 />
13.
Common Use cases - 1<br />Simple query result caching<br />$key = ”some:unique:key”;<br />$value = $memcache‐>get($key);<br />if ($value) { <br /> return $value; <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.
Common Use cases - 2<br />Caching network/webservice calls<br />$key = ”some:unique:key";<br />$value = $memcache->get($key);<br />if ($value) {<br /> return $value; <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 />
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.
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 />