摘要:文檔地址創(chuàng)建命令類生成文件編寫文件自定義命令的名稱自定義命令被執(zhí)行時,將會調(diào)用方法參數(shù)為空用戶關(guān)注關(guān)系更新到參數(shù)錯誤批量操作最后一個完成默認(rèn)參數(shù)可通過執(zhí)行來查看注冊一個命令
文檔地址
1.創(chuàng)建命令類
php artisan make:console user#生成文件user.php
2.編寫文件
argument("action"));
if (method_exists($this, $action)) {
$this->$action();
return false;
}
$this->error("action參數(shù)為空.");
}
/**
* 用戶關(guān)注關(guān)系更新到redis
* @return mixed #php artisan user -t redis -s 100 --usleep 0 attention
*/
protected function actionAttention()
{
if ($this->option("target") != "redis") {
$this->error("target 參數(shù)錯誤.");
return false;
}
$redis = RedisFacade::connection();
$usleep = $this->option("usleep");
$limit = $this->option("size");
$minId = 0;
while (true) {
$attention = Attention::where("id", ">", $minId)
->select("id", "user_id", "at_id", "created_at")
->orderBy("id")
->take($limit)
->get();
$selectCount = count($attention);
if (!$selectCount) {
break;
}
//批量操作
$redis->pipeline(function($pipe) use($attention) {
foreach ($attention as $v) {
$pipe->hSet("follows:list:" . $v["user_id"], $v["at_id"], strtotime($v["created_at"]));
$pipe->hSet("fans:list:" . $v["at_id"], $v["user_id"], strtotime($v["created_at"]));
}
});
if ($selectCount < $limit) {
break;
}
//最后一個id
$minId = $attention->last()->id;
unset($attention);
if ($usleep > 0) {
usleep($usleep);
}
}
$this->info("完成.");
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
["action", InputArgument::REQUIRED, "Action name, eg: sync"],
];
}
/**
* Get the console command options.
*默認(rèn)參數(shù)可通過執(zhí)行php artisan user -h 來查看
* @return array
*/
protected function getOptions()
{
return [
["target", "t", InputOption::VALUE_OPTIONAL, "Sync target", "redis"],
["size", "s", InputOption::VALUE_OPTIONAL, "Loop size", "100"],
["usleep", null, InputOption::VALUE_OPTIONAL, "Loop usleep", "0"],
];
}
}
3.注冊一個 Artisan 命令
vi app/Console/Kernel.php
protected $commands = [
"AppConsoleCommandsUser",
];
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/30298.html
摘要:初步嘗試既然最常見的注冊命令的方式是修改類中的,那么一般正常人都會從這邊開始下手。又要自己取出實例,又要自己調(diào)用方法,調(diào)用方法之前還有自己先把實例化這么繁瑣,肯定不是運行時添加命令的最佳實踐,所以我決定繼續(xù)尋找更優(yōu)解。 本文首發(fā)于我的博客,原文鏈接:https://blessing.studio/best-... 雖然 Laravel 官方文檔提供的添加 Artisan Command...
摘要:譯文原文鏈接在啟動計劃任務(wù)的事件的時候,的進(jìn)度管理器在對象上調(diào)用方法,表示該事件發(fā)生在內(nèi)。在方法里面定義每一個命令的互斥所以它是事件的表達(dá)式和命令字符串的組合。 譯文GitHub https://github.com/yuansir/diving-laravel-zh 原文鏈接 https://divinglaravel.com/task-scheduling/building-and...
摘要:擴(kuò)展 擴(kuò)展 https://github.com/Xethron/mi... https://github.com/orangehill... migrations-generator Generate Laravel Migrations from an existing database, including indexes and foreign keys! Upgradin...
摘要:應(yīng)用場景定時腳本任務(wù)需要在凌晨計算前一日的數(shù)據(jù)并匯總到統(tǒng)計表中。命令復(fù)雜的定時任務(wù)可以配合命令。命令按照命令行文檔,了解它的使用和配置。使用命令腳本名稱生成執(zhí)行文件,文件在中查看。 應(yīng)用場景: 定時腳本任務(wù)需要在凌晨計算前一日的數(shù)據(jù)并匯總到統(tǒng)計表中。 Artisan命令復(fù)雜的定時任務(wù)可以配合Artisan命令。 Artisan命令: 按照 Laravel Artisan命令行 文...
摘要:導(dǎo)語之前寫過使用的進(jìn)行定時任務(wù),實際上也可以執(zhí)行定時任務(wù)。需求是統(tǒng)計每日訪問的數(shù),雖然數(shù)據(jù)表中有數(shù)據(jù),為了演示,新建監(jiān)聽器統(tǒng)計。記錄這篇文章中介紹了實現(xiàn)了事件監(jiān)聽器,在此基礎(chǔ)上進(jìn)行擴(kuò)展。 導(dǎo)語 之前寫過使用 Linux 的進(jìn)行定時任務(wù),實際上 laravel 也可以執(zhí)行定時任務(wù)。需求是統(tǒng)計每日訪問的 IP 數(shù),雖然數(shù)據(jù)表中有數(shù)據(jù),為了演示,新建監(jiān)聽器統(tǒng)計。 記錄 IP 這篇文章中介紹了...
摘要:顯示幫助信息強制輸出禁用輸出 Laravel Framework version 5.1.3 (LTS) Usage: command [options] [arguments] Options: -h, --help 顯示幫助信息 -q, --quiet Do not output any message -V, --ve...
閱讀 1872·2023-04-25 23:43
閱讀 1004·2021-11-24 09:39
閱讀 782·2021-11-22 15:25
閱讀 1782·2021-11-22 12:08
閱讀 1167·2021-11-18 10:07
閱讀 2134·2021-09-23 11:22
閱讀 3432·2021-09-22 15:23
閱讀 2686·2021-09-13 10:32