PHPackages                             wangruyi/redis-kit - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. wangruyi/redis-kit

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wangruyi/redis-kit
==================

redis common toolset

00PHP

Since Mar 15Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/ruyi512/redis-kit)[ Packagist](https://packagist.org/packages/wangruyi/redis-kit)[ RSS](/packages/wangruyi-redis-kit/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

RedisKit - PHP Redis 工具包
========================

[](#rediskit---php-redis-工具包)

一个简单易用的 PHP Redis 组件库，提供 Redis 客户端连接、分布式锁、基于 Stream 的消息队列等功能。

功能特性
----

[](#功能特性)

- **Redis 客户端**：单例连接管理，支持多配置实例
- **分布式锁（RedisLock）**：基于 Redis 的带阻塞机制的分布式锁，支持自动过期与信号唤醒
- **消息队列（MessageQueue）**：基于 Redis Stream 结构，支持消费者组、未读/待处理消息重放、消息确认与删除
- **锁工厂（RedisLockFactory）**：便捷创建分布式锁实例

安装
--

[](#安装)

通过 Composer 安装：

```
composer require wangruyi/redis-kit
```

使用示例
----

[](#使用示例)

### 1. Redis 客户端

[](#1-redis-客户端)

```
use Wangruyi\RedisKit\Redis;

$config = [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'timeout'  => 5.0,
    'password' => '', // 可选
    'select'   => 0,  // 可选数据库编号
];

$redis = Redis::getInstance($config);

$redis->set('key', 'value');
echo $redis->get('key'); // value
```

### 2. 分布式锁（RedisLock）

[](#2-分布式锁redislock)

通过 `RedisLockFactory` 创建锁实例：

```
use Wangruyi\RedisKit\RedisLockFactory;

$lock = RedisLockFactory::create($config); // $config 同上

// 申请锁（键名、过期时间、阻塞时间）
if ($lock->acquire('myLock', 10, 5)) {
    // 成功获得锁，执行临界区代码
    echo 'Lock acquired';
    // ...
    $lock->release('myLock'); // 释放锁
} else {
    echo 'Failed to acquire lock';
}
```

直接使用 `RedisLock`：

```
use Wangruyi\RedisKit\Redis;
use Wangruyi\RedisKit\RedisLock;

$redis = Redis::getInstance($config);
$identifier = uniqid('', true); // 唯一标识
$lock = new RedisLock($redis, $identifier);

// 同样调用 acquire / release
```

### 3. 消息队列（MessageQueue）

[](#3-消息队列messagequeue)

```
use Wangruyi\RedisKit\Redis;
use Wangruyi\RedisKit\MessageQueue;

$redis = Redis::getInstance($config);
$queue = new MessageQueue($redis, 'myStream', 'myGroup', 'consumer1');

// 初始化队列（如果不存在会创建）
$queue->init();

// 生产消息
$queue->push(['Hello', 'World']);

// 消费消息（最多2条，阻塞1000毫秒）
$messages = $queue->pop(2, 1000);
foreach ($messages as $msg) {
    echo $msg['_id'] . ': ' . $msg['data'] . PHP_EOL;
    // 确认消息
    $queue->ack([$msg['_id']]);
}

// 重新处理待处理的消息（pending）
$pending = $queue->getPending(10);
// ...
```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance54

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity12

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c5f62e750ba0d8d714f9dba44198998f377d02313e5771cacea8b1617b143a33?d=identicon)[ruyi512](/maintainers/ruyi512)

---

Top Contributors

[![ruyi512](https://avatars.githubusercontent.com/u/8372066?v=4)](https://github.com/ruyi512 "ruyi512 (11 commits)")

### Embed Badge

![Health badge](/badges/wangruyi-redis-kit/health.svg)

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

###  Alternatives

[cekurte/environment

A library to get the values from environment variables and process to php data types

5886.2k8](/packages/cekurte-environment)[craftcms/simple-text

This plugin adds a new “Simple Text” field type to Craft, which provides a textarea that’s optimized for entering documentation.

2624.5k](/packages/craftcms-simple-text)[php-redis/bloom-filter

php-redis/bloom-filter

102.1k](/packages/php-redis-bloom-filter)

PHPackages © 2026

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