Installation
Before we start using Redis in PHP, we need to make sure that we have the Redis service and the PHP Redis driver installed and that PHP is working properly on your machine. Next, let's install the PHP redis driver:https://github.com/nicolasff/phpredis.
PHP Installation Redis Extensions
- /usr/local/PHP/bin/phpize #php安装后的路径
- ./Configure --with-php-config=/usr/local/PHP/Bin /php-config
- Make && make install
Modify the php.ini file
- VI /usr/local/PHP/lib/php. INI
Add the following content:
- Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
- Extension=Redis. so
Restart PHP-FPM or Apache after the installation is complete. View the phpinfo information to see the Redis extension.
Connect to the Redis service
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->
Connect(' 127.0.0.1 ', 6379);
- echo "Connection to Server sucessfully";
- //See if the service is running
- Echo "Server is running:"+ $redisping();
- ?>
Execute the script with the output as:
- Connection to server sucessfully
- Server is running: PONG
Redis PHP String (string) instance
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->
Connect(' 127.0.0.1 ', 6379);
- echo "Connection to Server sucessfully";
- //Set Redis string data
- $redis, set("Tutorial-name", "Redis Tutorial");
- //Get stored data and output
- echo "Stored string in Redis::" + Jedis. Get("Tutorial-name");
- ?>
Execute the script with the output as:
- Connection to server sucessfully
- Stored string in redis:: redis tutorial
Redis PHP list (list) instance
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->
Connect(' 127.0.0.1 ', 6379);
- echo "Connection to Server sucessfully";
- //Store data in the list
- $redis-Lpush("Tutorial-list", "Redis");
- $redis-Lpush("Tutorial-list", "Mongodb");
- $redis-Lpush("Tutorial-list", "Mysql");
- //Get stored data and output
- $arList = $redislrange("Tutorial-list", 0 ,5);
- echo "Stored string in Redis::"
- Print_r($arList);
- ?>
Execute the script with the output as:
- Connection to server sucessfully
- Stored string in redis::
- Redis
- Mongodb
- Mysql
Redis PHP Keys Instance
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->
Connect(' 127.0.0.1 ', 6379);
- echo "Connection to Server sucessfully";
- //Get data and output
- $arList = keys("*") $redis;
- echo "Stored keys in Redis::"
- Print_r($arList);
- ?>
Execute the script with the output as:
- Connection to server sucessfully
- Stored string in redis::
- Tutorial-name
- Tutorial-list
Using Redis with PHP