Predis for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Predis and why is it needed

\n\n

Predis is a full-featured client for working with Redis (Remote Dictionary Server) in PHP. Redis is a high-performance key-value data store, often used for caching, session management, message queues, and real-time applications. Predis allows PHP applications to interact with a Redis server by sending commands and receiving responses.

\n\n

Why use Predis? Unlike other clients (e.g., phpredis), Predis is written entirely in PHP and does not require installing additional C extensions. This makes it an ideal choice for environments where you cannot control extension installation (e.g., shared hosting). Predis supports all major Redis features: strings, hashes, lists, sets, transactions, Pub/Sub, clustering, and much more.

\n\n

Main use cases:

\n
    \n
  • Caching — speeding up your website by storing frequently requested data in memory.
  • \n
  • Session management — storing user sessions in Redis for fast access and scalability.
  • \n
  • Task queues — organizing background task processing using Redis lists.
  • \n
  • Ratings and counters — using sorted sets to build leaderboards.
  • \n
  • Pub/Sub — implementing real-time messaging systems.
  • \n
\n\n

Installing Predis

\n\n

Predis is distributed via Composer, the standard package manager for PHP. Make sure you have Composer installed. If not, download it from getcomposer.org.

\n\n

To install Predis, run the following command in the root of your project:

\n\n
composer require predis/predis
\n\n

After installation, a vendor folder and a composer.json file will appear in your project. To include Predis, use the Composer autoloader:

\n\n
require 'vendor/autoload.php';
\n\n

Minimum requirements: PHP 7.2 or higher. Predis does not require the Redis PHP extension, as it implements the Redis protocol in pure PHP.

\n\n

Quick start with code example

\n\n

Let's write a simple script that connects to Redis, saves a value, and reads it.

\n\n
<?php\nrequire 'vendor/autoload.php';\n\nuse Predis\\Client;\n\n// Create a Redis client (default connects to localhost:6379)\n$client = new Client();\n\n// Set a value\n$client->set('greeting', 'Hello, Redis!');\n\n// Get the value\n$value = $client->get('greeting');\necho $value; // Outputs: Hello, Redis!\n\n// Check if the key exists\nif ($client->exists('greeting')) {\n    echo "Key exists!";\n}\n\n// Delete the key\n$client->del(['greeting']);\n?>
\n\n

This code does the following:

\n
    \n
  1. Connects to the Redis server on localhost (port 6379).
  2. \n
  3. Saves the string "Hello, Redis!" under the key "greeting".
  4. \n
  5. Reads the value back and prints it.
  6. \n
  7. Checks if the key exists.
  8. \n
  9. Deletes the key.
  10. \n
\n\n

If Redis is running on a different host or port, pass connection parameters:

\n\n
$client = new Client([\n    'scheme' => 'tcp',\n    'host'   => '192.168.1.100',\n    'port'   => 6379,\n]);
\n\n

Main Predis methods

\n\n

Predis provides a convenient interface for all Redis commands. Let's look at the 10 most commonly used methods with detailed examples.

\n\n

1. set — setting a value

\n

What it does: Sets the value for the specified key. If the key already exists, its value is overwritten.

\n

Parameters:

\n
    \n
  • $key (string) — the key name.
  • \n

Recommendations