我做了这个函数:/* 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 没有启用,但万一有人想摆弄它