PHP Use Redis
Installation
started inPHPused inRedisago, we need to make sure we've installed theRedisServices andPHP RedisDrive, and you can use it properly on your machinePHP. next let's installPHP RedisDrive: For:Https://github.com/nicolasff/phpredis.
PHP installation Redis Extended
/usr/local/php/bin/phpize #php the path after installation
./configure--with-php-config=/usr/local/php/bin/php-config
Make && make install
Modify 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 after installation is complete PHP-FPM or Apache . View The phpinfo information to see the redis extension.
connected to Redis Service
<?php //Connect to localRedisService $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:" + $redis->ping ();
?>
Execute the script with the output as:
Connection to Server sucessfully
Server is Running:pong
Redis PHP String ( string ) Example
<?php //Connect to localRedisService $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to server sucessfully";
//SetRedisString Data
$redis->set ("Tutorial-name", "Redis Tutorial");
// get the 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 ) Example
<?php //Connect to localRedisService$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to server sucessfully";
//storing data in a list
$redis->lpush ("Tutorial-list", "Redis");
$redis->lpush ("Tutorial-list", "Mongodb");
$redis->lpush ("Tutorial-list", "Mysql");
// get the stored data and output
$arList = $redis->lrange ("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 Example
<?php //Connect to localRedisService$redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379);
echo "Connection to server sucessfully";
// get the data and output
$arList = $redis->keys ("*");
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