我做了这个功能: / * 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没有启用,但万一有人想把它搞砸一下
答案 0 :(得分:3)
您只能缓存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'];
答案 1 :(得分:1)
需要注意的重要一点是,您无法存储从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);
}
答案 2 :(得分:0)
Memcache不接受超过30天的TTL。您也可以使用ttl 0将密钥设置为永不过期,或者将ttl设置为少于30天。
eliminating memcached's 30-day limit
要创建一个易于序列化的变量,您可以执行以下操作。
$consulta = mysql_query($sql);
while ($row = mysql_fetch_assoc($consulta)) {
$data[] = $row;
}
$cache->set($nombre,$data, MEMCACHE_COMPRESSED, 60*60*24);