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

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sectoritnewsafrica
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 

Dernier (20)

QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
4. Cobus Valentine- Cybersecurity Threats and Solutions for the Public Sector
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 

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()); } }