已掉线,重新登录

首页 > 绿虎论坛 > 杂类 > 超级灌水 (发帖)

标题: 百度云搜索的类,可以设置清除缓存的规则

作者: @Ta

时间: 2014-07-30

点击: 676

<?php
class pan{
private $cacheFold='cache/';//缓存目录,需要提前手动建立
private $cacheTime=600;//缓存时间,单位秒,为0时不使用缓存
private $cacheClearTime=1200;//隔多久检查并清理缓存超时的缓存文件,单位秒,为0时不检查
private $url='http://m.sm.cn/s?page={$p}&q={$k}';//搜索引擎接口
public  $from='';//分辨来自缓存还是实时获取


function getResult($k,$p)//实例返回结果的方法
{
$this->delAllOutCacheFile();
$k=trim($k);
if($k=='')
{
return 0;
}
$p=intval($p);
$p=($p<1)?1:$p;
$p=($p>100)?100:$p;//此处视搜索引擎的限制而修改
$result=0;
//$cache=$this->existCache($k,$p);
if(($this->cacheTime)!=0 && ($cache=$this->existCache($k,$p))!='1')
{
$now=time();
if($this->outCacheTime($cache,$now))
{
unlink($cache);
$result=$this->getFromUrl($k,$p);
$this->from='缓存超时而实时获取';
}
else{
$result=$this->getFromCache($cache);
$this->from='来自缓存';
}
}
else{
$result=$this->getFromUrl($k,$p);
$this->from='实时获取';
}
return $result;
}


function existCache($k,$p)
{
$k=md5($k);
$str="{$this->cacheFold}{$k}#{$p}#*.txt";
$r=glob($str);
//print_r($r);
$n=count($r);
//echo $n;
if($n && $r[0]!='')
{
return $r[0];
}
else{
return '1';
}
}

function outCacheTime($cache,$now)//判断是否超时
{
//$now=time();
$tmp=basename($cache);
$tmp=explode('#',$tmp);
$tmp=explode('.',$tmp[2]);
$tmp=$tmp[0];//文件名中的时间戳
if(($now-$tmp)>$this->cacheTime)
{
return 1;
}
else{
return 0;
}
}


function getFromUrl($k,$p)
{
$con=$this->getPage($k,$p);
$result=$this->jiexi($con);
if($this->cacheTime!=0 && $con!='')
{
$this->saveCache($k,$p,$result);
}
return $result;
}


function saveCache($k,$p,$result)
{
$now=time();
$k=md5($k);
$con='';
$str="{$this->cacheFold}{$k}#{$p}#{$now}.txt";
for($i=0;isset($result[$i]);++$i)
{
$con="$con".urlencode($result[$i][0]).'#'.urlencode($result[$i][1]).'#'.urlencode($result[$i][2])."\n";
}
file_put_contents($str,$con);
}


function getPage($k,$p)//获取页面
{
$k="{$k}  site:pan.baidu.com";
$k=urlencode($k);
$url=str_replace('{$p}',$p,$this->url);
$url=str_replace('{$k}',$k,$url);
//$url="http://m.sm.cn/s?q={$k}&page={$p}";
return @file_get_contents($url);
}


function jiexi($c)//页面内容解析为数组
{
preg_match_all("~<div\ class=\"article.*</div></div>~Uis",$c,$c);
$c=$c[0];
$result=array();
for($i=0;isset($c[$i]);++$i)
{
$c2=$c[$i];
preg_match("~<a\ href=\"(.*)\">.*</a>~Uis",$c2,$c3);
$con[0]=str_replace('&amp;','&',$c3[1]);
$con[1]=strip_tags($c3[0]);
preg_match("~<p>.*</p>~Uis",$c2,$c3);
$con[2]=strip_tags($c3[0]);
$result[$i]=$con;
}
return $result;
}


function getFromCache($cache)
{
$result=array();
$con=file($cache);
for($i=0;isset($con[$i]);++$i)
{
$c=trim($con[$i]);
$c=explode('#',$c);
$c[0]=urldecode($c[0]);
$c[1]=urldecode($c[1]);
$c[2]=urldecode($c[2]);
$result[$i]=$c;
}
return $result;
}


function delAllOutCacheFile()//按照属性中定义的规则清理缓存文件,在getResult方法中调用
{
if ($this->cacheClearTime>0)
{
$lastFileUrl="{$this->cacheFold}lasttime.time";
$lastClearTime=@file_get_contents($lastFileUrl);
$lastClearTime=($lastClearTime>0)?$lastClearTime:0;
$now=time();
if(($now-$lastClearTime)>$this->cacheClearTime)
{
$str="{$this->cacheFold}*#*#*.txt";
$r=glob($str);
$n=count($r);
$j=0;//删除的缓存文件计数
for($i=0;$i<$n;++$i)
{
if($this->outCacheTime($r[$i],$now))
{
unlink($r[$i]);
++$j;
}
}
file_put_contents($lastFileUrl,$now);
return $j;

}
else{
return 0;//未到清除缓存的时间
}
}
else{
return 0;//属性中的规则为不清除缓存
}
}


function getCacheNum()//获取已缓存的文件数量
{
$tmp=glob("{$this->cacheFold}*.txt");
$n=count($tmp);
return $n;
}
}
//类结束

?>


[隐藏样式|查看源码]


『回复列表(2|隐藏机器人聊天)』

1. 顶一个,大神的类很给力!
(/@Ta/2014-07-30 11:39//)

2. 顶
(/@Ta/2014-07-30 13:38//)

回复需要登录

9月9日 03:43 星期二

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1