2

この関数を作成しました:/ * MEMCACHE * /

function cache_query($sql,$nombre,$tiempo = -1){
    $cache = new Memcache();
    $cache->pconnect('localhost',11211);
    $query_cacheada = $cache->get($nombre);
    if ( $query_cacheada  === false ) {
             /* key not in memcache, perfom query, cache it and return */
         $res = mysql_query($sql);
         $cache->set($nombre,$res, 0, 60*60*24);  
         return $res; /* this looks good */
    }else{
             /* key in memcache, just return cached  */
         return $query_cacheada;  /* this doesnt return right elements */
    }
}

私がそう使っているのは:

class text{

    protected $id;
    protected $key;
    protected $language;
    protected $text;

    function __construct($clave,$lan){
       $consulta = cache_query("SELECT * FROM textos  
                                WHERE clave = '$clave' AND lengua = '$lan'" ,"TRANSLATION_".$clave."_".$lan);


        if(mysql_num_rows($consulta)>0){
            while($item = mysql_fetch_array($consulta)){
                $this->id = $item['id'];
                $this->clave = $item['key'];
                $this->lengua = $item['language'];
                $this->texto = $item['text'];

            }
                return true;
         }
    }
    function get_text(){
          return $this->text;
    }
}
function translation($key,$language){
     $tem = new text($key,$language);
     return $tem->get_text();
}

それから:

$translationText = translation('hello','fr');

問題は、キャッシュ配列(常にゼロ)に格納され、次をvar_dump($m->get(k))返すことです。

int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0)....。

また、行が正常に収集されて正常に出力されるため、$ sqlクエリは正常です。問題は、格納された値にあります。

キャッシュをクリアしました(値が以前の間違った出力からのものでないことを確認するために、数回):

$consulta = $cache->get($nombre);
             /* manually*/
             $consulta = false;
        if ( $consulta === false) {
            $consulta = mysql_query($sql);
            $cache->set($nombre,$consulta, MEMCACHE_COMPRESSED, 60*60*24);
        };

だから..私は何が欠けていますか?

編集

これがコードパッドです。問題はmysql_queryであり、memecacheが有効になっていませんが、誰かがそれを少しいじりたい場合に備えて

http://codepad.viper-7.com/PJNepH

4

3 に答える 3

3

シリアル化できるものだけをキャッシュできます。これには、タイプリソース(成功したmysql_queryから返される)以外のすべてが含まれます。配列をキャッシュするように、ロジックを少し変更する必要があります。これを変える:

$res = mysql_query($sql);
$cache->set($nombre,$res, 0, 60*60*24); 

に:

$res = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($res)) $rows[] = $row;
$cache->set($nombre, $rows, 0, 60*60*24); 

次に、これを変更します。

if(mysql_num_rows($consulta)>0){
    while($item = mysql_fetch_array($consulta)){
        $this->id = $item['id'];
        $this->clave = $item['key'];
        $this->lengua = $item['language'];
        $this->texto = $item['text'];

    }
    return true;
}

に:

foreach($consulta as $item){
       $this->id = $item['id'];
       $this->clave = $item['key'];
       $this->lengua = $item['language'];
       $this->texto = $item['text'];
}

// This is your old code written to work with a 2D array instead of a resource,
// But this keeps overwriting the same variables in a loop,
// if you selected multiple rows; otherwise you don't even need a loop and can just do:

$this->id = $consulta[0]['id'];
$this->clave = $consulta[0]['key'];
$this->lengua = $consulta[0]['language'];
$this->texto = $consulta[0]['text'];
于 2012-05-28T23:19:41.900 に答える
1

注意すべき重要なことは、から返された結果リソースを保存できないmysql_queryことです。結果セットをループし、でフェッチしてmysql_fetch_arrayから、それらのオブジェクトをキャッシュに保存することをお勧めします。

編集PaulP.ROが指摘したように、明示的なシリアル化/逆シリアル化は冗長でした。

$result = mysql_query($sql) or die("Query failed");

$results = array();

while ($array = mysql_fetch_array($result))
{
    $results[] = $array;
}

$cache->set($nombre, $results, MEMCACHE_COMPRESSED, 60*60*24);

memcachedから取得するときは、シリアル化されていない配列を使用するだけです。

$cachedItem = $cache->get($nombre);

if ($cachedItem !== false) {
    var_dump($cachedItem);
}
于 2012-05-28T23:17:34.813 に答える
0

Memcacheは30日を超えるTTLを受け入れません。0のttlを使用して、キーが期限切れにならないように設定することも、30日未満のttlを設定することもできます。

memcachedの30日間の制限をなくす

簡単にシリアル化できる変数を作成するには、次のようにします。

$consulta = mysql_query($sql);   
while ($row = mysql_fetch_assoc($consulta)) {
  $data[] = $row;
}
$cache->set($nombre,$data, MEMCACHE_COMPRESSED, 60*60*24);
于 2012-05-27T21:01:11.620 に答える