PHPackages                             kalider/php-simple-queue - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kalider/php-simple-queue

ActiveLibrary

kalider/php-simple-queue
========================

php simple queue with small footprint

1.0.0(yesterday)02↑2900%PHPPHP &gt;=7.0

Since Aug 28Pushed yesterdayCompare

[ Source](https://github.com/kalider/php-simple-queue)[ Packagist](https://packagist.org/packages/kalider/php-simple-queue)[ RSS](/packages/kalider-php-simple-queue/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (4)Versions (2)Used By (0)

PHP Simple Queue
================

[](#php-simple-queue)

A lightweight, modular queue library for PHP 7.0+ implementing the **Adapter Pattern** and **Factory Pattern**. Supports **MySQL** (PDO) and **Redis** (`phpredis`), featuring exponential backoff, procedural job execution, and PSR-3 logging (Monolog).

---

🏗️ Architecture &amp; Design Patterns
-------------------------------------

[](#️-architecture--design-patterns)

- **Adapter Pattern**:
    - `PhpSimpleQueue\Queue`: The client interface/wrapper implementing `QueueInterface`.
    - `PhpSimpleQueue\Adapters\QueueAdapterInterface`: The common contract for queue drivers.
    - `PhpSimpleQueue\Adapters\MysqlAdapter`: MySQL storage adapter using PDO with atomic locking tokens.
    - `PhpSimpleQueue\Adapters\RedisAdapter`: Redis storage adapter using `phpredis` with atomic Lua scripts.
- **Factory Pattern**:
    - `PhpSimpleQueue\Factories\ConnectionFactory`: Assembles and returns database/cache connection instances (`PDO` or `Redis`).
    - `PhpSimpleQueue\Factories\QueueFactory`: Instantiates configured `Queue` instances with the corresponding adapter and connection.

---

🚀 Features
----------

[](#-features)

- **PHP 7.0+ Compatible**: Works on PHP 7.0 through PHP 8.x.
- **Dual Storage Adapters**:
    - **MySQL (PDO)**: Safe concurrency using atomic lock tokens (`reserved_by` &amp; `reserved_at`).
    - **Redis (`phpredis`)**: Atomic pop using Lua scripts and Sorted Sets (`ZSET`) for delayed processing.
- **Delayed Jobs**: Built-in support for delaying job execution by $N$ seconds.
- **Exponential Backoff**: Automatic retry with exponential delay (`pow(2, attempts) * 10` seconds) up to a max attempt threshold (default: 5).
- **Dead Letter / Failed Queue**: Permanently failed jobs are moved to `failed_jobs` table (MySQL) or `jobs:failed` list (Redis).
- **Procedural Job Dispatching**: Execute global functions or callables via `call_user_func_array()`.
- **PSR-3 Logging**: Fully compatible with PSR-3 loggers like Monolog.
- **Code Style (PHP-CS-Fixer)**: Pre-configured PSR-12 / PSR-2 code formatting and checking.

---

📦 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require kalider/php-simple-queue
```

---

🗄️ Database Setup (MySQL)
-------------------------

[](#️-database-setup-mysql)

If using the **MySQL adapter**, create the `jobs` and `failed_jobs` tables:

```
CREATE TABLE IF NOT EXISTS `jobs` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `payload` LONGTEXT NOT NULL,
    `available_at` INT UNSIGNED NOT NULL,
    `created_at` INT UNSIGNED NOT NULL,
    `reserved_at` INT UNSIGNED NULL DEFAULT NULL,
    `reserved_by` VARCHAR(255) NULL DEFAULT NULL,
    INDEX `idx_queue_reservation` (`reserved_at`, `available_at`),
    INDEX `idx_reserved_by` (`reserved_by`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `failed_jobs` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `payload` LONGTEXT NOT NULL,
    `failed_at` INT UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

---

🛠️ Usage
--------

[](#️-usage)

### 1. Define Your Job Function

[](#1-define-your-job-function)

```
function send_welcome_email(array $data)
{
    echo "Sending email to {$data['name']} ({$data['email']})...\n";
}
```

---

### 2. Creating Queue via `QueueFactory`

[](#2-creating-queue-via-queuefactory)

#### A. MySQL Queue (via Config Array)

[](#a-mysql-queue-via-config-array)

```
