سؤال

لقد قمت أخيرًا بتشغيل memcache على جهاز الكمبيوتر المنزلي الخاص بي حتى أتمكن من البدء في التطوير باستخدامه أخيرًا!

لم أبدأ بداية جيدة على الرغم من أنني أحاول استخدام الكود

php.net @ مجموعة memcache لا أستطيع الحصول على أي رمز مثال للعمل الذي ينشرونه

حاولت هذا:

<?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 قد ترغب في مشاركته وسأكون ممتنًا حقًا لرؤية بعض الأمثلة العملية

هل كانت مفيدة؟

المحلول

هل تدرك أنك بحاجة إلى استبدال "memcache_host" باسم المضيف و/أو المضيف المحلي؟أم أنني أفتقد النقطة تمامًا؟حاول أيضًا أن telnet localhost 11211 وثم telnet your-memcache-host-name 11211 ومعرفة ما إذا كنت ستحصل على نفس النتيجة (يجب عليك ذلك).

نصائح أخرى

إذا كنت تريد استخدام 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;
        }
    }

أنا أستخدم menarche مع 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 !!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top