PHP code example of since / redis
1. Go to this page and download the library: Download since/redis library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
since / redis example snippets
// 引入自动加载文件
获取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');
// 引入自动加载文件
�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);
$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 {
$redis->string()->set('name', 'RedisClient');
$name = $redis->string()->get('name');
} catch (\Exception $e) {
// 处理异常
echo 'Redis操作失败: ' . $e->getMessage();
}