Memcached FTW

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    7 Favorites

    Memcached FTW - Presentation Transcript

    1. 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/
    2. 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
    3. 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, ...
    4. 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
    5. 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
    6. 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)
    7. What to cache? • SQL resultsets • Whole pages • XSLT Transformation results • Generated HTML fragments • Images (thumbnails) • Anything ‘expensive’
    8. PHP and memcache • memcached daemon • PHP memcache PECL extension (not installed by default) • procedural and OO interfaces available
    9. running the daemon • memcached -d -m 1024 -p 11211
    10. 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] );
    11. Memcache singleton <?php require_once(\"constants.php\"); 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; } } ?>
    12. 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(\"TEH DATABASE HAS FAIL. It cried: \" . $e->getMessage()); } }
    13. STOP!!! demo time
    14. Resources • http://www.danga.com/memcached/ • http://www.php.net/memcache • http://www.i-marco.nl/weblog/

    + TheMarcoTheMarco, 2 years ago

    custom

    3798 views, 7 favs, 7 embeds more stats

    An introduction to dramatically improving your webs more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3798
      • 3013 on SlideShare
      • 785 from embeds
    • Comments 0
    • Favorites 7
    • Downloads 85
    Most viewed embeds
    • 769 views on http://www.i-marco.nl
    • 5 views on http://barcampbrighton2.backnetwork.com
    • 5 views on http://onthetech.com
    • 2 views on http://static.slideshare.net
    • 2 views on http://usefulgarbage.tumblr.com

    more

    All embeds
    • 769 views on http://www.i-marco.nl
    • 5 views on http://barcampbrighton2.backnetwork.com
    • 5 views on http://onthetech.com
    • 2 views on http://static.slideshare.net
    • 2 views on http://usefulgarbage.tumblr.com
    • 1 views on http://webtwee.net
    • 1 views on http://feeds.feedburner.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories