求一个php的缓存程序
<?php$s=file_get_contents("http://www.chaoliuxi.com/category.php?id=1");preg_match_al...
<?php
$s=file_get_contents("http://www.chaoliuxi.com/category.php?id=1");
preg_match_all('|<div class="bbpic">\s*<a><img src="([^"]*)"/></a>\s*</div>\s*<div class="bbinfo">\s*<a>([^<]*)</a>\s*</div>|', $s, $reg);
//提取图片网址和图片标题并输出
foreach ($reg[1] as $i=>$k1){
$k2=$reg[2][$i];
echo $k1;
echo $k2;
}
?>
采集的页面,现在匹配也没问题,请问如果把匹配提取的数组生成缓存文件呢,要求有个缓存开关和缓存时间,以及判断是否有缓存文件,如果有缓存文件就调用缓存文件,没有缓存文件就采集网站,谢谢了 展开
$s=file_get_contents("http://www.chaoliuxi.com/category.php?id=1");
preg_match_all('|<div class="bbpic">\s*<a><img src="([^"]*)"/></a>\s*</div>\s*<div class="bbinfo">\s*<a>([^<]*)</a>\s*</div>|', $s, $reg);
//提取图片网址和图片标题并输出
foreach ($reg[1] as $i=>$k1){
$k2=$reg[2][$i];
echo $k1;
echo $k2;
}
?>
采集的页面,现在匹配也没问题,请问如果把匹配提取的数组生成缓存文件呢,要求有个缓存开关和缓存时间,以及判断是否有缓存文件,如果有缓存文件就调用缓存文件,没有缓存文件就采集网站,谢谢了 展开
1个回答
展开全部
<?php
define('PHPMPS_ROOT', dirname(__FILE__));
if (!function_exists('file_get_contents'))
{
function file_get_contents($file)
{
if (($fp = @fopen($file, 'rb')) === false)
{
return false;
}
else
{
$fsize = @filesize($file);
if ($fsize)
{
$contents = fread($fp, $fsize);
}
else
{
$contents = '';
}
fclose($fp);
return $contents;
}
}
}
if (!function_exists('file_put_contents'))
{
define('FILE_APPEND', 'FILE_APPEND');
function file_put_contents($file, $data, $flags = '')
{
$contents = (is_array($data)) ? implode('', $data) : $data;
if ($flags == 'FILE_APPEND')
{
$mode = 'ab+';
}
else
{
$mode = 'wb';
}
if (($fp = @fopen($file, $mode)) === false)
{
return false;
}
else
{
$bytes = fwrite($fp, $contents);
fclose($fp);
return $bytes;
}
}
}
function read_cache($filename)
{
$result = array();
if (!empty($result[$filename]))
{
return $result[$filename];
}
$filepath = PHPMPS_ROOT . '/data/' . $filename . '.php';
if (file_exists($filepath))
{
include_once($filepath);
$result[$filename] = $data;
return $result[$filename];
}
else
{
return false;
}
}
function write_cache($filename, $val)
{
$filepath = PHPMPS_ROOT . '/data/' . $filename . '.php';
$content = "<?php\r\n";
$content .= "\$data = " . var_export($val, true) . ";\r\n";
$content .= "?>";
echo $content;
file_put_contents($filepath, $content, LOCK_EX);
}
$content = array();
foreach ($cat_content[1] as $i=> $k1){
$content[]=array('0' => $k1,
'1'=> $cat_content[2][$i],
'2'=> $cat_content[3][$i]);
}
write_cache('t_123',$content);
echo 'suc';
?>
define('PHPMPS_ROOT', dirname(__FILE__));
if (!function_exists('file_get_contents'))
{
function file_get_contents($file)
{
if (($fp = @fopen($file, 'rb')) === false)
{
return false;
}
else
{
$fsize = @filesize($file);
if ($fsize)
{
$contents = fread($fp, $fsize);
}
else
{
$contents = '';
}
fclose($fp);
return $contents;
}
}
}
if (!function_exists('file_put_contents'))
{
define('FILE_APPEND', 'FILE_APPEND');
function file_put_contents($file, $data, $flags = '')
{
$contents = (is_array($data)) ? implode('', $data) : $data;
if ($flags == 'FILE_APPEND')
{
$mode = 'ab+';
}
else
{
$mode = 'wb';
}
if (($fp = @fopen($file, $mode)) === false)
{
return false;
}
else
{
$bytes = fwrite($fp, $contents);
fclose($fp);
return $bytes;
}
}
}
function read_cache($filename)
{
$result = array();
if (!empty($result[$filename]))
{
return $result[$filename];
}
$filepath = PHPMPS_ROOT . '/data/' . $filename . '.php';
if (file_exists($filepath))
{
include_once($filepath);
$result[$filename] = $data;
return $result[$filename];
}
else
{
return false;
}
}
function write_cache($filename, $val)
{
$filepath = PHPMPS_ROOT . '/data/' . $filename . '.php';
$content = "<?php\r\n";
$content .= "\$data = " . var_export($val, true) . ";\r\n";
$content .= "?>";
echo $content;
file_put_contents($filepath, $content, LOCK_EX);
}
$content = array();
foreach ($cat_content[1] as $i=> $k1){
$content[]=array('0' => $k1,
'1'=> $cat_content[2][$i],
'2'=> $cat_content[3][$i]);
}
write_cache('t_123',$content);
echo 'suc';
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询