如何缓存这个mysql查询的车辆品牌和型号?

时间:2014-12-05 21:41:36

标签: php mysql caching

我有一些PHP代码,用于查询年份品牌和型号的mysql数据库。这一年在PHP中是硬编码的,因为它是少量数据。但是,根据之前的选择从数据库中查询make和model。 IE:选择2012允许仅在2012年制造汽车品牌,然后在选择品牌后列出所选品牌制造的车型。下面是我创建查询以选择make的代码。在调用一段时间后加速后续查询后如何缓存?或者甚至可能吗?

我应该提一下,我的托管服务提供商不允许我为mysql启用缓存。这就是为什么我想在php中尝试它。

$year = $_POST['year'];     // get the year from post - sent via ajax

$vehmakes = $cl->getmakes($year);
$output = '<option value="">-- All Makes --</option>';
while($row = mysql_fetch_row($vehmakes)){
$output .= '<option value="'.$row[0].'">'.$row[0].'</option>/n';
};

echo $output;

谢谢!

1 个答案:

答案 0 :(得分:0)

我只想在平面文件中写入给定年份的逗号分隔的make列表,并在进行db查询之前检查文件是否存在。您可以在文本文件中设置将来的时间戳,并检查是否已过期,如果这样,则创建一个覆盖旧文件的新缓存文件。

if(file_exists($_POST['year'].'.txt'))
{
 // the file exists
 $select_options = explode(",",file_get_contents($_POST['year'].'.txt'));
// loop through the array for your options
}
else
{
 // make your db query
}