需求:在項(xiàng)目中,需要采集文章并把文章中的圖片本地化/更新到oss中萝挤。如果按照以往的方式御毅,直來直往地采集并替換到圖片,耗時(shí)約20-50秒平斩。因此亚享,可以考慮實(shí)用lumen中的隊(duì)列任務(wù)Queue.
代碼片段
Controller 寫入數(shù)據(jù)庫后,再執(zhí)行隊(duì)列任務(wù)
if (is_array($return)){
TabHeadlineArticle::insert($return); //采集的文章入表
$res = ['code'=>200,'count'=>count($return),'msg'=>'采集成功']; //返回格式
dispatch(new PhotoJob($return));//執(zhí)行隊(duì)列任務(wù)
return $res;返回結(jié)果
}
隊(duì)列文件位置在/app/jobs
PhotoJobs
public function __construct($return)
{
$data = DB::table('tab_tool')->where('name','oss_storage')->get();
$data = json_decode($data,true);
$config = json_decode($data[0]['config'],true);;
$this->accessKeyId = $config['accesskeyid'];
$this->accessKeySecret = $config['accesskeysecr'];
$this->endpoint = $config['domain'];
$this->bucket= $config['bucket'];
$this->return = $return;
}
public function handle()
{
foreach($this->return as $key =>$val) {
$val['article_content'] = $this->replaceimg($val['article_content']);
DB::table('tab_headline_article')->where('md5',$val['md5'])->update(['article_content'=>$val['article_content']]);
}
}
最后绘面,在命令行中執(zhí)行
php artisan queue:work 即可
本地化的代碼參見之前文章就有.以此記錄