• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Memcached FTW
 

Memcached FTW

on

  • 8,137 views

An introduction to dramatically improving your website's performance by using memcached.

An introduction to dramatically improving your website's performance by using memcached.

Statistics

Views

Total Views
8,137
Views on SlideShare
6,813
Embed Views
1,324

Actions

Likes
10
Downloads
118
Comments
0

10 Embeds 1,324

http://www.i-marco.nl 1218
http://learnwithachila.blogspot.com 75
http://www.slideshare.net 13
http://barcampbrighton2.backnetwork.com 5
http://onthetech.com 5
http://usefulgarbage.tumblr.com 2
http://blog.bbnnt.com 2
http://static.slideshare.net 2
http://webtwee.net 1
http://feeds.feedburner.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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…
Post Comment
Edit your comment

    Memcached FTW Memcached FTW Presentation Transcript

    • memcached FTW! Because performance does matter BarCamp Brighton 2 - March 16 2008 Marco van Hylckama Vlieg marco@i-marco.nl http://www.i-marco.nl/weblog/
    • memcached • memcached is used by very large, high profile websites such as Livejournal, Wikipedia and Facebook • General purpose, scalable caching solution • Extremely fast • Extremely easy to use
    • Universal • memcached can be used with just about any programming language • Runs on almost any box • Multiple boxes can be deployed • Can be used with PHP, Python, Perl, Java, C#, Ruby, ...
    • How does it work? • Store anything that can be serialised • Store with unique key • Fetch back with this unique key • Expire time can be set
    • Usage pattern • Store items with key and expire time • Request item : • found? return it! • not found? produce item and store in cache • If needed, create own expiration mechanism
    • Limitations • Can’t dump contents of the cache • Cache is not persistent • Not redundant • No security • Max 1MB - 42 bytes cached object size (can be increased at compile time)
    • What to cache? • SQL resultsets • Whole pages • XSLT Transformation results • Generated HTML fragments • Images (thumbnails) • Anything ‘expensive’
    • PHP and memcache • memcached daemon • PHP memcache PECL extension (not installed by default) • procedural and OO interfaces available
    • running the daemon • memcached -d -m 1024 -p 11211
    • Basic usage // create Memcache object $objMem = new Memcache; // Store something $objMem->set( $key, $value, $compress, $expire ); // Retrieve something $retrievedItem = $objMem->get( $key ); // Manual expire / delete $objMem->delete( $key, [$timeout] );
    • Memcache singleton <?php require_once(quot;constants.phpquot;); class memSingleton extends Memcache { private function __construct() {} private function __clone() {} static private $objMem = NULL; public static function getMem() { if (self::$objMem == NULL) { self::$objMem = new Memcache; if(!@self::$objMem->connect(__MEMHOST, __MEMPORT)) { throw new Exception('FAIL! ZOMG! Memcache server not available.'); } } return self::$objMem; } } ?>
    • SQL Caching function function cachedSQL($sSQL, $nExpire) { try { if($aResultSet = memSingleton::getMem()->get(MD5($sSQL))) { echo '<p>CACHE HIT!</p>'; return $aResultSet; } } catch(Exception $e) {} // manually retrieving uncached item try { $objResultSet = PDOSingleton::getInstance()->query($sSQL); // Store the resultset in the cache $aResultSet = Array(); foreach($objResultSet as $row) { $aResultSet[] = $row; } if(!@memSingleton::getMem()->set(MD5($sSQL), $aResultSet, true, $nExpire)) { echo '<p><strong>CAUTION</strong>: CACHE IS DEAD! Serving uncached data.</p>'; return $aResultSet; } echo '<p>CACHE MISS!</p>'; return $aResultSet; } catch(PDOException $e) { die(quot;TEH DATABASE HAS FAIL. It cried: quot; . $e->getMessage()); } }
    • STOP!!! demo time
    • Resources • http://www.danga.com/memcached/ • http://www.php.net/memcache • http://www.i-marco.nl/weblog/