Reputation: 16095
Here is my code to "store" mysql queries with memcache
$mem = new Memcache;
$mem->connect('127.0.0.1', 11211);
$query = "SELECT * FROM tbl limit 0,20 ";
$key = md5($query);
$get_data = $memcache->get($key);
if($get_data) {
echo 'Data Pulled From Cache';
} else {
$res = mysql_fetch_array(mysql_query($query));
$memcache->set($key, $res, TRUE, 3600);
}
The problem is that memcache store only the first row returned by query. How to save all 20 rows within one key in memcache ?
Upvotes: 2
Views: 3234
Reputation: 41050
Instead of
$res = mysql_fetch_array(mysql_query($query));
$memcache->set($key, $res, TRUE, 3600);
use mysql_fetch_array()
with while
, check this
Upvotes: 1
Reputation: 533
The statement
$res = mysql_fetch_array(mysql_query($query);
only fetches ONE row from the query result.
Try something like this to fetch the entire result before storing it in the cache:
$res = array();
$qres = mysql_query($query);
while($a = mysql_fetch_array($qres, MYSQL_ASSOC))
$res[] = $a;
mysql_free_result($res);
$memcache->set($key, $res, TRUE, 3600);
Upvotes: 7