配置php redis 扩展
参照runoob:PHP 使用 Redis
Windows:
- 假设redis已经安装好 服务启动
- xampp (php 7.1 x86 windows)
查看phpinfo (php 7.1 x86/x64 ts/uts vc14/vc15)
Zend Extension Build API320160303,TS,VC14
PHP Extension Build API20160303, TS,VC14
https://windows.php.net/downloads/pecl/releases/igbinary/2.0.6rc1/
https://windows.php.net/downloads/pecl/releases/redis/3.1.3/
; php.ini extension=php_igbinary.dll extension=php_redis.dll
查看phpinfo 有redis扩展
test
<?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo "Connection to server sucessfully"; //设置 redis 字符串数据 $redis->set("tutorial-name", "Redis tutorial"); // 获取存储的数据并输出 echo "Stored string in redis:: " . $redis->get("tutorial-name"); ?>
Mac:
下载php-redis,地址:https://nodeload.github.com/nicolasff/phpredis/zip/master
链接: https://pan.baidu.com/s/1-08Wb7TsigVa1fj9k_oByg 密码: ny9g
mv ~/Downloads/phpredis-master.zip .
unzip phpredis-master.zip -d .
cd ./phpredis-master
sudo phpize
若未安装autoconf,则会报错 $ brew install autoconf
./configure --with-php-config=`which php-config`
make
make test
sudo make install
sudo emacs /Applications/XAMPP/etc/php.ini
+++++++++++++++++++++++++++++++++
extension="redis.so"
================================
重启apahce后
php -m|grep redis
查看phpredis扩展是否开启。
test:
<?php $redis = new Redis(); // connect $handle = $redis->connect('127.0.0.1', 6379); if ($handle) { echo "Connect to server successfull".PHP_EOL; var_dump($handle); } echo "Server is running: ".$redis->ping().PHP_EOL; // string $redis->set("tutorial-name", "Redis tutorial"); echo "Stored string in redis::" .$redis->get("tutorial-name").PHP_EOL; // list $redis->del("tutorial-list"); $a = ["Redis", "MongoDB", "MySQL"]; array_walk($a, function($item, $key, $redis) { $redis->lpush("tutorial-list", $item); }, $redis); $arList = $redis->lrange("tutorial-list", 0, 5); print_r($arList); echo PHP_EOL; // keys $keys = $redis->keys("*"); print_r($keys); echo PHP_EOL;
execute:
$ php connect.php
Connect to server successfull
bool(true)
Server is running: +PONG
Stored string in redis::Redis tutorial
Array
(
[0] => MySQL
[1] => MongoDB
[2] => Redis
)
Array
(
[0] => tutorial-name
[1] => myhash
[2] => foo
[3] => key
[4] => tutorial-list
)