Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

PHP Redis Tutorial

Introduction to Redis

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. In this tutorial, we will learn how to interact with Redis using PHP.

Installing Redis and PHP Redis Extension

First, you need to install Redis on your system. You can do this by following the instructions on the official Redis website.

sudo apt-get update

sudo apt-get install redis-server

Next, we need to install the PHP Redis extension. This can be done using PECL (PHP Extension Community Library).

sudo pecl install redis

After the installation, enable the extension in your php.ini file:

extension=redis.so

Finally, restart your web server to apply the changes:

sudo systemctl restart apache2

or

sudo systemctl restart nginx

Connecting to Redis

Let's start by creating a simple PHP script to connect to Redis.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server successfully";
?>

Save this script as connect.php and run it in your browser. If everything is set up correctly, you should see the message "Connection to server successfully".

Working with Strings

Redis provides simple commands to set and get string values. Let's see how to use them in PHP.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Setting a string value
$redis->set("tutorial-name", "PHP Redis tutorial");

// Getting the string value
$value = $redis->get("tutorial-name");
echo "Stored string in redis: " . $value;
?>

Save this script as strings.php and run it in your browser. You should see the message "Stored string in redis: PHP Redis tutorial".

Working with Hashes

A Redis hash is a collection of key-value pairs. Let's see how to set and get hash values in PHP.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Setting hash values
$redis->hSet("user:1000", "name", "John Doe");
$redis->hSet("user:1000", "email", "john.doe@example.com");

// Getting hash values
$name = $redis->hGet("user:1000", "name");
$email = $redis->hGet("user:1000", "email");
echo "Name: " . $name . ", Email: " . $email;
?>

Save this script as hashes.php and run it in your browser. You should see the message "Name: John Doe, Email: john.doe@example.com".

Working with Lists

Redis lists are simple lists of strings, sorted by insertion order. Let's see how to work with lists in PHP.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Adding values to a list
$redis->lPush("tutorial-list", "PHP");
$redis->lPush("tutorial-list", "Redis");
$redis->lPush("tutorial-list", "Tutorial");

// Getting values from the list
$list = $redis->lRange("tutorial-list", 0, -1);
echo "List items: ";
print_r($list);
?>

Save this script as lists.php and run it in your browser. You should see the list items printed to the screen.

Working with Sets

Redis sets are unordered collections of strings. Let's see how to work with sets in PHP.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Adding values to a set
$redis->sAdd("tutorial-set", "PHP");
$redis->sAdd("tutorial-set", "Redis");
$redis->sAdd("tutorial-set", "Tutorial");

// Getting values from the set
$set = $redis->sMembers("tutorial-set");
echo "Set members: ";
print_r($set);
?>

Save this script as sets.php and run it in your browser. You should see the set members printed to the screen.

Working with Sorted Sets

Redis sorted sets are similar to sets but they maintain a score associated with each element. Let's see how to work with sorted sets in PHP.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Adding values to a sorted set
$redis->zAdd("tutorial-sorted-set", 1, "PHP");
$redis->zAdd("tutorial-sorted-set", 2, "Redis");
$redis->zAdd("tutorial-sorted-set", 3, "Tutorial");

// Getting values from the sorted set
$sortedSet = $redis->zRange("tutorial-sorted-set", 0, -1);
echo "Sorted set members: ";
print_r($sortedSet);
?>

Save this script as sortedsets.php and run it in your browser. You should see the sorted set members printed to the screen.

Conclusion

In this tutorial, we covered the basics of using Redis with PHP. We learned how to connect to a Redis server and perform various operations with strings, hashes, lists, sets, and sorted sets. Redis is a powerful tool that can greatly enhance the performance of your PHP applications by providing fast and efficient data storage and retrieval.

For more advanced features and use cases, refer to the official Redis documentation.