Home > Backend Development > PHP Tutorial > PHP link redis method code

PHP link redis method code

小云云
Release: 2023-03-20 20:28:01
Original
3636 people have browsed it

Before using Redis in a PHP program, you need to ensure that the Redis PHP driver and PHP environment are installed on the machine. You can first install PHP on your computer and configure the environment. This article will share with you the method code of PHP linking to redis, hoping to help you.

Installation

Now, let’s see how to set up the Redis PHP driver.
Download phpredis from the github library => http://github.com/nicolasff/phpredis. After downloading it, extract the files to the phpredis directory. On Ubuntu, install the following extensions.

cd phpredis 
sudo phpize 
sudo ./configure 
sudo make 
sudo make install
Copy after login

Now, copy and paste the contents of the “modules” folder into the PHP extensions directory and add the following lines in php.ini.

extension = redis.so
Copy after login

Now, the Redis PHP installation is complete!

Connect to the Redis server using

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   //check whether server is running or not 
   echo "Server is running: ".$redis->ping(); ?>
Copy after login

PHP

When the program is executed, the following results will be produced.

Connection to server sucessfully 
Server is running: PONG
Copy after login

Redis PHP string example

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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"); ?>
Copy after login

Execute the above code, the following results will be generated-

Connection to server sucessfully 
Stored string in redis:: Redis tutorial
Copy after login

Redis php list example

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 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); ?>
Copy after login

Execute the above code, The following results will be generated-

Connection to server sucessfully 
Stored string in redis:: Redis 
Mongodb 
Mysql
Copy after login

Redis php key example

<?php 
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect(&#39;127.0.0.1&#39;, 6379); 
   echo "Connection to server sucessfully"; 
   // Get the stored keys and print it 
   $arList = $redis->keys("*"); 
   echo "Stored keys in redis:: " 
   print_r($arList); ?>
Copy after login

Executing the above code will generate the following results-

Connection to server sucessfully 
Stored string in redis:: tutorial-name 
tutorial-list
Copy after login

Related recommendations:

Explanation of PHP operation Redis examples

A simple example sharing of php+redis

Some ways to use Redis in PHP

The above is the detailed content of PHP link redis method code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template