最近为了提高实例检索过程的速度,看了一些memcache 的资料,开始认为可以向memcache发送SQL查询,但看了 memcache_engine + memcachedb = 高性能分布式内存数据库 这篇文章,明白不可以向 memcache发送SQL语句,memcache只提供key-value对,此处,key可以是一个SQL查询语句,value可以是一个查询结果集,
如下例所示:(来自 http://tech.ddvip.com/2009-06/1244806953123497.html,还可参见 http://info.codepub.com/2008/08/info-21343.html一文)
写个简单脚本测试一下。
<?php
$host = '192.168.1.21:3306';
$user = 'webuser';
$passwd = '123456';
$db = 'test';
$conn = mysql_connect($host,$user,$passwd);
mysql_select_db($db,$conn);
$sql = 'select * from t order by id desc';
$result = mysql_query($sql,$conn);
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
$test_key[] = $row;
}
$sql = md5($sql);
$mem = new Memcache;
$mem->connect("192.168.1.21", 11211);
$mem->set($sql,$test_key, MEMCACHE_COMPRESSED, 600);
print_r($mem->get($sql));
?>
看看结果出来了。
C:\>php -f "d:/lamp/web2/phpinfo.php"
Array
(
[0] => Array
(
[id] => d8f1ec2a-c033-11dd-bd1a-002215c94322
[username] => david
)
[1] => Array
(
[id] => d8f1eb9e-c033-11dd-bd1a-002215c94322
[username] => Sivia
)
[2] => Array
(
[id] => d8f1ea9a-c033-11dd-bd1a-002215c94322
[username] => Lucy
)
[3] => Array
(
[id] => d8f1e658-c033-11dd-bd1a-002215c94322
[username] => Livia
)
)