Redis client for PHP | Generated by AI

Home 2025.08

To use the "predis/predis": "1.1" library in PHP, you’re referring to a Redis client for PHP that can be installed via Composer. Here’s a step-by-step guide to set it up and use it in your PHP project:


1. Install Composer (if not already installed)

If you don’t have Composer installed, download and install it from getcomposer.org. It’s a dependency manager for PHP.


2. Set Up Your Project

If you don’t already have a project, create a directory for it:

mkdir my-redis-project
cd my-redis-project

3. Add Predis to Your Project

Run the following Composer command to install version 1.1 of Predis:

composer require predis/predis:1.1

This will download Predis version 1.1 and generate a composer.json file (and a vendor directory with the library).

Alternatively, if you already have a composer.json file, you can manually add this line to it:

{
    "require": {
        "predis/predis": "1.1"
    }
}

Then run:

composer install

4. Include the Autoloader

In your PHP script, include the Composer autoloader to load Predis:

require 'vendor/autoload.php';

5. Basic Usage Example

Here’s a simple example of how to connect to a Redis server and use Predis:

<?php
require 'vendor/autoload.php';

use Predis\Client;

// Create a new Redis client instance
$redis = new Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1', // Replace with your Redis server host
    'port'   => 6379,        // Default Redis port
]);

// Set a key-value pair
$redis->set('mykey', 'Hello, Redis!');

// Get the value back
$value = $redis->get('mykey');
echo $value; // Outputs: Hello, Redis!

// Example with a list
$redis->lpush('mylist', 'item1');
$redis->lpush('mylist', 'item2');
$list = $redis->lrange('mylist', 0, -1);
print_r($list); // Outputs: Array ( [0] => item2 [1] => item1 )
?>

6. Configuration Options

You can customize the connection by passing an array of options to the Client constructor. For example:

$redis = new Client([
    'scheme' => 'tcp',
    'host'   => 'redis.example.com',
    'port'   => 6379,
    'password' => 'yourpassword', // If authentication is required
    'database' => 0,              // Select a specific Redis database
]);

7. Common Commands

Predis supports most Redis commands directly as methods. Here are a few examples:

For a full list, check the Predis documentation or the Redis command reference.


8. Error Handling

Wrap your Redis calls in a try-catch block to handle potential connection or runtime errors:

try {
    $redis->set('test', 'value');
    echo $redis->get('test');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

9. Prerequisites


That’s it! You’re now set up to use Predis 1.1 in your PHP project. Let me know if you need help with specific Redis operations or troubleshooting!


Back Donate