Reputation: 20163
i made this function: /* 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 */
}
}
wich i am using so:
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();
}
then:
$translationText = translation('hello','fr');
The problem is that it stores in cache arrays (always zero), var_dump($m->get(k))
returns:
int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0) int(0)
.....
And the $sql query is fine because the rows are collected fine and printed fine, the problem is with the stored value..
I have cleared the cache doing (several times, to make sure the values are not from a previous wrong output):
$consulta = $cache->get($nombre);
/* manually*/
$consulta = false;
if ( $consulta === false) {
$consulta = mysql_query($sql);
$cache->set($nombre,$consulta, MEMCACHE_COMPRESSED, 60*60*24);
};
so.. what am I missing?
EDIT
Here is a codepad, the problem is the mysql_query and memecache is not enabled, but in case someone wants to fiddle with it a bit
http://codepad.viper-7.com/PJNepH
Upvotes: 2
Views: 1743
Reputation: 5012
The important thing to note is that you cannot store a result resource returned from mysql_query
. I would recommend looping through the result set, fetching them with mysql_fetch_array
, and then storing those objects in the cache.
Edit As PaulP.R.O. pointed out, the explicit serialize/deserialize were redundant.
$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);
When retrieving from memcached, simply use the unserialized array.
$cachedItem = $cache->get($nombre);
if ($cachedItem !== false) {
var_dump($cachedItem);
}
Upvotes: 1
Reputation: 141827
You can only cache something that can be serialized. This includes everything but the type resource (which is returned from a successful mysql_query). You need to change your logic a bit so that you are caching an array. Change this:
$res = mysql_query($sql);
$cache->set($nombre,$res, 0, 60*60*24);
To:
$res = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($res)) $rows[] = $row;
$cache->set($nombre, $rows, 0, 60*60*24);
Then change this:
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;
}
To:
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'];
Upvotes: 3
Reputation: 12010
Memcache doesn't accept TTLs longer than 30 days. You can also use a ttl of 0 to set the key to never expire OR you set a ttl of less than 30 days.
eliminating memcached's 30-day limit
To create an easily serializable variable, you can do something like this.
$consulta = mysql_query($sql);
while ($row = mysql_fetch_assoc($consulta)) {
$data[] = $row;
}
$cache->set($nombre,$data, MEMCACHE_COMPRESSED, 60*60*24);
Upvotes: 0