PHPackages                             alxmsl/primitives - 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. [Caching](/categories/caching)
4. /
5. alxmsl/primitives

ActiveLibrary[Caching](/categories/caching)

alxmsl/primitives
=================

Simple library with primitives: sets, queues etc.

v1.7.2(11y ago)0230WTFPLPHP

Since Jul 9Pushed 11y agoCompare

[ Source](https://github.com/alxmsl/Primitives)[ Packagist](https://packagist.org/packages/alxmsl/primitives)[ RSS](/packages/alxmsl-primitives/feed)WikiDiscussions master Synced 3d ago

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

Primitives
==========

[](#primitives)

Base classes for abstract primitives: sets, queues etc.

Set on Redis storage usage example
----------------------------------

[](#set-on-redis-storage-usage-example)

```
use alxmsl\Connection\Redis\RedisFactory;
use alxmsl\Primitives\SetFactory;

// Create redis connection
$Connection = RedisFactory::createRedisByConfig(array(
    'host' => 'localhost',
    'port' => 6379,
));

// Create set on the redis connection
$Set = SetFactory::createRedisSet('test', $Connection);

// Add set elements
$Set->add('obj_01');
$Set->add('obj_02');

// Check items existence
var_dump($Set->exists('obj_01'), $Set->exists('obj_03'));

```

Set on Postgres table usage example
-----------------------------------

[](#set-on-postgres-table-usage-example)

```
use alxmsl\Connection\Postgresql\Connection;
use alxmsl\Primitives\SetFactory;

// Create new postgres connection
$Connection = new Connection();
$Connection->setNeedBusyCheckup(false)
    ->setUserName('postgres')
    ->setPassword('postgres')
    ->setDatabase('postgres')
    ->setHost('localhost')
    ->setPort(5432);

// Create set instance
$Set = SetFactory::createPostgresSet('test', $Connection);

// Add items to set
$Set->add('obj_01');
$Set->add('obj_02');

// Check items existence
var_dump($Set->exists('obj_01'), $Set->exists('obj_03'));

```

Set`s iteration
---------------

[](#sets-iteration)

With set instance you can use an iterator support. Just enable enlisted mode

```
$Set->getProvider()->setEnlistedMode(true);

```

And use set`s iterator

```
foreach ($Set->getIterator() as $item) {
    var_dump($item);
}

```

Queue on Redis storage usage example
------------------------------------

[](#queue-on-redis-storage-usage-example)

```
use alxmsl\Connection\Redis\RedisFactory;
use alxmsl\Primitives\QueueFactory;

$Connection = RedisFactory::createRedisByConfig(array(
    'host' => 'localhost',
    'port' => 6379,
));

$Queue = QueueFactory::createRedisQueue('myqueue_01', $Connection);

$queue = array(1, 2, 4, 5, 6, 7, 8, 5);
foreach ($queue as $item) {
    $Queue->enqueue($item);
}

$result = array();
for (;;) {
    $item = $Queue->dequeue();
    if ($item !== false) {
        $result[] = $item;
    } else {
        break;
    }
}

$diff = array_diff($queue, $result);
var_dump(empty($diff));

```

Of course, queue instance implements[Iterator](php.net/manual/class.iterator.php) interface. Usage example:

```
foreach ($queue as $item) {
    $Queue->enqueue($item);
}

$result = array();
foreach ($Queue->getIterator() as $item) {
    $result[] = $item;
}

$diff = array_diff($queue, $result);
var_dump(empty($diff));

```

Queues pool usage example
-------------------------

[](#queues-pool-usage-example)

```
$Queue1 = QueueFactory::createRedisQueue('myqueue_pool_01', $Connection1);
$Queue2 = QueueFactory::createRedisQueue('myqueue_pool_02', $Connection2);

// Create new pool
$Pool = new Pool();
$Pool->addQueue($Queue1)
    ->addQueue($Queue2);

// Write to pool
$items = range(1, 5);
foreach ($items as $item) {
    $Pool->enqueue($item);
    printf("enqueued: %s\n", $item);
}

// Flush pool
while ($Item = $Pool->dequeue()) {
    printf("dequeued: %s\n", $Item);
}

```

Hierarchical cache usage example
--------------------------------

[](#hierarchical-cache-usage-example)

```
$RootCache = CacheFactory::createMemcachedCache('key_03', Cache::getClass(), $Connection);
$Level2Cache = CacheFactory::createMemcachedCache('key_03', Level2Cache::getClass(), $Connection);
$Level3Cache = CacheFactory::createMemcachedCache('key_03', Level3Cache::getClass(), $Connection);

// Leveled value write and read
$Level3Cache->set('some_level3_key', 5, Item::TYPE_NUMBER);
unset($Level3Cache);

$Level3Cache = CacheFactory::createMemcachedCache('key_03', Level3Cache::getClass(), $Connection);
var_dump($Level3Cache->get('some_level3_key')->getValue() == 5);

// Check cached level 3 value from level 2
$Level2Value = $Level2Cache->get('level3')->getValue();
var_dump($Level2Value->some_level3_key->getValue() == $Level3Cache->get('some_level3_key')->getValue());

// Check cached level 3 value from root level
$RootLevelValue = $RootCache->get('level2')->getValue();
var_dump($RootLevelValue->level3->getValue()->some_level3_key->getValue() == $Level3Cache->get('some_level3_key')->getValue());

// Set another value on level 2 and invalidate level 3
$Level2Cache->set('another_level2_key', 7);
$Level3Cache->invalidate();
unset($Level2Cache);

// Then check level 2 cached value
$Level2Cache = CacheFactory::createMemcachedCache('key_03', Level2Cache::getClass(), $Connection);
var_dump($Level2Cache->get('another_level2_key')->getValue() == 7);

// Check what level 3 is empty
try {
    $Level2Cache->get('level3');
    printf("error: level3 was not delete\n");
} catch (MissingException $Ex) {
    var_dump(true);
}

```

Semaphore usage example
-----------------------

[](#semaphore-usage-example)

```
// Create semaphore instance
$Semaphore = SemaphoreFactory::createRedisSemaphore($Connection, 'locker');

// Use semaphore
$result = $Semaphore->wait();
if ($result) {
    sleep(1);
    $Semaphore->signal();
} else {
    printf("semaphore are locked now\n");
}

```

License
-------

[](#license)

Copyright © 2014 Alexey Maslov This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity73

Established project with proven stability

 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

Every ~12 days

Recently: every ~5 days

Total

22

Last Release

4062d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/950057?v=4)[Alexey Maslov](/maintainers/alxmsl)[@alxmsl](https://github.com/alxmsl)

---

Top Contributors

[![alxmsl](https://avatars.githubusercontent.com/u/950057?v=4)](https://github.com/alxmsl "alxmsl (37 commits)")

---

Tags

postgresqlpostgresredisqueuessets

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alxmsl-primitives/health.svg)

```
[![Health](https://phpackages.com/badges/alxmsl-primitives/health.svg)](https://phpackages.com/packages/alxmsl-primitives)
```

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[clue/redis-protocol

A streaming Redis protocol (RESP) parser and serializer written in pure PHP.

5311.0M13](/packages/clue-redis-protocol)[cache/redis-adapter

A PSR-6 cache implementation using Redis (PhpRedis). This implementation supports tags

523.9M27](/packages/cache-redis-adapter)[moox/jobs

Manage Job Queues, Failed Jobs and Batches in Filament.

6421.8k](/packages/moox-jobs)[rtcamp/nginx-helper

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also provides cloudflare edge cache purging with Cache-Tags.

23817.0k1](/packages/rtcamp-nginx-helper)

PHPackages © 2026

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