SlideShare une entreprise Scribd logo
1  sur  18
Télécharger pour lire hors ligne
Intro to Memcached

   By Jason Anderson
What is it?
• Free and open source
• High performance object caching server
  – Objects are stored in-memory
  – Multiple clients can share the same Memcached
    server
• Developed for LiveJournal in 2003
Who uses it?
Installation
Ubuntu/Debian
• apt-get install memcached php5-memcache
Fedora
• yum install memcached php-pecl-memcache
Basic Operations
• $memcache = new Memcache();
  – Creates a new Memcache object


• $memcache->addServer($hostname, $port)
  – $hostname: Hostname or IP of Memcached server
  – $port: Port to connect on (Default: 11211)


• $memcache->set($key, $value, $compress,
  $expire)
  – Each distinct $key maps to a single $value
  – $compress: Boolean defining whether or not to
    compress $value
  – $expire: Number of seconds until the $key, $value
    pair is deleted
Basic Operations (Cont.)
• $memcache->get($key)
 – Returns the stored value
   associated with $key. If no value
   has been assigned, NULL is
   returned.


• $memcache->delete($key)
 – Removes the key/value pair
   associated with $key.
Example
$memcache = new Memcache();

// Connect to a Memcached server
$memcache->addServer("localhost", 11211);

// Time to answer the ultimate question.
// If "ultimateAnswer" isn't in Memcache, fetch from DB
if (!($ultimateAnswer = $memcache->get("ultimateAnswer"))) {
  $ultimateAnswer = (int)UltimateAnswer::dbGet();

  // Add/Update the Memcache entry for 'ultimateAnswer'
  $memcache->set("ultimateAnswer", $ultimateAnswer, false, 60*24);
}
echo $ultimateAnswer;   // 42 of course

// Delete 'ultimateAnswer' from Memcache
$memcache->delete("ultimateAnswer");
$tweet->getText()




$tweet->getPostDate()
class Tweet {
  private $_id;
  private $_postDate;
  private $_text;
  public function __construct($id, $postDate, $text) {
    $this->_id = $id;
    $this->_postDate = $postDate;
    $this->_text = $text;
  }
  public function getId() { return $this->_id; }
  public function getPostDate() { return $this->_postDate; }
  public function getText() { return $this->_text; }
}

$tweet = new Tweet(334089370476544, '2010-11-04 23:50:27', 'The
best thing about telepathy is...I know, right?');
echo serialize($tweet);


//
O:5:"Tweet":3:{s:10:"Tweet_id";i:334089370476544;s:16:"Tweet_post
Date";s:19:"2010-11-04 23:50:27";s:12:"Tweet_text";s:50:"The best
thing about telepathy is...I know, right?";}
// Add Tweet to Memcache
$memcache->set('Tweet:' . $tweet->getId(), serialize($tweet));

// Fetch Tweet from Memcache
$tweet = unserialize($memcache->get('Tweet:334089370476544'));
$tweet->getFavorites()
class Tweet {
...
  private $_favorites;
...
  public function getFavorites() {
    // If the Favorites aren't in Memcache, fetch from DB
    if (!$this->_favorites = unserialize($memcache->get('Favorites:' . $this->_id))){
      $this->_favorites = Favorites::dbGet($this->_id);
      // Add Favorites to Memcache
      $memcache->set('Favorites:' . $this->_id, serialize($this->_favorites), false,
60*24);
    }
    return $this->_favorites;
    }
    // What happens when this Tweet class gets serialized?
}
// $_favorites gets serialized. Not good.

class Tweet {
...
  // Called before the class is serialized.
  // Returns list of class variable names to serialize.
  public function __sleep() {
    return array('_id', '_postDate', '_text');
    // Won't serialize $_favorites
  }
}
$tweet->getUser()
class Tweet {
  ...
  private $_userId;
  private $_user;
  ...
  public function __sleep() {
    return array('_id', '_postDate', '_text', '_userId');
    // Won't serialize $_favorites or $_user
  }
  ...
  public function getUser() {
    // If User isn't in Memcache, fetch from DB
    if (!$this->_user= unserialize($memcache->get('User:' . $this->_userId))) {
           $this->_user = User::dbGet($this->_userId);
           // Add the User to Memcache
           $memcache->set('User:' . $this->_userId, serialize($this->_user), false,
             60*24);
         }
         return $this->_user;
  }
}
$tweet->getText()       $tweet->getUser()




             $tweet->getPostDate()




$tweet->getFavorites()
Cache Stats
      $memcache->getStats();

Memcache Server version:                    1.2.2
Process id of this server process           14038
Number of seconds this server has been
                                            1896810
running
Accumulated user time for this process      102.725383 seconds
Accumulated system time for this process    398.138473 seconds
Total number of items stored by this
                                            881381
server ever since it started
Number of open connections                  11
Total number of connections opened since
                                         1877
the server started running
Number of connection structures
                                            27
allocated by the server
Cumulative number of retrieval requests     13062495
Cumulative number of storage requests       881381
Number of keys that have been requested
                                            12181114 (93.253%)
and found present
Number of items that have been
                                            881381(6.747%)
requested and not found
Total number of bytes read by this server
                                            894.00982284546 Mega Bytes
from network
Total number of bytes sent by this server
                                            5860.2824373245 Mega Bytes
to network
Number of bytes this server is allowed to
                                            64 Mega Bytes
use for storage.
Number of valid items removed from
                                            0
cache to free memory for new items.
Questions?
$name = 'Jason Anderson';
$email = ‘nectro@gmail.com';
$twitter = '@nectro';
$website['Twitter Aggregator'] = 'http://slothnod.com';

Contenu connexe

Tendances

Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!
Michaël Figuière
 
Easy Scaling with Open Source Data Structures, by Talip Ozturk
Easy Scaling with Open Source Data Structures, by Talip OzturkEasy Scaling with Open Source Data Structures, by Talip Ozturk
Easy Scaling with Open Source Data Structures, by Talip Ozturk
ZeroTurnaround
 
Tokyo cassandra conference 2014
Tokyo cassandra conference 2014Tokyo cassandra conference 2014
Tokyo cassandra conference 2014
jbellis
 
APC & Memcache the High Performance Duo
APC & Memcache the High Performance DuoAPC & Memcache the High Performance Duo
APC & Memcache the High Performance Duo
Anis Berejeb
 
Cassandra Summit 2013 Keynote
Cassandra Summit 2013 KeynoteCassandra Summit 2013 Keynote
Cassandra Summit 2013 Keynote
jbellis
 
Ipc: aidl sexy, not a curse
Ipc: aidl sexy, not a curseIpc: aidl sexy, not a curse
Ipc: aidl sexy, not a curse
Yonatan Levin
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
Wesley Beary
 

Tendances (20)

Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!Cassandra summit 2013 - DataStax Java Driver Unleashed!
Cassandra summit 2013 - DataStax Java Driver Unleashed!
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniques
 
Clustering your Application with Hazelcast
Clustering your Application with HazelcastClustering your Application with Hazelcast
Clustering your Application with Hazelcast
 
Easy Scaling with Open Source Data Structures, by Talip Ozturk
Easy Scaling with Open Source Data Structures, by Talip OzturkEasy Scaling with Open Source Data Structures, by Talip Ozturk
Easy Scaling with Open Source Data Structures, by Talip Ozturk
 
Remove php calls and scale your site like crazy !
Remove php calls and scale your site like crazy !Remove php calls and scale your site like crazy !
Remove php calls and scale your site like crazy !
 
Tokyo cassandra conference 2014
Tokyo cassandra conference 2014Tokyo cassandra conference 2014
Tokyo cassandra conference 2014
 
Node.js in action
Node.js in actionNode.js in action
Node.js in action
 
load errorcmd
 load errorcmd load errorcmd
load errorcmd
 
Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)
 
APC & Memcache the High Performance Duo
APC & Memcache the High Performance DuoAPC & Memcache the High Performance Duo
APC & Memcache the High Performance Duo
 
Replication and Replica Sets
Replication and Replica SetsReplication and Replica Sets
Replication and Replica Sets
 
Network
NetworkNetwork
Network
 
Cassandra Summit 2013 Keynote
Cassandra Summit 2013 KeynoteCassandra Summit 2013 Keynote
Cassandra Summit 2013 Keynote
 
IPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curseIPC: AIDL is sexy, not a curse
IPC: AIDL is sexy, not a curse
 
Ipc: aidl sexy, not a curse
Ipc: aidl sexy, not a curseIpc: aidl sexy, not a curse
Ipc: aidl sexy, not a curse
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
fog or: How I Learned to Stop Worrying and Love the Cloud (OpenStack Edition)
 
Apache zookeeper 101
Apache zookeeper 101Apache zookeeper 101
Apache zookeeper 101
 
Results cache
Results cacheResults cache
Results cache
 
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
MongoDB London 2013: Basic Replication in MongoDB presented by Marc Schwering...
 

Similaire à Php user groupmemcached

Scalling web applications using memcache
Scalling web applications using memcacheScalling web applications using memcache
Scalling web applications using memcache
Sudar Muthu
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
guoqing75
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
yiditushe
 
Rails' Next Top Model
Rails' Next Top ModelRails' Next Top Model
Rails' Next Top Model
Adam Keys
 
Geneva JUG - Cassandra for Java Developers
Geneva JUG - Cassandra for Java DevelopersGeneva JUG - Cassandra for Java Developers
Geneva JUG - Cassandra for Java Developers
Michaël Figuière
 
Embedded Mirror Maker
Embedded Mirror MakerEmbedded Mirror Maker
Embedded Mirror Maker
Simon Suo
 
YaJug - Cassandra for Java Developers
YaJug - Cassandra for Java DevelopersYaJug - Cassandra for Java Developers
YaJug - Cassandra for Java Developers
Michaël Figuière
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
isnull
 
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back AgainPuppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet
 

Similaire à Php user groupmemcached (20)

Scalling web applications using memcache
Scalling web applications using memcacheScalling web applications using memcache
Scalling web applications using memcache
 
Using memcache to improve php performance
Using memcache to improve php performanceUsing memcache to improve php performance
Using memcache to improve php performance
 
Memcached Study
Memcached StudyMemcached Study
Memcached Study
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
MUC - Moodle Universal Cache
MUC - Moodle Universal CacheMUC - Moodle Universal Cache
MUC - Moodle Universal Cache
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
Rails' Next Top Model
Rails' Next Top ModelRails' Next Top Model
Rails' Next Top Model
 
Curator intro
Curator introCurator intro
Curator intro
 
Geneva JUG - Cassandra for Java Developers
Geneva JUG - Cassandra for Java DevelopersGeneva JUG - Cassandra for Java Developers
Geneva JUG - Cassandra for Java Developers
 
Embedded Mirror Maker
Embedded Mirror MakerEmbedded Mirror Maker
Embedded Mirror Maker
 
memcached Distributed Cache
memcached Distributed Cachememcached Distributed Cache
memcached Distributed Cache
 
YaJug - Cassandra for Java Developers
YaJug - Cassandra for Java DevelopersYaJug - Cassandra for Java Developers
YaJug - Cassandra for Java Developers
 
Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010Dutch php conference_apc_mem2010
Dutch php conference_apc_mem2010
 
Caching and tuning fun for high scalability @ LOAD2012
Caching and tuning fun for high scalability @ LOAD2012Caching and tuning fun for high scalability @ LOAD2012
Caching and tuning fun for high scalability @ LOAD2012
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
I Have the Power(View)
I Have the Power(View)I Have the Power(View)
I Have the Power(View)
 
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
Saving The World From Guaranteed APOCALYPSE* Using Varnish and Memcached
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back AgainPuppet Camp Charlotte 2015: Exporting Resources: There and Back Again
Puppet Camp Charlotte 2015: Exporting Resources: There and Back Again
 

Php user groupmemcached

  • 1. Intro to Memcached By Jason Anderson
  • 2. What is it? • Free and open source • High performance object caching server – Objects are stored in-memory – Multiple clients can share the same Memcached server • Developed for LiveJournal in 2003
  • 4. Installation Ubuntu/Debian • apt-get install memcached php5-memcache Fedora • yum install memcached php-pecl-memcache
  • 5. Basic Operations • $memcache = new Memcache(); – Creates a new Memcache object • $memcache->addServer($hostname, $port) – $hostname: Hostname or IP of Memcached server – $port: Port to connect on (Default: 11211) • $memcache->set($key, $value, $compress, $expire) – Each distinct $key maps to a single $value – $compress: Boolean defining whether or not to compress $value – $expire: Number of seconds until the $key, $value pair is deleted
  • 6. Basic Operations (Cont.) • $memcache->get($key) – Returns the stored value associated with $key. If no value has been assigned, NULL is returned. • $memcache->delete($key) – Removes the key/value pair associated with $key.
  • 7. Example $memcache = new Memcache(); // Connect to a Memcached server $memcache->addServer("localhost", 11211); // Time to answer the ultimate question. // If "ultimateAnswer" isn't in Memcache, fetch from DB if (!($ultimateAnswer = $memcache->get("ultimateAnswer"))) { $ultimateAnswer = (int)UltimateAnswer::dbGet(); // Add/Update the Memcache entry for 'ultimateAnswer' $memcache->set("ultimateAnswer", $ultimateAnswer, false, 60*24); } echo $ultimateAnswer; // 42 of course // Delete 'ultimateAnswer' from Memcache $memcache->delete("ultimateAnswer");
  • 9. class Tweet { private $_id; private $_postDate; private $_text; public function __construct($id, $postDate, $text) { $this->_id = $id; $this->_postDate = $postDate; $this->_text = $text; } public function getId() { return $this->_id; } public function getPostDate() { return $this->_postDate; } public function getText() { return $this->_text; } } $tweet = new Tweet(334089370476544, '2010-11-04 23:50:27', 'The best thing about telepathy is...I know, right?'); echo serialize($tweet); // O:5:"Tweet":3:{s:10:"Tweet_id";i:334089370476544;s:16:"Tweet_post Date";s:19:"2010-11-04 23:50:27";s:12:"Tweet_text";s:50:"The best thing about telepathy is...I know, right?";}
  • 10. // Add Tweet to Memcache $memcache->set('Tweet:' . $tweet->getId(), serialize($tweet)); // Fetch Tweet from Memcache $tweet = unserialize($memcache->get('Tweet:334089370476544'));
  • 12. class Tweet { ... private $_favorites; ... public function getFavorites() { // If the Favorites aren't in Memcache, fetch from DB if (!$this->_favorites = unserialize($memcache->get('Favorites:' . $this->_id))){ $this->_favorites = Favorites::dbGet($this->_id); // Add Favorites to Memcache $memcache->set('Favorites:' . $this->_id, serialize($this->_favorites), false, 60*24); } return $this->_favorites; } // What happens when this Tweet class gets serialized? }
  • 13. // $_favorites gets serialized. Not good. class Tweet { ... // Called before the class is serialized. // Returns list of class variable names to serialize. public function __sleep() { return array('_id', '_postDate', '_text'); // Won't serialize $_favorites } }
  • 15. class Tweet { ... private $_userId; private $_user; ... public function __sleep() { return array('_id', '_postDate', '_text', '_userId'); // Won't serialize $_favorites or $_user } ... public function getUser() { // If User isn't in Memcache, fetch from DB if (!$this->_user= unserialize($memcache->get('User:' . $this->_userId))) { $this->_user = User::dbGet($this->_userId); // Add the User to Memcache $memcache->set('User:' . $this->_userId, serialize($this->_user), false, 60*24); } return $this->_user; } }
  • 16. $tweet->getText() $tweet->getUser() $tweet->getPostDate() $tweet->getFavorites()
  • 17. Cache Stats $memcache->getStats(); Memcache Server version: 1.2.2 Process id of this server process 14038 Number of seconds this server has been 1896810 running Accumulated user time for this process 102.725383 seconds Accumulated system time for this process 398.138473 seconds Total number of items stored by this 881381 server ever since it started Number of open connections 11 Total number of connections opened since 1877 the server started running Number of connection structures 27 allocated by the server Cumulative number of retrieval requests 13062495 Cumulative number of storage requests 881381 Number of keys that have been requested 12181114 (93.253%) and found present Number of items that have been 881381(6.747%) requested and not found Total number of bytes read by this server 894.00982284546 Mega Bytes from network Total number of bytes sent by this server 5860.2824373245 Mega Bytes to network Number of bytes this server is allowed to 64 Mega Bytes use for storage. Number of valid items removed from 0 cache to free memory for new items.
  • 18. Questions? $name = 'Jason Anderson'; $email = ‘nectro@gmail.com'; $twitter = '@nectro'; $website['Twitter Aggregator'] = 'http://slothnod.com';