この関数を作成しました:/ * 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が有効になっていませんが、誰かがそれを少しいじりたい場合に備えて