I am having a problem with Memcache being unable to set a key. Basically I have my testing page:
$db=new db;
$my_test_cache=$db->query("SELECT `txt` FROM `lang` WHERE `pg`='front' LIMIT 2", 10);
And a db class for caching:
class db {
public function query($query, $ttl=30, $cache=true, $compr=false) {
global $memcache;
if ($cache==true) {
$res=$memcache->get(hash('md4', $query));
if ($res!=false) {
echo 'this is cached';
return $res;
}
else {
$res=mysql_query($query);
$memcache->set(hash('md4', $query), $res, $compr, $ttl);
echo 'this is not cached<hr>
Query: ',$query,'<br/>
Md4: ',hash('md4',$query),'<br/>';
var_dump($res);
return $res;
}
}
else return mysql_query($query);
unset($res, $query);
}
}
Sadly, this does not set any data to cache, even though all the resources seem to work properly. AKA my page outputs:
- this is not cached Query: SELECT * FROM
langWHEREpg=’front’
LIMIT 2 - Md4: 34aa4e46a15413f5091dac79c9e86306
- resource(7) of type (mysql result)
Would anyone be so kind as to tip me off on what to do here?
Typo:
As well, unless memcache is far smarter than I think it is, you’re simply cacheing a query result handle, not the actual query results:
At that point, $res is just a randomish number, NOT the
txtfield you requested. You have to first FETCH a row from that result in order to retrieve the data: