+ All Categories
Home > Documents > Php user groupmemcached

Php user groupmemcached

Date post: 03-Dec-2014
Category:
Upload: jason-anderson
View: 557 times
Download: 1 times
Share this document with a friend
Description:
 
18
Intro to Memcached By Jason Anderson
Transcript
Page 1: Php user groupmemcached

Intro to Memcached

By Jason Anderson

Page 2: Php user groupmemcached

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

Page 3: Php user groupmemcached

Who uses it?

Page 4: Php user groupmemcached

Installation

Ubuntu/Debian • apt-get install memcached php5-memcache

Fedora • yum install memcached php-pecl-memcache

Page 5: Php user groupmemcached

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

Page 6: Php user groupmemcached

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.

Page 7: Php user groupmemcached

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");

Page 8: Php user groupmemcached

$tweet->getText()

$tweet->getPostDate()

Page 9: Php user groupmemcached

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?";}

Page 10: Php user groupmemcached

// Add Tweet to Memcache $memcache->set('Tweet:' . $tweet->getId(), serialize($tweet)); // Fetch Tweet from Memcache $tweet = unserialize($memcache->get('Tweet:334089370476544'));

Page 11: Php user groupmemcached

$tweet->getFavorites()

Page 12: Php user groupmemcached

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? }

Page 13: Php user groupmemcached

// $_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 } }

Page 14: Php user groupmemcached

$tweet->getUser()

Page 15: Php user groupmemcached

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

Page 16: Php user groupmemcached

$tweet->getText() $tweet->getUser()

$tweet->getFavorites()

$tweet->getPostDate()

Page 17: Php user groupmemcached

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

Page 18: Php user groupmemcached

Questions?

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


Recommended