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