你如何在PHP中使用memcache

时间:2009-07-30 12:29:28

标签: php memcached

我终于在家用电脑上运行了memcache,所以我终于可以开始使用它了!

虽然我正在尝试使用

上的代码,但我还没有良好的开端

php.net @ memcache-set 我无法获得他们发布的示例代码

我试过了:

<?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');
?>

<小时/>

然后

<?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');
?>

<小时/>

从上面的代码中得到了这些错误;

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

<小时/>

然后我在网上找到了这个代码并且确实有效

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

<小时/>

如何让PHP.net中的示例正常工作?


我也很乐意看到任何涉及memcache的emample代码,你可能想分享我真的很感激看到一些有用的例子

3 个答案:

答案 0 :(得分:10)

您是否意识到需要将“memcache_host”替换为您的主机名和/或localhost?还是我完全忽略了这一点?另外,尝试telnet localhost 11211然后telnet your-memcache-host-name 11211,看看你是否得到相同的结果(你应该)。

答案 1 :(得分:2)

如果您想将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;
        }
    }

答案 2 :(得分:1)

我正在使用基于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 );
    }

您也可以在http://php.net/manual/en/memcache.set.php !!

获取示例