SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
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/

Contenu connexe

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Dernier (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

En vedette

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 

En vedette (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Memcached FTW

  • 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(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; } } ?>
  • 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(quot;TEH DATABASE HAS FAIL. It cried: quot; . $e->getMessage()); } }