资讯专栏INFORMATION COLUMN

laravel artisan command

reclay / 1764人阅读

摘要:文档地址创建命令类生成文件编写文件自定义命令的名称自定义命令被执行时,将会调用方法参数为空用户关注关系更新到参数错误批量操作最后一个完成默认参数可通过执行来查看注册一个命令

文档地址
1.创建命令类
php artisan make:console user#生成文件user.php
2.编写文件

argument("action"));
        if (method_exists($this, $action)) {
            $this->$action();
            return false;
        }
        $this->error("action参数为空.");
    }
    /**
     * 用户关注关系更新到redis
     * @return mixed #php artisan user -t redis -s 100 --usleep 0 attention
     */
    protected function actionAttention()
    {
        if ($this->option("target") != "redis") {
            $this->error("target 参数错误.");
            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.
     *默认参数可通过执行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",
];

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/30298.html

相关文章

  • Laravel 动态添加 Artisan 命令的最佳实践

    摘要:初步尝试既然最常见的注册命令的方式是修改类中的,那么一般正常人都会从这边开始下手。又要自己取出实例,又要自己调用方法,调用方法之前还有自己先把实例化这么繁琐,肯定不是运行时添加命令的最佳实践,所以我决定继续寻找更优解。 本文首发于我的博客,原文链接:https://blessing.studio/best-... 虽然 Laravel 官方文档提供的添加 Artisan Command...

    ninefive 评论0 收藏0
  • 剖析 Laravel 计划任务--创建和运行系统命令

    摘要:译文原文链接在启动计划任务的事件的时候,的进度管理器在对象上调用方法,表示该事件发生在内。在方法里面定义每一个命令的互斥所以它是事件的表达式和命令字符串的组合。 译文GitHub https://github.com/yuansir/diving-laravel-zh 原文链接 https://divinglaravel.com/task-scheduling/building-and...

    luodongseu 评论0 收藏0
  • Laravel从已有数据库表生成对应的migration和seed文件

    摘要:扩展 扩展 https://github.com/Xethron/mi... https://github.com/orangehill... migrations-generator Generate Laravel Migrations from an existing database, including indexes and foreign keys! Upgradin...

    dack 评论0 收藏0
  • laravel定时任务和命令行

    摘要:应用场景定时脚本任务需要在凌晨计算前一日的数据并汇总到统计表中。命令复杂的定时任务可以配合命令。命令按照命令行文档,了解它的使用和配置。使用命令脚本名称生成执行文件,文件在中查看。 应用场景: 定时脚本任务需要在凌晨计算前一日的数据并汇总到统计表中。 Artisan命令复杂的定时任务可以配合Artisan命令。 Artisan命令: 按照 Laravel Artisan命令行 文...

    MartinDai 评论0 收藏0
  • laravel 任务调度实例

    摘要:导语之前写过使用的进行定时任务,实际上也可以执行定时任务。需求是统计每日访问的数,虽然数据表中有数据,为了演示,新建监听器统计。记录这篇文章中介绍了实现了事件监听器,在此基础上进行扩展。 导语 之前写过使用 Linux 的进行定时任务,实际上 laravel 也可以执行定时任务。需求是统计每日访问的 IP 数,虽然数据表中有数据,为了演示,新建监听器统计。 记录 IP 这篇文章中介绍了...

    loonggg 评论0 收藏0
  • Laravel Artisan 命令

    摘要:显示帮助信息强制输出禁用输出 Laravel Framework version 5.1.3 (LTS) Usage: command [options] [arguments] Options: -h, --help 显示帮助信息 -q, --quiet Do not output any message -V, --ve...

    txgcwm 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<