PHP Redis Basic Example

lastmafia

Administrator
Staff member
Jun 4, 2024
5
0
1
Certainly! Below are some examples of using Redis with PHP through the Predis library. These examples cover basic operations such as setting and getting strings, working with lists, sets, and hashes.

Setting Up Predis​

First, make sure you have the Predis library installed. You can install it using Composer:


Code:
composer require predis/predis

Connecting to Redis​

Here's how you can connect to a Redis server using Predis:


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

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

echo "Connected to Redis";
?>

Example 1: Working with Strings​


PHP:
<?php
// Set a string value
$client->set('mykey', 'Hello, Redis!');

// Get the string value
$value = $client->get('mykey');
echo $value; // Output: Hello, Redis!
?>

Example 2: Working with Lists​


PHP:
<?php
// Push values to the list
$client->rpush('mylist', 'one');
$client->rpush('mylist', 'two');
$client->rpush('mylist', 'three');

// Get the entire list
$list = $client->lrange('mylist', 0, -1);
print_r($list); // Output: Array ( [0] => one [1] => two [2] => three )

// Pop a value from the list
$first = $client->lpop('mylist');
echo $first; // Output: one

// Get the remaining list
$list = $client->lrange('mylist', 0, -1);
print_r($list); // Output: Array ( [0] => two [1] => three )
?>

Example 3: Working with Sets​


PHP:
<?php
// Add values to the set
$client->sadd('myset', 'apple');
$client->sadd('myset', 'banana');
$client->sadd('myset', 'orange');

// Get all values from the set
$set = $client->smembers('myset');
print_r($set); // Output: Array ( [0] => apple [1] => banana [2] => orange )

// Check if a value is in the set
$isMember = $client->sismember('myset', 'banana');
echo $isMember ? 'Yes' : 'No'; // Output: Yes
?>

Example 4: Working with Hashes​


PHP:
<?php
// Set hash fields
$client->hmset('user:1000', [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'age' => 30
]);

// Get a specific field from the hash
$name = $client->hget('user:1000', 'name');
echo $name; // Output: John Doe

// Get all fields from the hash
$user = $client->hgetall('user:1000');
print_r($user); // Output: Array ( [name] => John Doe [email] => john.doe@example.com [age] => 30 )
?>

Example 5: Using Expiry​


PHP:
<?php
// Set a key with an expiration time of 5 seconds
$client->set('tempkey', 'This is temporary', 'EX', 5);

// Get the key before it expires
$tempValue = $client->get('tempkey');
echo $tempValue; // Output: This is temporary

// Sleep for 6 seconds to allow the key to expire
sleep(6);

// Attempt to get the key after it has expired
$tempValue = $client->get('tempkey');
echo $tempValue; // Output: (null)
?>

These examples cover some basic operations you can perform with Redis using PHP and the Predis library. You can explore more advanced features and commands of Redis as needed by referring to the Predis documentation and the Redis command reference.