Php user groupmemcached

Post on 03-Dec-2014

557 views 1 download

description

 

transcript

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_postDate";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->getFavorites()

$tweet->getPostDate()

Cache Stats

Memcache Server version: 1.2.2

Process id of this server process 14038

Number of seconds this server has been running 1896810

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 server ever since it started 881381

Number of open connections 11

Total number of connections opened since the server started running 1877

Number of connection structures allocated by the server 27

Cumulative number of retrieval requests 13062495

Cumulative number of storage requests 881381

Number of keys that have been requested and found present 12181114 (93.253%)

Number of items that have been requested and not found 881381(6.747%)

Total number of bytes read by this server from network 894.00982284546 Mega Bytes

Total number of bytes sent by this server to network 5860.2824373245 Mega Bytes

Number of bytes this server is allowed to use for storage. 64 Mega Bytes

Number of valid items removed from cache to free memory for new items. 0

$memcache->getStats();

Questions?

$name = 'Jason Anderson'; $email = ‘nectro@gmail.com'; $twitter = '@nectro'; $website['Twitter Aggregator'] = 'http://slothnod.com';