gpt4 book ai didi

php - Memcache->get 返回不同的数组

转载 作者:行者123 更新时间:2023-11-29 01:44:24 25 4
gpt4 key购买 nike

我做了这个功能:/* 内存缓存 */

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

最佳答案

您只能缓存可以是 serialized 的内容.这包括除类型 resource 之外的所有内容(从成功的 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'];

关于php - Memcache->get 返回不同的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com