Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP缓存MySQL查询结果的最简单方法是什么?_Php_Mysql_Caching - Fatal编程技术网

使用PHP缓存MySQL查询结果的最简单方法是什么?

使用PHP缓存MySQL查询结果的最简单方法是什么?,php,mysql,caching,Php,Mysql,Caching,每次有人登陆我的页面list.php?id=xxxxx时,它都会重新查询一些MySQL查询以返回以下内容: $ids = array(..,..,..); // not big array - not longer then 50 number records $thumbs = array(..,..,..); // not big array - not longer then 50 text records $artdesc = "some text not very long"; //

每次有人登陆我的页面
list.php?id=xxxxx
时,它都会重新查询一些MySQL查询以返回以下内容:

$ids = array(..,..,..); // not big array - not longer then 50 number records
$thumbs = array(..,..,..); // not big array - not longer then 50 text records
$artdesc = "some text not very long"; // text field
因为我进行查询的数据库很大,我想把这个结果缓存在一个文件中24小时,比如:
xxxxx.php
在/cache/目录中,这样我就可以在
include(“xxxxx.php”)
中使用它了。(或txt文件!?,或任何其他方式)

因为有非常简单的数据,我相信它可以使用一些PHP行来完成,而不需要使用memcached或其他专业对象

由于我的PHP非常有限,有人能为这个任务放置PHP主线(或代码)吗


我真的非常感激

缓存PHP数组非常简单:

file_put_contents($path, '<?php return '.var_export($my_array,true).';?>');

您可能也希望这样做,它在内部提供缓存。

只需编写一个名为$\u GET['id']的新文件,以及您希望缓存的内容,每次检查该文件是否存在,否则就创建一个。大概是这样的:

$id = $_GET['id']

if (file_exists('/a/dir/' . $id)) {
  $data = file_get_contents('/a/dir/' . $id);
} else {
  //do mysql query, set data to result
  $handle = fopen('/a/dir/' . $id, 'w+');
  fwrite($handle, $data);
  fclose($handle);
}
尝试使用序列化

假设您在两个数组中获取数据
$array1
$array2
。现在需要做的是将这些数组存储在文件中。将字符串(问题中的第三个变量)存储到文件很容易,但要存储数组,必须首先将其转换为字符串

$string_of_array1 = serialize( $array1 );
$string_of_array2 = serialize( $array2 );
下一个问题是缓存文件的命名,这样您就可以轻松地检查缓存中是否已经存在相关数组。最好的方法是创建mysql查询的MD5哈希,并将其用作缓存文件名

$cache_dir = '/path/cache/';

$query1 = 'SELECT many , fields FROM first_table INNER JOIN another_table ...';
$cache1_filename = md5( $query1 );

if( file_exists( $cache_dir . $cache1_filename ) )
{
    if( filemtime( $cache_dir . $cache1_filename ) > ( time( ) - 60 * 60 * 24 ) )
    {
        $array1 = unserialize( file_get_contents( $cache_dir . $cache1_filename ) );
    }
}

if( !isset( $array1 ) )
{
    $array1 = run_mysql_query( $query1 );
    file_put_contents( serialize( $array1 ) );
}
对另一个数组重复上述操作,该数组应存储在单独的文件中,第二个查询的MD5用作第二个缓存文件的名称

最后,您必须决定缓存应保持有效的时间。对于同一查询,mysql表中的记录可能会发生更改,从而使文件系统缓存过时。因此,您不能仅仅依靠唯一的文件名进行唯一的查询

重要提示:

  • 必须删除旧的缓存文件。您可能需要编写一个例程来检查目录中的所有文件,并删除超过n秒的文件
  • 将缓存目录保持在webroot之外

然后有人用
script.php?id=../../etc/passwd
或其他有趣的东西调用您的脚本。清理用户输入…Ty lemondrop;据我所知,文件本身没有任何扩展名,对吗?如何将我的3个项目(2个数组+1个变量)放入$data?此外,如果你能提供我的方式来检查24小时缓存,这将是伟大的!!我想我明白了。将尝试按照您描述的步骤实施它。非常感谢Hamid。我修改了答案,添加了24小时缓存有效性的解决方案。非常感谢您以这种方式处理此问题。它确实指示我为我的案例构建一个简单的解决方案!
$cache_dir = '/path/cache/';

$query1 = 'SELECT many , fields FROM first_table INNER JOIN another_table ...';
$cache1_filename = md5( $query1 );

if( file_exists( $cache_dir . $cache1_filename ) )
{
    if( filemtime( $cache_dir . $cache1_filename ) > ( time( ) - 60 * 60 * 24 ) )
    {
        $array1 = unserialize( file_get_contents( $cache_dir . $cache1_filename ) );
    }
}

if( !isset( $array1 ) )
{
    $array1 = run_mysql_query( $query1 );
    file_put_contents( serialize( $array1 ) );
}