Installation
To use Redis in a PHP program, you first need to make sure that the Redis PHP driver and the PHP installation are set up on the machine. You can view PHP tutorials to teach you how to install PHP on your machine. Now, let's look at how to set up a Redis PHP driver.
Need to download Phpredis from GitHub on the repository: Https://github.com/nicolasff/phpredis. After the download is complete, unzip the file to the Phpredis directory. Install this extension on Ubuntu, which can be installed using the commands shown.
CD Phpredissudo Phpizesudo./configuresudo Makesudo Make Install
Now copy and paste the contents of the "modules" folder into the PHP extension directory and add the following lines in the php.ini.
Extension = redis.so
Now the Redis and PHP installation is complete.
Connect to a Redis server
<?php //connecting to Redis server on localhost $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); echo "Connection to Server sucessfully"; Check whether server is running or not echoes "server is running:". $redis->ping ();? >
When the program executes, the following results are produced:
Connection to server Sucessfullyserver is Running:pong
Example of a Redis PHP string
<?php //connecting to Redis server on localhost $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); echo "Connection to Server sucessfully"; Set the data in Redis string $redis->set ("Tutorial-name", "Redis Tutorial"); Get the stored data and print it echo "stored string in Redis::". $redis. Get ("Tutorial-name");? >
When the program executes, the following results are produced:
Connection to server sucessfullystored string in Redis:: Redis Tutorial
Example of a redis PHP column
<?php //connecting to Redis server on localhost $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); echo "Connection to Server sucessfully"; Store data in Redis list $redis->lpush ("Tutorial-list", "Redis"); $redis->lpush ("Tutorial-list", "Mongodb"); $redis->lpush ("Tutorial-list", "Mysql"); Get the stored data and print it $arList = $redis->lrange ("Tutorial-list", 0, 5); echo "Stored string in Redis::" print_r ($arList);? >
When the program executes, the following results are produced:
Connection to server sucessfullystored string in Redis::redismongodbmysql
Example of a Redis PHP key
<?php //connecting to Redis server on localhost $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); echo "Connection to Server sucessfully"; Get the stored keys and print it $arList = $redis->keys ("*"); echo "Stored keys in Redis::" print_r ($arList);? >
When the program executes, the following results are produced:
Connection to server sucessfullystored string in Redis::tutorial-nametutorial-list
Redis PHP Connection operation