Domanda

Finalmente ho avuto memcache in esecuzione sul mio pc di casa in modo da poter finalmente iniziare a sviluppare con esso!

Non sto iniziando bene anche se sto cercando di utilizzare il codice su

php.net @ memcache-set Non riesco a far funzionare entrambi i codici di esempio che pubblicano

Ho provato questo:

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


E poi

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


E ottenuto questi errori dal codice sopra;

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


Ho quindi trovato questo codice sulla rete da qualche parte e funziona

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


Come posso far funzionare gli esempi da PHP.net?


Inoltre mi piacerebbe vedere qualsiasi codice di esempio che coinvolge memcache che potresti voler condividere. Mi farebbe molto piacere vedere alcuni esempi funzionanti

È stato utile?

Soluzione

Ti rendi conto che devi sostituire " memcache_host " con il tuo nome host e / o localhost? O mi manca completamente il punto? Inoltre, prova a telnet localhost 11211 e poi telnet your-memcache-host-name 11211 e vedi se ottieni lo stesso risultato (dovresti).

Altri suggerimenti

Se vuoi usare Memcached con PHP per una query nel database, ecco un esempio di quello che ho usato:

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

Sto usando menarche con php per ridurre l'hit del mio database facendo qualcosa del genere

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

Inoltre puoi ottenere gli esempi su http://php.net/manual/en /memcache.set.php !!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top