{"id":1078,"date":"2010-03-12T09:18:00","date_gmt":"2010-03-12T01:18:00","guid":{"rendered":"\/?p=1078"},"modified":"2010-03-12T09:18:00","modified_gmt":"2010-03-12T09:18:00","slug":"1078","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/php\/1078.html","title":{"rendered":"\u542f\u7528memcached\u538b\u7f29\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"

      \u5728php\u5f00\u53d1\u4e2d\uff0c\u5f00\u542fmemcache\u7684\u6570\u636e\u538b\u7f29\u5b58\u50a8\u662f\u4e00\u4ef6\u5f88\u7b80\u5355\u7684\u4e8b\u60c5\u3002\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u538b\u7f29\u6570\u636e\u4e0d\u4ec5\u4e0d\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u53cd\u5012\u4f1a\u56e0\u4e3a\u7f51\u7edc\u4f20\u8f93\u7684\u5f00\u9500\u964d\u4f4e\uff0c\u5e26\u6765\u901f\u5ea6\u63d0\u5347\u3002\u770b\u770b\u6700\u5e38\u7528\u7684Memcache::set\u65b9\u6cd5:
bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )<\/p>\n

\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5c06$flag\u8bbe\u7f6e\u4e3aMEMCACHE_COMPRESSED\u5373\u53ef\u542f\u7528memcache\u538b\u7f29\u5b58\u50a8\u3002
\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u5f0a\u7aef\uff1f<\/p>\n

\u5982\u679c\u6ca1\u6709\u505a\u989d\u5916\u5224\u65ad\uff0c\u6bcf\u4e00\u6b21\u5199\u5165memcache\u90fd\u4f1a\u542f\u7528\u538b\u7f29\uff0c\u4e0d\u7ba1\u6570\u636e\u7684\u5927\u5c0f\u3002\u5bf9\u5e94\u7684\uff0c\u6bcf\u6b21\u83b7\u5f97\u6570\u636e\u90fd\u9700\u8981\u505a\u4e00\u6b21\u89e3\u538b\u7f29\u7684\u64cd\u4f5c\uff0c\u8fd9\u662f\u5178\u578b\u7684\u4e00\u5200\u5207\u624b\u6cd5\u3002\u5b9e\u9645\u4e0a\u5728\u6570\u636e\u5f88\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u9700\u8981\u538b\u7f29\uff0c\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\u538b\u7f29\u7701\u4e0d\u4e86\u591a\u5c11\u7a7a\u95f4\u3002
\u66f4\u597d\u7684\u538b\u7f29\u7b56\u7565\uff1f<\/p>\n

\u597d\u4e86\uff0c\u6211\u7684\u60f3\u6cd5\u662f\u5728\u6570\u636e\u8d85\u8fc7\u4e00\u5b9a\u5927\u5c0f\uff08\u6bd4\u59822k\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u624d\u5f00\u542f\u538b\u7f29\u3002\u8fd9\u4e2a\u597d\u529e\uff0c\u634b\u8d77\u8896\u5b50\u5c31\u5e72\uff0c\u5728\u8c03\u7528Memcache::set\u65b9\u6cd5\u4e4b\u524d\uff0c\u9996\u5148\u5224\u65ad\u4e00\u4e0b\u6570\u636e\u7684\u5927\u5c0f\uff0c\u4e00\u4e2astrlen\u5c31\u641e\u5b9a\u4e86\uff0c\u518d\u7b80\u5355\u4e0d\u8fc7\u4e86\u3002
PLAIN TEXT
PHP:
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211);
$flag = strlen($data)> 2048 ? MEMCACHE_COMPRESSED : 0;
$memcache->set(‘mykey’, $data, $flag); <\/p>\n

\u6709\u4eba\u53ef\u80fd\u4f1a\u95ee\u4e86\uff0carray\u548cobject\u600e\u4e48\u529e\uff0c\u8fd9\u73a9\u610f\u53ef\u4e0d\u80fd\u7528strlen\u5224\u65ad\u957f\u5ea6\u3002<\/p>\n

\u8fd9\u8fd8\u771f\u80fd\u96be\u4f4f\u6211\u4e00\u9635\u5b50\uff0c\u8981\u77e5\u9053\u628aarray\/object\u5199\u5165memcache\u7684\u65f6\u5019\uff0cphp\u4f1a\u81ea\u52a8\u505aserialize\uff0c\u518d\u628a\u5b83\u5f53\u4f5c\u5b57\u7b26\u4e32\u63d2\u5165memcache\u3002
PLAIN TEXT
PHP:
$flag = strlen(serialize($data))> 2048 ? MEMCACHE_COMPRESSED : 0; <\/p>\n

\u8c01\u4f1a\u91c7\u7528\u8fd9\u6bb5\u4ee3\u7801\uff1f\u770b\u8d77\u6765\u975e\u5e38\u5c71\u5be8\uff0c\u800c\u4e14serialize\u4e5f\u4e0d\u5feb\uff0c\u8d54\u672c\u4e70\u5356\u3002
\u66f4\u597d\u7684\u529e\u6cd5\uff01<\/p>\n

\u4e0a\u9762\u7684\u6587\u5b57\u90fd\u662f\u5e9f\u8bdd\uff0c\u76f4\u63a5\u770b\u8fd9\u6bb5\u5c31\u597d\u3002Memcache::setCompressThreshold\u65b9\u6cd5\u53ef\u4ee5\u5305\u529e\u4e4b\u524d\u6240\u6709\u7684\u903b\u8f91\u3002<\/p>\n

Memcache::setCompressThreshold \u2015 Enable automatic compression of large values<\/p>\n

bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )<\/p>\n

\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4f1a\u81ea\u52a8\u542f\u7528\u538b\u7f29\u7b56\u7565\uff0c\u5f53\u6570\u636e\u5927\u4e8e2k\u65f6\uff0c\u4ee50.2\u7684\u538b\u7f29\u6bd4\u8fdb\u884czlib\u3002
PLAIN TEXT
PHP:
$memcache->setCompressThreshold(2000, 0.2); <\/p>\n

\u6839\u636e\u6211\u7684\u6d4b\u8bd5\u7ed3\u679c\uff0csetCompressThreshold\u65b9\u6cd5\u4f1a\u5ffd\u7565Memcache::set\u7684flag\u53c2\u6570\u3002<\/p>\n

\u5f15\u7528\uff1a<\/strong>http:\/\/www.ooso.net\/archives\/475<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u5728php\u5f00\u53d1\u4e2d\uff0c\u5f00\u542fmemcache\u7684\u6570\u636e\u538b\u7f29\u5b58\u50a8\u662f\u4e00\u4ef6\u5f88\u7b80\u5355\u7684\u4e8b\u60c5\u3002\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u538b\u7f29\u6570\u636e\u4e0d\u4ec5\u4e0d\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u53cd\u5012\u4f1a\u56e0\u4e3a\u7f51\u7edc\u4f20\u8f93\u7684\u5f00\u9500\u964d\u4f4e\uff0c\u5e26\u6765\u901f\u5ea6\u63d0\u5347\u3002\u770b\u770b\u6700\u5e38\u7528\u7684Memcache::set\u65b9\u6cd5:
bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )<\/p>\n

\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5c06$flag\u8bbe\u7f6e\u4e3aMEMCACHE_COMPRESSED\u5373\u53ef\u542f\u7528memcache\u538b\u7f29\u5b58\u50a8\u3002
\u8fd9\u6837\u505a\u6709\u4ec0\u4e48\u5f0a\u7aef\uff1f<\/p>\n

\u5982\u679c\u6ca1\u6709\u505a\u989d\u5916\u5224\u65ad\uff0c\u6bcf\u4e00\u6b21\u5199\u5165memcache\u90fd\u4f1a\u542f\u7528\u538b\u7f29\uff0c\u4e0d\u7ba1\u6570\u636e\u7684\u5927\u5c0f\u3002\u5bf9\u5e94\u7684\uff0c\u6bcf\u6b21\u83b7\u5f97\u6570<\/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\/1078"}],"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=1078"}],"version-history":[{"count":0,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1078\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}