2009-07-30 64 views
8

我終於在家用電腦上運行了memcache,所以我終於可以開始開發它了!如何在PHP中使用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的例子。淨工作雖然?


我也很想看到涉及的memcache任何emample代碼,你可能想分享我真的很感激看到一些工作實例

+0

是否使用默認端口號(11211)設置了memcache,並設置爲在所有接口上偵聽? – Kazar 2009-07-30 12:32:31

+0

這裏是我SETING [內存緩存] memcache.allow_failover = 1個 memcache.max_failover_attempts = 20 memcache.chunk_size = 8192 memcache.default_port = 11211 – JasonDavis 2009-07-30 13:08:50

回答

10

你一定要明白,你需要替換「memcache_host」與你的主機名和/或本地主機?或者我完全錯過了這一點?此外,請嘗試telnet localhost 11211然後telnet your-memcache-host-name 11211並查看是否得到相同的結果(您應該)。

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; 
     } 
    } 
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得到的例子!