开发者

memcached - how to use it for multiple records returned from single SELECT operation?

memchached is useful for caching and looking up of single independent records. For the multiple records returned from doing a SELECT operation, how could I make good use of the memcached for caching a开发者_高级运维nd looking up later?


I didn't get you. If you use php or .net (Enyim client) you can store your result in to an object and set it in to the memcached. (Client will serialize the object and store it in the memcached.)

Following example will store one or more records (results) returned by the db query.

//Init Memcache, Try avoid multiple init if possible  because this is a costly operation 
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

mysql_pconnect("localhost","root","");
mysql_select_db("YOURDB");

$query = "SELECT * FROM table where `firstname`='dasun';";
$key = md5($query);
$get_result = $memcache->get($key);

if ($get_result) {
    print_r($get_result);
    echo "It's a hit! :)";
}
else {
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    print_r($row);

    // Store the result for 5 minutes
    $memcache->set($key, $row, TRUE, 300); 
    echo "Not a hit. :(";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜