Predis — a flexible Redis client for PHP
Redis is a high-performance key-value data store often used for caching, sessions, queues, and real-time applications. There are several libraries for working with Redis from PHP, and one of the most popular is Predis. It is a pure PHP client implementation that does not require installing additional extensions (unlike phpredis). Predis supports all basic Redis commands, clustering, replication, Pub/Sub, and transactions.
Installing Predis via Composer
Predis is installed in the standard way via Composer. Make sure Composer is installed in your project and run the command:
composer require predis/predisAfter installation, the dependency will appear in the composer.json file, and the library will be available in the vendor folder.
Connecting to a Redis server
To get started, create an instance of the Predis client. By default, it connects to 127.0.0.1:6379, but you can specify your own parameters.
<?phprequire 'vendor/autoload.php';
use Predis\\Client;
// Connection with default parameters$client = new Client();
// Or with explicit host and port$client = new Client([ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379,]);
// If a password is required$client = new Client([ 'host' => 'localhost', 'port' => 6379, 'password' => 'my_secret_password',]);?>If Redis is running on a remote server or uses a UNIX socket, simply change the corresponding parameters.
Basic data operations
Predis supports all standard Redis data types: strings, hashes, lists, sets, and sorted sets. Let's look at basic examples.
Working with strings
<?php// Set a value$client->set('user:100:name', 'Ivan Petrov');
// Get a value$name = $client->get('user:100:name');echo $name; // Ivan Petrov
// Check if a key existsif ($client->exists('user:100:name')) { echo 'Key exists';}
// Delete a key$client->del(['user:100:name']);?>Working with hashes
Hashes are convenient for storing objects — for example, user data.
<?php// Set hash fields$client->hmset('user:100', [ 'name' => 'Anna Smirnova', 'email' => 'anna@example.com', 'age' => 28,]);
// Get a single field$email = $client->hget('user:100', 'email');
// Get all fields$userData = $client->hgetall('user:100');print_r($userData);
// Increment a numeric field$client->hincrby('user:100', 'age', 1);?>Working with lists
Redis lists are ideal for queues and logs.
<?php// Add elements to the beginning of the list (stack)$client->lpush('tasks', 'task1', 'task2', 'task3');
// Add to the end of the list (queue)$client->rpush('tasks', 'task4');
// Get all list elements$allTasks = $client->lrange('tasks', 0, -1);print_r($allTasks);
// Pop the first element$first = $client->lpop('tasks');echo $first; // task3?>Setting key time-to-live (TTL)
One of the powerful features of Redis is automatic key deletion after a specified time. This is often used for caching.
<?php// Set a key with a TTL of 60 seconds$client->setex('cache:page', 60, 'Page HTML content');
// Or for an existing key$client->expire('cache:page', 120);
// Check remaining time-to-live$ttl = $client->ttl('cache:page');echo "$ttl seconds remaining";?>Working with transactions
Predis supports multi-commands (trans
Also in library
C++ Benchmarking Guide: Google Benchmark Tutorial & Examples
NetworkExtension for Swift: Complete Guide with Examples
Swift QR Code Generator: Core Image, CIFilter and Custom Solutions
Task Queues for TypeScript: Bull, Bee-Queue, and Asynchronous Processing