【发布时间】:2012-05-26 21:52:56
【问题描述】:
我做了这个功能: /* 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 未启用,但以防有人想稍微摆弄一下
【问题讨论】:
-
传递
$sql但使用$query,传递$tiempo但使用$iempo可以吗? -
是的。您想知道那个变量实际上是什么,不是吗?
-
嘿!最后我看到了 var_dump: boolean(false) 所以它不是finidng它。还稍微编辑了函数@zerkms
-
您为什么要尝试使用 memcache,而 MySQL query cache 应该对您的应用程序完全透明地完成这项工作?
-
@eggyal mysql 负载过多.. 我知道这不是正确的解决方法,但它会为我争取一些时间.. 我想知道如何做到这一点:)
标签: php mysql object memcached