{"id":1065,"date":"2010-03-08T13:34:00","date_gmt":"2010-03-08T05:34:00","guid":{"rendered":"\/?p=1065"},"modified":"2010-03-08T13:34:00","modified_gmt":"2010-03-08T13:34:00","slug":"1065","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/php\/1065.html","title":{"rendered":"PHP Memcached + APC + \u6587\u4ef6\u7f13\u5b58\u5c01\u88c5"},"content":{"rendered":"

\u4f7f\u7528\u65b9\u6cd5\uff1a
Memcached
$cache = new Cache_MemCache();
$cache->addServer(‘www1’);
$cache->addServer(‘www2’,11211,20); \/\/ this server has double the memory, and gets double the weight
$cache->addServer(‘www3’,11211);<\/p>\n

\/\/ Store some data in the cache for 10 minutes
$cache->store(‘my_key’,’foobar’,600); <\/p>\n

\/\/ Get it out of the cache again
echo($cache->fetch(‘my_key’));<\/p>\n

\u6587\u4ef6\u7f13\u5b58
$cache = new Cache_File();<\/p>\n

$key = ‘getUsers:selectAll’;<\/p>\n

\/\/ check if the data is not in the cache already
if (!$data = $cache->fetch($key)) {
\/\/ assuming there is a database connection
$result = mysql_query("SELECT * FROM users");
$data = array();<\/p>\n

\/\/ fetching all the data and putting it in an array
while($row = mysql_fetch_assoc($result)) { $data[] = $row; }<\/p>\n

\/\/ Storing the data in the cache for 10 minutes
$cache->store($key,$data,600);
}
\u4e0b\u8f7d: class_cache3.php
<?php<\/p>\n

abstract class Cache_Abstract {
abstract function fetch($key);
abstract function store($key, $data, $ttl);
abstract function delete($key);
}<\/p>\n

class Cache_APC extends Cache_Abstract {<\/p>\n

function fetch($key) {
return apc_fetch($key);
}<\/p>\n

function store($key, $data, $ttl) {
return apc_store($key, $data, $ttl);
}<\/p>\n

function delete($key) {
return apc_delete($key);
}<\/p>\n

}<\/p>\n

class Cache_MemCache extends Cache_Abstract {
public $connection;<\/p>\n

function __construct() {
$this->connection = new MemCache;
}<\/p>\n

function store($key, $data, $ttl) {
return $this->connection->set($key, $data, 0, $ttl);
}<\/p>\n

function fetch($key) {
return $this->connection->get($key);
}<\/p>\n

function delete($key) {
return $this->connection->delete($key);
}<\/p>\n

function addServer($host, $port = 11211, $weight = 10) {
$this->connection->addServer($host, $port, true, $weight);
}<\/p>\n

}<\/p>\n

class Cache_File extends Cache_Abstract {<\/p>\n

function store($key, $data, $ttl) {
$h = fopen($this->getFileName($key), ‘a+’);
if (!$h)
throw new Exception(‘Could not write to cache’);
flock($h, LOCK_EX);
fseek($h, 0);
ftruncate($h, 0);
$data = serialize(array(time() + $ttl, $data));
if (fwrite($h, $data) === false) {
throw new Exception(‘Could not write to cache’);
}
fclose($h);
}<\/p>\n

function fetch($key) {
$filename = $this->getFileName($key);
if (!file_exists($filename))
return false;
$h = fopen($filename, ‘r’);
if (!$h)
return false;
flock($h, LOCK_SH);
$data = file_get_contents($filename);
fclose($h);
$data = @ unserialize($data);
if (!$data) {
unlink($filename);
return false;
}
if (time() > $data[0]) {
unlink($filename);
return false;
}
return $data[1];
}<\/p>\n

function delete($key) {
$filename = $this->getFileName($key);
if (file_exists($filename)) {
return unlink($filename);
}
else {
return false;
}
}<\/p>\n

private function getFileName($key) {
return ‘\/tmp\/s_cache’ . md5($key);
}<\/p>\n

}
?><\/p>\n

\u539f\u6587\u5730\u5740\uff1ahttp:\/\/www.21andy.com\/blog\/20100206\/1643.html<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f7f\u7528\u65b9\u6cd5\uff1a
Memcached
$cache = new Cache_MemCache();
$cache->addServer(‘www1’);
$cache->addServer(‘www2’,11211,20); \/\/ this server has double the memory, and gets double the weight
$cache->addServer(‘www3’,11211);<\/p>\n

\/\/ Store some data in the cache for 10 minutes
$cache->store(‘my_key’,’foobar’,600); <\/p>\n

\/\/ Get it out of the cache again
echo($cache->fetch(‘my_key’));<\/p>\n

\u6587\u4ef6\u7f13\u5b58
$cache = new Cache_File();<\/p>\n

$key = ‘getUse<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1065"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1065"}],"version-history":[{"count":0,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}