laravel artisan command

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

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use App\Models\Attention;
use RedisFacade;

class User extends Command
{
    /**
     * The console command name.
     *自定义命令的 名称
     * @var string
     */
    protected $name = 'user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'User';

    /**
     * Execute the console command.
     *自定义命令被执行时,将会调用 fire 方法
     * @return mixed
     */
    public function fire()
    {
        $action = 'action' . ucfirst($this->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 = [
    'App\Console\Commands\User',
];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值