文章永久连接:https://tech.souyunku.com/4801
PHP 可以通过 phpredis 扩展访问 Redis
安装 phpredis 扩展
PHP 语言访问 Redis 需要先安装 Redis 服务和 PHP Redis 扩展。
phpredis 扩展官方地址为 https://github.com/phpredis/phpredis
phpredis 扩展下载地址为: https://github.com/phpredis/phpredis/releases
当前最新的扩展版本为: 3.1.4
1. 使用下面的一些列命令安装这个 phpredis 扩展
$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ tar zxvf 3.1.4.tar.gz # 解压
$ cd phpredis-3.1.4 # 进入 phpredis 目录
$ /usr/local/php/bin/phpize # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && sudo make install
如果是 PHP 7 及以上版本,则需要下载指定分支:
git clone -b php7 https://github.com/phpredis/phpredis.git
2. 修改 php.ini 文件
修改 php.ini 文件添加 redis 扩展
vi /usr/local/php/lib/php.ini
增加如下内容:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension = redis.so
3. 重启 WEB 服务器
安装完成后重启 php-fpm 或 apache,查看 phpinfo 信息,就能看到 redis 扩展
或者输入以下命令来检查
$ php -i | grep redis
/usr/local/etc/php/5.6/conf.d/ext-redis.ini,
redis
Registered save handlers => files user memcache memcached redis rediscluster
PHP 连接到 Redis 服务
<?php
/*
* filename: main.php
* author: 搜云库技术团队(tech.souyunku.com)
* Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully\n";
//查看服务是否运行
echo "Server is running: " . $redis->ping();
echo "\n";
运行以上 PHP 脚本,输出结果如下
$ php main.php
Connection to server sucessfully
Server is running: +PONG
PHP 存储/获取 Redis 字符串( String )
<?php
/*
* filename: main.php
* author: 搜云库技术团队(tech.souyunku.com)
* Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully\n";
//设置 redis 字符串数据
$redis->set("site", "tech.souyunku.com");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("site");
echo "\n";
运行以上 PHP 脚本,输出结果如下
$ php main.php
Connection to server sucessfully
Stored string in redis:: tech.souyunku.com
PHP 访问 Redis 列表( List )
<?php
/*
* filename: main.php
* author: 搜云库技术团队(tech.souyunku.com)
* Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->flushall();
echo "Connection to server sucessfully\n";
//存储数据到列表中
$redis->lpush("language", "Python2");
$redis->lpush("language", "C++");
$redis->lpush("language", "Perl");
// 获取存储的数据并输出
$arList = $redis->lrange("language", 0 ,5);
echo "Stored string in Redis\n";
print_r($arList);
运行以上 PHP 脚本,输出结果如下
$ php main.php
Connection to server sucessfully
Stored string in Redis
Array
(
[0] => Perl
[1] => C++
[2] => Python2
)
PHP 访问 Redis 键( Keys )
<?php
/*
* filename: main.php
* author: 搜云库技术团队(tech.souyunku.com)
* Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully\n";
// 获取数据并输出
$arList = $redis->keys("*");
echo "Stored keys in redis:: ";
print_r($arList);
运行以上 PHP 脚本,输出结果如下
$ php main.php
Connection to server sucessfully
Stored keys in redis:: Array
(
[0] => language
)
干货推荐
附录:Redis 教程:系列文章
- 一、Redis 基础教程
- 二、Redis 简介
- 三、Redis 安装
- 四、Redis 配置
- 五、Redis redis.conf 配置选项
- 六、Redis 数据类型
- 七、Redis 命令
- 八、Redis 键(key) 命令
- 九、Redis 字符串(String) 命令
- 十、Redis 哈希(Hash) 命令
- 十一、Redis 列表(List) 命令
- 十二、Redis 集合(Set) 命令
- 十三、Redis 有序集合(sorted set) 命令
- 十四、Redis HyperLogLog 命令
- 十五、Redis 发布订阅
- 十六、Redis 事务
- 十七、Redis Script( 脚本 ) 命令
- 十八、Redis 连接命令
- 十九、Redis 服务器
- 二十、Java 使用 Redis
- 【当前读到】二十一、PHP 和 Redis
- 二十二、Redis 数据备份与恢复
- 二十三、Redis 服务安全
- 二十四、Redis 性能测试
- 二十五、Redis 客户端连接
- 二十六、Redis 管道技术
- 二十七、Redis 分区