你如何在 PHP 中使用 memcache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1206283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you use memcache with PHP
提问by JasonDavis
I finally got memcache running on my home pc so I can start developing with it at last!
我终于在我的家用电脑上运行了 memcache,所以我终于可以开始使用它进行开发了!
I am not off to a good start though I am trying to use the code on
尽管我正在尝试使用代码,但我的开端并不好
php.net @ memcache-setI am unable to get either example code to work that they post
php.net @ memcache-set我无法让他们发布的示例代码工作
I tried this:
我试过这个:
<?php
/* procedural API */
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?>
And then
进而
<?php
/* OO API */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$memcache_obj->set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj->get('var_key');
?>
And got these errors from the code above;
并从上面的代码中得到这些错误;
Warning: Memcache::connect() [memcache.connect]: Can't connect to memcache_host:11211, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060) in C:\webserver\htdocs\test\memcache\index.php on line 36
Warning: Memcache::set() [memcache.set]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 42
Warning: Memcache::get() [memcache.get]: Failed to extract 'connection' variable from object in C:\webserver\htdocs\test\memcache\index.php on line 44
I then found this code on the net somewhere and it does work
然后我在网上某处找到了这段代码,它确实有效
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
// add cache
$memcache->set('key', $tmp_object, false, 30) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 30 seconds)<br/>\n";
// get cache
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>
How can I get the examples from PHP.net to work though?
我怎样才能让 PHP.net 中的示例工作呢?
Also I would love to see any emample code involving memcache you might want to share I would really appreciate seeing some working examples
此外,我很想看到您可能想分享的任何涉及 memcache 的示例代码,我真的很感激看到一些工作示例
回答by Nir Levy
You do realise that you need to replace "memcache_host" with your hostname and/or localhost? Or am I missing the point completly? Also, try to telnet localhost 11211and then telnet your-memcache-host-name 11211and see if you get the same result (you should).
您确实意识到需要用您的主机名和/或 localhost 替换“memcache_host”?还是我完全错过了这一点?另外,尝试telnet localhost 11211然后telnet your-memcache-host-name 11211看看你是否得到相同的结果(你应该)。
回答by VIKASH
If you want to use Memcached with PHP for a database query, here is an example of what I used:
如果您想将 Memcached 与 PHP 一起用于数据库查询,以下是我使用的示例:
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211);
$qry = QUERY;
$C = connection to ur database;
findValue($qry, $c);
function findValue($qry,$c)
{
$id = md5($qry);
if ($gotten = $memcache->get($id)) {
echo $id." retrieved from memcached </br> ";
return $gotten;
} else {
### Daemon running but it was NOT cached
echo " from database (was NOT cached)";
# Never mind - fetch it and store for next time!
$gotten = dbfetch($qry,$c);
$memcache->set($id,$gotten);
return $gotten;
}
}
回答by rxpande
I am using menarche with php for reducing my database hit by doing some thing like this
我正在使用带有 php 的月经初潮,通过做这样的事情来减少我的数据库命中
$memcache = new Memcache;
//Ip address and and port number.
$memcache->connect('192.168.xxx.xxx', 'xxxx');
//Fetching data from memcache server
$arrobj = $memcache->get("arrobj");
if( false == is_array( $arrobj ) ) {
$arrobj = data retrieve from Database.
//Storing data in memcache server for 100 sec.
$memcache->set( "arrobj", $arrobj, MEMCACHE_COMPRESSED, 100 );
}
Also you can get the examples at http://php.net/manual/en/memcache.set.php!!

