文档地址
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',
];

苏生不惑
18.9k 声望1.9k 粉丝