PHPackages                             bardoqi/php-hive-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. [Database &amp; ORM](/categories/database)
4. /
5. bardoqi/php-hive-queue

ActiveLibrary[Database &amp; ORM](/categories/database)

bardoqi/php-hive-queue
======================

$queue-&gt;push('I can be popped off after', '10 minutes');

0.12.2(5y ago)041MITPHPPHP ^5.4|^7.0

Since Dec 7Pushed 5y ago1 watchersCompare

[ Source](https://github.com/BardoQi/php-hive-queue)[ Packagist](https://packagist.org/packages/bardoqi/php-hive-queue)[ Docs](https://github.com/bardoqi/php-hive-queue)[ GitHub Sponsors](https://github.com/bardoqi)[ RSS](/packages/bardoqi-php-hive-queue/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)Dependencies (1)Versions (2)Used By (0)

Phive Queue
===========

[](#phive-queue)

[![Build Status](https://camo.githubusercontent.com/e19833250145256c406d46f1de160963a9a9f3c2f7ebd5ccba684319f842068b/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f626172646f71692f7068702d686976652d71756575652e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/bardoqi/php-hive-queue)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/d113ac5820f4a43f7c0e199923be8761b84236a35aa029d7da87e8049c034d1d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f626172646f71692f7068702d686976652d71756575652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/bardoqi/php-hive-queue/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/e1afd615b1ffc730c5487bfc0aa6f9841bfa2741b75b9bd197a733d6de24bc48/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f626172646f71692f7068702d686976652d71756575652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/bardoqi/php-hive-queue/?branch=master)

Phive Queue is a time-based scheduling queue with multiple backend support.

Note: This package is forked from

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage example](#usage-example)
- [Queues](#queues)
    - [MongoQueue](#mongoqueue)
    - [RedisQueue](#redisqueue)
    - [TarantoolQueue](#tarantoolqueue)
    - [PheanstalkQueue](#pheanstalkqueue)
    - [GenericPdoQueue](#genericpdoqueue)
    - [SqlitePdoQueue](#sqlitepdoqueue)
    - [SysVQueue](#sysvqueue)
    - [InMemoryQueue](#inmemoryqueue)
- [Item types](#item-types)
- [Exceptions](#exceptions)
- [Tests](#tests)
    - [Performance](#performance)
    - [Concurrency](#concurrency)
- [License](#license)

Installation
------------

[](#installation)

The recommended way to install Phive Queue is through [Composer](http://getcomposer.org):

```
$ composer require bardoqi/php-hive-queue
```

Usage example
-------------

[](#usage-example)

```
use Phive\Queue\InMemoryQueue;
use Phive\Queue\NoItemAvailableException;

$queue = new InMemoryQueue();

$queue->push('item1');
$queue->push('item2', new DateTime());
$queue->push('item3', time());
$queue->push('item4', '+5 seconds');
$queue->push('item5', 'next Monday');

// get the queue size
$count = $queue->count(); // 5

// pop items off the queue
// note that is not guaranteed that the items with the same scheduled time
// will be received in the same order in which they were added
$item123 = $queue->pop();
$item123 = $queue->pop();
$item123 = $queue->pop();

try {
    $item4 = $queue->pop();
} catch (NoItemAvailableException $e) {
    // item4 is not yet available
}

sleep(5);
$item4 = $queue->pop();

// clear the queue (will remove 'item5')
$queue->clear();
```

Queues
------

[](#queues)

Currently, there are the following queues available:

- [MongoQueue](#mongoqueue)
- [RedisQueue](#redisqueue)
- [TarantoolQueue](#tarantoolqueue)
- [PheanstalkQueue](#pheanstalkqueue)
- [GenericPdoQueue](#genericpdoqueue)
- [SqlitePdoQueue](#sqlitepdoqueue)
- [SysVQueue](#sysvqueue)
- [InMemoryQueue](#inmemoryqueue)

#### MongoQueue

[](#mongoqueue)

The `MongoQueue` requires the [Mongo PECL](http://pecl.php.net/package/mongo) extension *(v1.3.0 or higher)*.

*Tip:* Before making use of the queue, it's highly recommended to create an index on a `eta` field:

```
$ mongo my_db --eval 'db.my_coll.ensureIndex({ eta: 1 })'
```

##### Constructor

[](#constructor)

```
public MongoQueue::__construct(MongoClient $mongoClient, string $dbName, string $collName)
```

Parameters:

> **mongoClient** The MongoClient instance
> **dbName** The database name
> **collName** The collection name

##### Example

[](#example)

```
use Phive\Queue\MongoQueue;

$client = new MongoClient();
$queue = new MongoQueue($client, 'my_db', 'my_coll');
```

#### RedisQueue

[](#redisqueue)

For the `RedisQueue` you have to install the [Redis PECL](http://pecl.php.net/package/redis) extension *(v2.2.3 or higher)*.

##### Constructor

[](#constructor-1)

```
public RedisQueue::__construct(Redis $redis)
```

Parameters:

> **redis** The Redis instance

##### Example

[](#example-1)

```
use Phive\Queue\RedisQueue;

$redis = new Redis();
$redis->connect('127.0.0.1');
$redis->setOption(Redis::OPT_PREFIX, 'my_prefix:');

// Since the Redis client v2.2.5 the RedisQueue has the ability to utilize serialization:
// $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);

$queue = new RedisQueue($redis);
```

#### TarantoolQueue

[](#tarantoolqueue)

To use the `TarantoolQueue` you have to install the [Tarantool PECL](https://github.com/tarantool/tarantool-php)extension and a [Lua script](https://github.com/tarantool/queue) for managing queues.

##### Constructor

[](#constructor-2)

```
public TarantoolQueue::__construct(Tarantool $tarantool, string $tubeName [, int $space = null ])
```

Parameters:

> **tarantool** The Tarantool instance
> **tubeName** The tube name
> **space** *Optional*. The space number. Default to 0

##### Example

[](#example-2)

```
use Phive\Queue\TarantoolQueue;

$tarantool = new Tarantool('127.0.0.1', 33020);
$queue = new TarantoolQueue($tarantool, 'my_tube');
```

#### PheanstalkQueue

[](#pheanstalkqueue)

The `PheanstalkQueue` requires the [Pheanstalk](https://github.com/pda/pheanstalk)library ([Beanstalk](http://kr.github.io/beanstalkd) client) to be installed:

```
$ composer require pda/pheanstalk:~3.0
```

##### Constructor

[](#constructor-3)

```
public PheanstalkQueue::__construct(Pheanstalk\PheanstalkInterface $pheanstalk, string $tubeName)
```

Parameters:

> **pheanstalk** The Pheanstalk\\PheanstalkInterface instance
> **tubeName** The tube name

##### Example

[](#example-3)

```
use Pheanstalk\Pheanstalk;
use Phive\Queue\PheanstalkQueue;

$pheanstalk = new Pheanstalk('127.0.0.1');
$queue = new PheanstalkQueue($pheanstalk, 'my_tube');
```

#### GenericPdoQueue

[](#genericpdoqueue)

The `GenericPdoQueue` is intended for PDO drivers whose databases support stored procedures/functions (in fact all drivers except SQLite).

The `GenericPdoQueue` requires [PDO](http://php.net/pdo) and a [PDO driver](http://php.net/manual/en/pdo.drivers.php)for a particular database be installed. On top of that PDO error mode must be set to throw exceptions (`PDO::ERRMODE_EXCEPTION`).

SQL files to create the table and the stored routine can be found in the [res](res) directory.

##### Constructor

[](#constructor-4)

```
public GenericPdoQueue::__construct(PDO $pdo, string $tableName [, string $routineName = null ] )
```

Parameters:

> **pdo** The PDO instance
> **tableName** The table name
> **routineName** *Optional*. The routine name. Default to **tableName**\_pop

##### Example

[](#example-4)

```
use Phive\Queue\Pdo\GenericPdoQueue;

$pdo = new PDO('pgsql:host=127.0.0.1;port=5432;dbname=my_db', 'db_user', 'db_pass');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$queue = new GenericPdoQueue($pdo, 'my_table', 'my_routine');
```

#### SqlitePdoQueue

[](#sqlitepdoqueue)

The `SqlitePdoQueue` requires [PDO](http://php.net/pdo) and [SQLite PDO driver](http://php.net/manual/en/ref.pdo-sqlite.php). On top of that PDO error mode must be set to throw exceptions (`PDO::ERRMODE_EXCEPTION`).

SQL file to create the table can be found in the [res/sqlite](res/sqlite) directory.

*Tip:* For performance reasons it's highly recommended to activate [WAL mode](http://www.sqlite.org/wal.html):

```
$pdo->exec('PRAGMA journal_mode=WAL');
```

##### Constructor

[](#constructor-5)

```
public SqlitePdoQueue::__construct(PDO $pdo, string $tableName)
```

Parameters:

> **pdo** The PDO instance
> **tableName** The table name

##### Example

[](#example-5)

```
use Phive\Queue\Pdo\SqlitePdoQueue;

$pdo = new PDO('sqlite:/opt/databases/my_db.sq3');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('PRAGMA journal_mode=WAL');

$queue = new SqlitePdoQueue($pdo, 'my_table');
```

#### SysVQueue

[](#sysvqueue)

The `SysVQueue` requires PHP to be compiled with the option **--enable-sysvmsg**.

##### Constructor

[](#constructor-6)

```
public SysVQueue::__construct(int $key [, bool $serialize = null [, int $perms = null ]] )
```

Parameters:

> **key** The message queue numeric ID
> **serialize** *Optional*. Whether to serialize an item or not. Default to false
> **perms** *Optional*. The queue permissions. Default to 0666

##### Example

[](#example-6)

```
use Phive\Queue\SysVQueue;

$queue = new SysVQueue(123456);
```

#### InMemoryQueue

[](#inmemoryqueue)

The `InMemoryQueue` can be useful in cases where the persistence is not needed. It exists only in RAM and therefore operates faster than other queues.

##### Constructor

[](#constructor-7)

```
public InMemoryQueue::__construct()
```

##### Example

[](#example-7)

```
use Phive\Queue\InMemoryQueue;

$queue = new InMemoryQueue();
```

Item types
----------

[](#item-types)

The following table details the various item types supported across queues.

Queue/Typestringbinary stringnullboolintfloatarrayobject[MongoQueue](#mongoqueue)✓✓✓✓✓✓[RedisQueue](#redisqueue)✓✓✓✓✓✓✓\*✓\*[TarantoolQueue](#tarantoolqueue)✓✓✓✓✓✓[PheanstalkQueue](#pheanstalkqueue)✓✓✓✓✓✓[GenericPdoQueue](#genericpdoqueue)✓✓✓✓✓[SqlitePdoQueue](#sqlitepdoqueue)✓✓✓✓✓[SysVQueue](#sysvqueue)✓✓✓\*✓✓✓✓\*✓\*[InMemoryQueue](#inmemoryqueue)✓✓✓✓✓✓✓✓> ✓\* — supported if the serializer is enabled.

To bypass the limitation of unsupported types for the particular queue you could convert an item to a non-binary string before pushing it and then back after popping. The library ships with the `TypeSafeQueue` decorator which does that for you:

```
use Phive\Queue\GenericPdoQueue;
use Phive\Queue\TypeSafeQueue;

$queue = new GenericPdoQueue(...);
$queue = new TypeSafeQueue($queue);

$queue->push(['foo' => 'bar']);
$array = $queue->pop(); // ['foo' => 'bar'];
```

Exceptions
----------

[](#exceptions)

Every queue method declared in the [Queue](src/Queue.php) interface will throw an exception if a run-time error occurs at the time the method is called.

For example, in the code below, the `push()` call will fail with a `MongoConnectionException`exception in a case a remote server unreachable:

```
use Phive\Queue\MongoQueue;

$queue = new MongoQueue(...);

// mongodb server goes down here

$queue->push('item'); // throws MongoConnectionException
```

But sometimes you may want to catch exceptions coming from a queue regardless of the underlying driver. To do this just wrap your queue object with the `ExceptionalQueue` decorator:

```
use Phive\Queue\ExceptionalQueue;
use Phive\Queue\MongoQueue;

$queue = new MongoQueue(...);
$queue = new ExceptionalQueue($queue);

// mongodb server goes down here

$queue->push('item'); // throws Phive\Queue\QueueException
```

And then, to catch queue level exceptions use the `QueueException` class:

```
use Phive\Queue\QueueException;

...

try {
    do_something_with_a_queue();
} catch (QueueException $e) {
    // handle queue exception
} catch (\Exception $e) {
    // handle base exception
}
```

Tests
-----

[](#tests)

Phive Queue uses [PHPUnit](http://phpunit.de) for unit and integration testing. In order to run the tests, you'll first need to install the library dependencies using composer:

```
$ composer install
```

You can then run the tests:

```
$ phpunit
```

You may also wish to specify your own default values of some tests (db names, passwords, queue sizes, etc.). You can do it by setting environment variables from the command line:

```
$ export PHIVE_PDO_PGSQL_PASSWORD="pgsql_password"
$ export PHIVE_PDO_MYSQL_PASSWORD="mysql_password"
$ phpunit
```

You may also create your own `phpunit.xml` file by copying the [phpunit.xml.dist](phpunit.xml.dist)file and customize to your needs.

#### Performance

[](#performance)

To check the performance of queues run:

```
$ phpunit --group performance
```

This test inserts a number of items (1000 by default) into a queue, and then retrieves them back. It measures the average time for `push` and `pop` operations and outputs the resulting stats, e.g.:

```
RedisQueue::push()
   Total operations:      1000
   Operations per second: 14031.762 [#/sec]
   Time per operation:    71.267 [ms]
   Time taken for test:   0.071 [sec]

RedisQueue::pop()
   Total operations:      1000
   Operations per second: 16869.390 [#/sec]
   Time per operation:    59.279 [ms]
   Time taken for test:   0.059 [sec]
.
RedisQueue::push() (delayed)
   Total operations:      1000
   Operations per second: 15106.226 [#/sec]
   Time per operation:    66.198 [ms]
   Time taken for test:   0.066 [sec]

RedisQueue::pop() (delayed)
   Total operations:      1000
   Operations per second: 14096.416 [#/sec]
   Time per operation:    70.940 [ms]
   Time taken for test:   0.071 [sec]
```

You may also change the number of items involved in the test by changing the `PHIVE_PERF_QUEUE_SIZE`value in your `phpunit.xml` file or by setting the environment variable from the command line:

```
$ PHIVE_PERF_QUEUE_SIZE=5000 phpunit --group performance
```

#### Concurrency

[](#concurrency)

In order to check the concurrency you'll have to install the [Gearman](http://gearman.org) server and the [German PECL](http://pecl.php.net/package/gearman) extension. Once the server has been installed and started, create a number of processes (workers) by running:

```
$ php tests/worker.php
```

Then run the tests:

```
$ phpunit --group concurrency
```

This test inserts a number of items (100 by default) into a queue, and then each worker tries to retrieve them in parallel.

You may also change the number of items involved in the test by changing the `PHIVE_CONCUR_QUEUE_SIZE`value in your `phpunit.xml` file or by setting the environment variable from the command line:

```
$ PHIVE_CONCUR_QUEUE_SIZE=500 phpunit --group concurrency
```

License
-------

[](#license)

Phive Queue is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1988d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bfb2d0271845406a6b777d824993d97eec9f249b61ffa43b86994698b988e30?d=identicon)[bardoqi](/maintainers/bardoqi)

---

Top Contributors

[![BardoQi](https://avatars.githubusercontent.com/u/409367?v=4)](https://github.com/BardoQi "BardoQi (2 commits)")

---

Tags

mysqlsqlitepostgrespdoredisqueueschedulemongodbbeanstalkprioritytarantooldelayedsysv

### Embed Badge

![Health badge](/badges/bardoqi-php-hive-queue/health.svg)

```
[![Health](https://phpackages.com/badges/bardoqi-php-hive-queue/health.svg)](https://phpackages.com/packages/bardoqi-php-hive-queue)
```

###  Alternatives

[rybakit/phive-queue

$queue-&gt;push('I can be popped off after', '10 minutes');

16441.5k1](/packages/rybakit-phive-queue)[apix/cache

A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache taggging and indexing to Redis, Memcached, PDO/SQL, APC and other adapters.

114542.8k6](/packages/apix-cache)[aura/sql

A PDO extension that provides lazy connections, array quoting, query profiling, value binding, and convenience methods for common fetch styles. Because it extends PDO, existing code that uses PDO can use this without any changes to the existing code.

5632.5M43](/packages/aura-sql)[mmucklo/queue-bundle

Symfony2/3/4/5 Queue Bundle (for background jobs) supporting Mongo (Doctrine ODM), Mysql (and any Doctrine ORM), RabbitMQ, Beanstalkd, Redis, and ... {write your own}

120839.8k](/packages/mmucklo-queue-bundle)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[aura/sqlschema

Provides facilities to read table names and table columns from a database using PDO.

41234.1k4](/packages/aura-sqlschema)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
