简体   繁体   English

PHP-Memcached:MySql资源缓存

[英]PHP - Memcached : MySql resource caching

This does not work 这行不通

    $get_data_qry   = "SELECT * FROM list;";
    $get_data_res   = $db->Query($get_data_qry);

    $key           = 'someKey';  /** silly mistake corrected after being notified by comments*/
    $get_data_res   = $memcache->get($key);

    if ($get_data_res) {
        PushOutput($get_data_res);
        echo "<br/>Directly from cache";
    }

    else {
        $get_data_res   = $db->Query($get_data_qry);
        $memcache->set($key, $get_data_res, 0, 20000);  /*Store the MySql resource in the cache*/
        PushOutput($get_data_res);
    }

I get the following message: PHP Warning: Memcache::set() expects parameter 1 to be string, resource given in E:\\Repository\\HTML\\tooldent\\songs\\songList.tpl on line 54. 我收到以下消息: PHP Warning: Memcache::set() expects parameter 1 to be string, resource given in E:\\Repository\\HTML\\tooldent\\songs\\songList.tpl on line 54.

It seems weird, why a resource cannot be cached? 似乎很奇怪,为什么无法缓存资源? Any alternatives? 有其他选择吗?

You just can't cache a resource. 您只是无法缓存资源。 The data being cached needs to be serializable: 缓存的数据需要可序列化:

http://php.net/manual/en/memcache.set.php http://php.net/manual/en/memcache.set.php

Remember that resource variables (ie file and connection descriptors) cannot be stored in the cache, because they cannot be adequately represented in serialized state. 请记住,资源变量(即文件和连接描述符)不能存储在缓存中,因为它们不能在序列化状态下充分表示。

You would need to create an array from your data if you want to cache it. 如果要缓存它,则需要根据数据创建一个数组。 Here is a function from my website: 这是我网站上的功能:

function query_rows_cached($query){
$rowset = $memcache->get(md5($query));
if($rowset){
    return $rowset;
}
else{
    $r=mysql_fetch_array(mysql_query($q));
    $days=1;
    $memcache->set(md5($query),$r, false, 60*60*24*$days);
    return $r;
}
}

This function works well if the query only returns one row. 如果查询仅返回一行,则此功能运行良好。 If your query returns multiple rows, you might have to do some work on the results. 如果查询返回多行,则可能必须对结果做一些工作。 Specifically, go through the results and put them in a multidemensional array as talisin says. 具体来说,请按照结果进行浏览,然后将它们按多维数据集排列(如护目素所述)。

If your $db construct use mysql_query: 如果您的$ db构造使用mysql_query:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success.

A resultset is not a string and you set $key to the resultset at line 4. 结果集不是字符串,您在第4行将$ key设置为结果集。

Update: 更新:

$result = $db->query( "SELECT * FROM table" );
$memcache->set(1,$result,TRUE,86400) or die ("Failed to save data");
$cached_result = $memcache->get(1);
var_dump($cached_result);

Output: 输出:

object(mysql_result)#2 (0) { }

But as far as I know you can't treat this object as a normal mysql object: 但据我所知,您不能将此对象视为普通的mysql对象:

 mysql_fetch_assoc($cached_result)

You can try to create a multidimensional array out of the resultset for memcache. 您可以尝试从结果集中为内存缓存创建多维数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM