
Product
Socket Zero Introduces Automated Remediation PRs
Put vulnerability remediation on autopilot with automated security patches and intelligent dependency upgrades across your GitHub repositories.
since/redis
Advanced tools
一个基于PHP 8+的高性能Redis客户端库,提供连接池+门面模式和原始单例模式两种使用方式,支持Redis所有数据结构操作,包含完整的类型声明和异常处理机制。
一个基于PHP 8+的高性能Redis客户端库,提供连接池+门面模式和原始单例模式两种使用方式,支持Redis所有数据结构操作,包含完整的类型声明和异常处理机制。
使用Composer安装:
composer require since/redis
ext-redis)ext-json)门面模式是推荐的使用方式,它通过连接池管理Redis连接,适合高并发场景。
// 引入自动加载文件
require_once 'vendor/autoload.php';
use since\RedisFacade;
// 获取RedisFacade实例(可配置连接参数)
$redis = RedisFacade::getInstance([
'host' => '127.0.0.1', // Redis服务器地址
'port' => 6379, // Redis端口
'password' => '', // Redis密码(如无密码则为空)
'database' => 0, // 数据库索引
'timeout' => 2, // 连接超时时间(秒)
'max_connections' => 10, // 最大连接数
]);
// 字符串操作示例
$redis->string()->set('name', 'RedisClient');
$name = $redis->string()->get('name');
// 哈希操作示例
$redis->hash()->hSet('user:1', 'name', '张三');
$redis->hash()->hSet('user:1', 'age', 25);
$userInfo = $redis->hash()->hGetAll('user:1');
// 列表操作示例
$redis->list()->lpush('list', 'item1');
$redis->list()->lpush('list', 'item2');
$items = $redis->list()->lrange('list', 0, -1);
// 键操作示例
$redis->key()->expire('name', 3600);
$exists = $redis->key()->exists('name');
单例模式是传统的使用方式,适合简单场景:
// 引入自动加载文件
require_once 'vendor/autoload.php';
use since\Redis;
// 获取Redis实例(可配置连接参数)
$redis = Redis::getInstance([
'host' => '127.0.0.1', // Redis服务器地址
'port' => 6379, // Redis端口
'password' => '', // Redis密码(如无密码则为空)
'select' => 0, // 数据库索引
'timeout' => 0, // 连接超时时间(秒)
'expire' => 0, // 默认过期时间(秒)
'persistent' => false, // 是否使用持久连接
'prefix' => '', // 键前缀
'serialize' => true, // 是否自动序列化
]);
// 直接调用Redis方法
$redis->set('name', 'RedisClient');
$name = $redis->get('name');
$redis->expire('name', 3600);
本库支持Redis的所有数据结构操作,通过不同的操作类提供:
| 数据结构 | 操作类 | 门面方法 | 描述 |
|---|---|---|---|
| String | StringOperation | string() | 字符串类型操作 |
| Hash | HashOperation | hash() | 哈希表类型操作 |
| List | ListOperation | list() | 列表类型操作 |
| Set | SetOperation | set() | 集合类型操作 |
| SortedSet | SortedSetOperation | sortedSet() | 有序集合类型操作 |
| Geo | GeoOperation | geo() | 地理空间类型操作 |
| HyperLogLog | HyperLogLogOperation | hyperLogLog() | 基数统计类型操作 |
| Stream | StreamOperation | stream() | 流类型操作 |
| Key | KeyOperation | key() | 键操作 |
连接池模式下的主要配置参数:
$config = [
'host' => '127.0.0.1', // Redis服务器地址
'port' => 6379, // Redis端口
'password' => '', // Redis密码
'database' => 0, // 数据库索引
'timeout' => 2, // 连接超时时间(秒)
'max_connections' => 10, // 最大连接数
];
$redis = RedisFacade::getInstance($config);
单例模式下的主要配置参数:
$options = [
'host' => '127.0.0.1', // Redis服务器地址
'port' => 6379, // Redis端口
'password' => '', // Redis密码
'select' => 0, // 数据库索引
'timeout' => 0, // 连接超时时间(秒)
'expire' => 0, // 默认过期时间(秒)
'persistent' => false, // 是否使用持久连接
'prefix' => '', // 键前缀
'serialize' => true, // 是否自动序列化
];
$redis = Redis::getInstance($options);
本库提供了完善的异常处理机制,使用时建议添加try-catch块捕获可能的异常:
try {
$redis->string()->set('name', 'RedisClient');
$name = $redis->string()->get('name');
} catch (\Exception $e) {
// 处理异常
echo 'Redis操作失败: ' . $e->getMessage();
}
高并发场景推荐使用门面模式(连接池):
合理设置连接池参数:
max_connectionstimeout,避免连接等待时间过长使用键前缀:
合理设置过期时间:
错误处理:
使用管道(Pipeline):
合理使用事务:
避免大键:
本项目使用MIT许可证 - 详见LICENSE文件
FAQs
一个基于PHP 8+的高性能Redis客户端库,提供连接池+门面模式和原始单例模式两种使用方式,支持Redis所有数据结构操作,包含完整的类型声明和异常处理机制。
We found that since/redis demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.

Product
Put vulnerability remediation on autopilot with automated security patches and intelligent dependency upgrades across your GitHub repositories.

Product
Socket Zero automates vulnerability remediation with upgrades and targeted patches, including fixes for 100% of known, patchable RubyGems CVEs.

Product
Explore Socket’s redesigned Alerts UI, multi-branch repository monitoring, and new tools for resolving findings, searching scans, and filtering security alerts.