문제

마침내 홈 PC에서 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와 관련된 Emamle 코드를보고 싶습니다.

도움이 되었습니까?

해결책

"memcache_host"를 호스트 이름 및/또는 로컬 호스트로 바꿔야한다는 것을 알고 있습니까? 아니면 포인트를 완전히 놓치고 있습니까? 또한 시도하십시오 telnet localhost 11211 그리고 telnet your-memcache-host-name 11211 그리고 동일한 결과를 얻는 지 확인하십시오.

다른 팁

데이터베이스 쿼리에 PHP와 함께 MemCached를 사용하려면 다음은 다음과 같은 예를 제공합니다.

$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;
        }
    }

나는 이와 같은 일을함으로써 내 데이터베이스를 줄이기 위해 PHP와 함께 Menarche를 사용하고 있습니다.

    $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