我做了这个函数:/* 内存缓存 */
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
您只能缓存可以序列化的内容。 这包括除资源类型(从成功的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'];
需要注意的重要一点是,您不能存储从 mysql_query
返回的结果资源。我建议循环访问结果集,使用 mysql_fetch_array
获取它们,然后将这些对象存储在缓存中。
编辑 正如PaulP.R.O.指出的那样,显式序列化/反序列化是多余的。
$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);
}
Memcache 不接受超过 30 天的 TTL。您还可以使用 ttl 0 将密钥设置为永不过期,或者将 ttl 设置为小于 30 天。
消除 memcached 的 30 天限制
要创建一个易于序列化的变量,您可以执行以下操作。
$consulta = mysql_query($sql);
while ($row = mysql_fetch_assoc($consulta)) {
$data[] = $row;
}
$cache->set($nombre,$data, MEMCACHE_COMPRESSED, 60*60*24);