Redis Queue a cikin Laravel: Gudanar da Queuing

A cikin Laravel, Redis Queue kayan aiki ne mai ƙarfi da ake amfani da shi don ɗaukar ayyuka masu ɗaukar dogon lokaci da ɗaukar lokaci ba tare da jiran kammala su ba. Ta amfani da Redis Queue, zaku iya ba da jerin ayyuka kamar aika imel, sarrafa ayyukan baya, ko samar da rahotanni, da aiwatar da su a daidaita, haɓaka aikin aikace-aikacen da haɓaka ƙwarewar mai amfani.

Matakai na asali don amfani Redis Queue a ciki Laravel

Sanya Redis

Da farko, kuna buƙatar shigarwa kuma saita Redis cikin Laravel. Tabbatar cewa kun shigar da Redis kunshin ta hanyar Mawaƙi kuma saita Redis sigogin haɗi a cikin .env fayil ɗin.

CACHE_DRIVER=redis  
REDIS_HOST=127.0.0.1  
REDIS_PASSWORD=null  
REDIS_PORT=6379  

Bayyana Ayyuka

Na gaba, kuna buƙatar ayyana ayyukan da kuke son sanya a cikin jerin gwano. Za a gudanar da waɗannan ayyukan ba tare da ɓata lokaci ba kuma ba tare da babban aiki na aikace-aikacen ba.

// Example defining a job to send an email  
namespace App\Jobs;  
  
use Illuminate\Bus\Queueable;  
use Illuminate\Contracts\Queue\ShouldQueue;  
use Illuminate\Foundation\Bus\Dispatchable;  
use Illuminate\Queue\InteractsWithQueue;  
use Illuminate\Queue\SerializesModels;  
use Illuminate\Support\Facades\Mail;  
  
class SendEmailJob implements ShouldQueue  
{  
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;  
  
    protected $user;  
  
    public function __construct($user)  
    {  
        $this->user = $user;  
    }  
  
    public function handle()  
    {  
        // Handle sending an email to the user  
        Mail::to($this->user->email)->send(new WelcomeEmail());  
    }  
}  

Sanya Ayyuka a cikin jerin gwano

Lokacin da kake son yin aiki, kawai ka sanya shi cikin layi ta amfani da ayyuka dispatch ko dispatchNow ayyuka:

use App\Jobs\SendEmailJob;  
use Illuminate\Support\Facades\Queue;  
  
// Put the job into the queue and perform asynchronously  
Queue::push(new SendEmailJob($user));  
  
// Put the job into the queue and perform synchronously(without waiting)  
Queue::push(new SendEmailJob($user))->dispatchNow();  

Tsara Ayyuka daga Queue

Bayan an sanya aikin a cikin jerin gwano, kuna buƙatar saita Worker don aiwatar da ayyukan a cikin jerin gwano. Laravel ya zo tare da wani artisan command don gudanar da worker:

php artisan queue:work

Za worker a ci gaba da saurare da aiwatar da ayyukan a cikin jerin gwano. Kuna iya saita worker don sarrafa adadin ayyuka da lokacin jira tsakanin zagayen sarrafawa.

Sarrafa Ayyuka a cikin jerin gwano

Laravel yana ba da tsarin gudanarwa inda za ku iya saka idanu da sarrafa ayyukan da ke cikin jerin gwano. Kuna iya duba adadin ayyukan da ke jiran aiki, lokacin sarrafawa, har ma da sake gwada ayyukan da suka gaza.

 

Ƙarshe Amfani da Redis Queue ciki Laravel hanya ce mai inganci don gudanar da ayyuka masu tsayi ba tare da tarwatsa babban sarrafa aikace-aikacen ba. Ta amfani da Redis Queue, zaku iya inganta aikin aikace-aikacen da haɓaka ƙwarewar mai amfani.