PHPackages                             veasin/ff-redis - 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. veasin/ff-redis

ActiveLibrary[Caching](/categories/caching)

veasin/ff-redis
===============

Redis connection pool, queue driver, and cache driver for ff framework

0.0.1(1mo ago)00LGPL-3.0-or-laterPHPPHP &gt;=8.4

Since Jul 8Pushed 1mo agoCompare

[ Source](https://github.com/veasin/ff-redis)[ Packagist](https://packagist.org/packages/veasin/ff-redis)[ Docs](https://github.com/veasin/ff)[ RSS](/packages/veasin-ff-redis/feed)WikiDiscussions main Synced 1w ago

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

ff-redis
========

[](#ff-redis)

Redis connection pool, queue driver, and cache driver for ff framework (PHP 8.4+).

```
composer require veasin/ff-redis

```

---

Connection Pool
---------------

[](#connection-pool)

按配置名管理 Redis 连接生命周期，支持惰性验证与自动重连。由 `ff\resource\redis()` 函数实现，被 `ff\cache\redis()` 和 `ff\queue\redis()` 驱动内部自动调用，也可直接使用。

```
use function ff\resource\redis;

$redis = redis('default');   // get/create default connection
$redis = redis('cache');     // get/create named connection
redis(null);                  // clear all connections + failure flags
```

连接通过 `container("#redis.{name}")` 配置。默认 `$name = 'default'`，未配置时自动回退：

```
['host' => '127.0.0.1', 'port' => 6379]
```

Redis 扩展不存在或连接失败时返回 `null`，不会抛异常。

### 惰性验证

[](#惰性验证)

通过 `#resource.redis.ttl` 设置验证间隔（秒），超时后自动执行 `ping()` 检测连接健康，断连时自动重建。默认 `0` 不验证。

```
container('#resource.redis.ttl', 30);  // 每 30 秒验证一次
```

### 自定义工厂

[](#自定义工厂)

Swoole 协程版 Redis 或其他自定义实现：

```
container('#resource/redis:', fn($config) => {
    $c = new \Swoole\Coroutine\Redis();
    $c->connect($config['host'], $config['port'] ?? 6379);
    return $c;
});
```

### 配置项

[](#配置项)

容器 key类型说明`#redis.{name}``array`Redis 连接配置，支持 `host`、`port`、`password`、`database`、`timeout`、`prefix``#resource.redis.ttl``int`连接验证间隔秒数，默认 `0` 不验证`#resource/redis:``callable`自定义 Redis 工厂，签名 `fn(array $config): \Redis`---

Queue Driver
------------

[](#queue-driver)

基于 Redis list 的队列驱动，生产使用 `lPush + serialize`，消费使用 `brPop` 阻塞弹出。

### 注册

[](#注册)

```
container('^#ext.queue.redis', \ff\queue\redis(...));
```

ff-redis 的 `bootstrap.php` 已自动完成注册，安装后即可使用。

### 使用

[](#使用)

```
// 生产
ff\queue\redis('orders', ['order_id' => 123]);
ff\queue\redis('orders', 'plain text');

// 消费
ff\queue\redis('orders', fn($msg) => true);     // ack
ff\queue\redis('orders', fn($msg) => false);    // nack + requeue
ff\queue\redis('orders', fn($msg) => null);     // nack + discard
ff\queue\redis('orders', fn($msg) => 5);        // retry after 5 seconds
```

### 独立使用

[](#独立使用)

在 `ext('queue', ...)` 之外也可直接调用底层驱动：

```
use function ff\queue\redis;
redis('orders', ['id' => 1]);                              // 生产
redis('orders', fn($m) => true);                           // 消费
redis('orders', fn($m) => true, ['timeout' => 10]);        // 指定超时
```

`timeout` 为 `brPop` 阻塞等待秒数，`0` 表示永久阻塞。

### 配置

[](#配置)

通过 `container('#queue/redis')` 配置：

```
container('#queue/redis', [
    'config'  => 'default',   // 使用的 Redis 连接名（#redis.{name}）
    'prefix'  => 'q:',        // 队列 key 前缀
    'timeout' => 10,          // brPop 超时秒数，0 永久阻塞
]);
```

容器 key类型说明`#queue/redis``array`队列驱动配置：`config`、`prefix`、`timeout`、`ttl`---

Cache Driver
------------

[](#cache-driver)

签名与 `apcu()` 完全一致。底层使用 `ff\resource\redis()` 获取连接，无需单独配置连接。

### CRUD

[](#crud)

```
use function ff\cache\redis;

redis('key');                       // read
redis('key', 'value');              // write
redis('key', null);                 // delete
redis(null);                        // flush all
redis('key', 'value', 60);          // write + TTL
redis('key', 'value', ['ttl' => 60, 'config' => 'cfg']);  // TTL + 配置名
redis('key', 'value', 'cfg');       // 配置名（string 简写）

// Batch
redis(['k1', 'k2']);                // multi-get
redis(['k1' => 'v1']);              // multi-set
```

### 中间件工厂模式

[](#中间件工厂模式)

与 `cache()` 编排函数配合使用：

```
cache(redis('my_key', middleware: ['ttl' => 60]), fn($next) => render());
cache(redis('my_key', 'fallback', middleware: 60), fn($next) => null);
```

### 配置

[](#配置-1)

```
container('#cache.redis', [
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'password' => 'secret',
    'database' => 0,
]);

// 命名配置
container('#cache.redis.cfg', [
    'ttl'    => 3600,
    'prefix' => 'c:',
]);
```

容器 key类型说明`#cache.redis``array`默认缓存 Redis 连接配置`#cache.redis.{name}``array`命名配置，支持 `ttl`、`prefix`---

Ext Registration
----------------

[](#ext-registration)

ff-redis 自动在 `bootstrap.php` 中注册 ext 驱动（通过 `composer.json` 的 `autoload.files` 加载）：

```
container('^#ext.queue.redis', \ff\queue\redis(...));
container('^#ext.cache.redis', \ff\cache\redis(...));
```

安装后即可通过 `ext('queue', 'redis', ...)` 和 `ext('cache', 'redis', ...)` 调用。

也支持直接 `use function ff\queue\redis` / `use function ff\cache\redis` 独立使用。

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance90

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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

49d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/127591812?v=4)[vea](/maintainers/vea)[@Vea](https://github.com/Vea)

### Embed Badge

![Health badge](/badges/veasin-ff-redis/health.svg)

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

###  Alternatives

[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

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