PHPackages                             baagee/redis-tools - 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. baagee/redis-tools

ActiveLibrary[Caching](/categories/caching)

baagee/redis-tools
==================

Redis distributed lock and current limiter

v0.0.2(6y ago)022MITPHPPHP &gt;=7.1

Since Apr 19Pushed 6y ago1 watchersCompare

[ Source](https://github.com/baagee/redis-tools)[ Packagist](https://packagist.org/packages/baagee/redis-tools)[ RSS](/packages/baagee-redis-tools/feed)WikiDiscussions master Synced today

READMEChangelog (2)DependenciesVersions (3)Used By (0)

redis-tools
===========

[](#redis-tools)

Redis distributed lock and current limiter

结合redis实现的分布式锁和限流器,令牌桶

Redis分布式抢占锁用法
-------------

[](#redis分布式抢占锁用法)

```
include __DIR__ . '/../vendor/autoload.php';

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$lock = \BaAGee\RedisTools\PreemptiveLock::getInstance($redis);
//方式1
$getLock = $lock->lock("test", 3, 10, 10000);
if ($getLock) {
    echo "get lock" . PHP_EOL;
    $lock->unlock('test');
} else {
    echo 'not get lock' . PHP_EOL;
}

// 方式2
$execute = $lock->run(function () {
    echo "我获得锁啦，哈哈哈" . PHP_EOL;
}, "test2", 5, 20, 20000);
if ($execute) {
    echo "获得锁并且已经执行了" . PHP_EOL;
} else {
    echo "没获得锁也没执行" . PHP_EOL;
}
```

限速器用法
-----

[](#限速器用法)

```
include __DIR__ . '/../vendor/autoload.php';

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$sl = \BaAGee\RedisTools\SpeedLimit::getInstance($redis);
for ($i = 0; $i < 100; $i++) {
    echo str_repeat('#', 70) . PHP_EOL;
    // 十秒内最多允许7次操作
    //用法1
    $res = $sl->isAllow("user_id", 'replay', 10, 7);
    echo ($res ? 'replay允许' : 'replay不允许') . $i . PHP_EOL;

    // 十秒内最多允许7次操作
    //用法1
    $res = $sl->run(function () {
        echo '买了' . PHP_EOL;
    }, "user_id2", 'buy', 10, 7);

    echo ($res ? '已经买完了' : '不能买') . $i . PHP_EOL;
    usleep(1000000);
}
```

令牌桶
---

[](#令牌桶)

```
include __DIR__ . '/../vendor/autoload.php';

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$bucket = \BaAGee\RedisTools\TokenBucket::getInstance($redis);
//方式1
$action = 'eat';
//设置桶容量 设置添加令牌的速度（每毫秒添加几个）
$bucket->setCapacity(100)->setLeakingRate(1);
for ($i = 0; $i < 1000; $i++) {
    $res = $bucket->getToken($action);
    echo ($res ? '可以' : '不可以') . $action . PHP_EOL;
}
// 方式2

for ($i = 0; $i < 1000; $i++) {
    $res = $bucket->run(function () use ($action) {
        echo '小明' . $action . PHP_EOL;
    }, $action);
    echo ($res ? 'success' : 'failed') . PHP_EOL;
}
```

###  Health Score

20

—

LowBetter than 13% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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 ~72 days

Total

2

Last Release

2193d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22995500?v=4)[BaAGee](/maintainers/baagee)[@baagee](https://github.com/baagee)

---

Top Contributors

[![baagee](https://avatars.githubusercontent.com/u/22995500?v=4)](https://github.com/baagee "baagee (1 commits)")

---

Tags

redislimiterdistributed-lock

### Embed Badge

![Health badge](/badges/baagee-redis-tools/health.svg)

```
[![Health](https://phpackages.com/badges/baagee-redis-tools/health.svg)](https://phpackages.com/packages/baagee-redis-tools)
```

###  Alternatives

[predis/predis

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

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

A Redis bundle for Symfony

1.0k40.9M75](/packages/snc-redis-bundle)[clue/redis-protocol

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

5216.6M14](/packages/clue-redis-protocol)[cache/redis-adapter

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

584.1M27](/packages/cache-redis-adapter)[enqueue/redis

Message Queue Redis Transport

445.7M30](/packages/enqueue-redis)[kdyby/redis

Redis storage for Nette Framework

501.7M2](/packages/kdyby-redis)

PHPackages © 2026

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