mjohann/simple-redis

Simple Redis is a PHP class that provides a practical and reusable abstraction for working with Redis. It allows you to easily create connections and perform common operations such as set, get, del, list, pub, sub and many others, without worrying about the underlying configuration complexity.

v1.1.0 2025-04-21 09:35 UTC

This package is auto-updated.

Last update: 2025-04-21 09:43:05 UTC


README

SimpleRedis is a PHP class that offers a practical and reusable abstraction for working with Redis. It allows you to easily establish connections and perform common operations, as outlined in the features section β€” all without the need to worry about the underlying configuration complexity.

πŸ“¦ Installation

You can install the library via Packagist/Composer:

composer require mjohann/simple-redis

βš™οΈ Requirements

  • PHP 8.0 or higher

πŸš€ Features

  • SimpleRedis uses predis/predis as a dependency
  • Supported:
    • set
    • get
    • del
    • list
    • pub
    • sub

πŸ§ͺ Usage Example

<?php

require_once "vendor/autoload.php";

use MJohann\Packlib\SimpleRedis;

// Create and configure a Redis connection
$redis = new SimpleRedis();
$redis->open();

// SET: Store the key "key" with the value "value" and a TTL (Time To Live) of 60 seconds
echo "Set: ";
var_dump($redis->set("key", "value", 60));
echo PHP_EOL, PHP_EOL;

// GET: Retrieve the value associated with the key "key"
echo "Get: ";
var_dump($redis->get("key"));
echo PHP_EOL, PHP_EOL;

// Wait 10 seconds before deleting the key
sleep(10);

// DEL: Delete the key "key" from Redis
echo "Del: ";
var_dump($redis->del("key"));
echo PHP_EOL, PHP_EOL;

// Define the key for the RPUSH list
$keyListRPUSH = "list:names_RPUSH";

// RPUSH: Add elements to the end (right) of the list
echo "List RPUSH: ";
var_dump($redis->listPush($keyListRPUSH, "Matheus", "r"));
echo PHP_EOL, PHP_EOL;

echo "List RPUSH: ";
var_dump($redis->listPush($keyListRPUSH, "Johann", "r"));
echo PHP_EOL, PHP_EOL;

echo "List RPUSH: ";
var_dump($redis->listPush($keyListRPUSH, "AraΓΊjo", "r"));
echo PHP_EOL, PHP_EOL;

// Update the value at index 0 in the list
echo "List set: ";
var_dump($redis->listSet($keyListRPUSH, 0, "Matheus Johann"));
echo PHP_EOL, PHP_EOL;

// Wait 5 seconds
sleep(5);

// Remove the element "Johann" from the list
echo "List remove: ";
var_dump($redis->listRemove($keyListRPUSH, "Johann"));
echo PHP_EOL;

// Define the key for the LPUSH list
$keyList = "list:names_LPUSH";

// LPUSH: Add elements to the beginning (left) of the list
echo "List LPUSH: ",
var_dump($redis->listPush($keyList, "Matheus"));
echo PHP_EOL, PHP_EOL;

echo "List LPUSH: ",
var_dump($redis->listPush($keyList, "Johann"));
echo PHP_EOL, PHP_EOL;

echo "List LPUSH: ",
var_dump($redis->listPush($keyList, "AraΓΊjo"));
echo PHP_EOL, PHP_EOL;

// LLEN: Get the total number of elements in the list
echo "List size: ";
var_dump($redis->listSize($keyList));
echo PHP_EOL, PHP_EOL;

// LINDEX: Retrieve the element at index 0
echo "List index [0]: ";
var_dump($redis->listIndex($keyList, 0));
echo PHP_EOL, PHP_EOL;

// LRANGE: Retrieve all elements from the list
echo "List all: ";
var_dump($redis->listAll($keyList));
echo PHP_EOL;

// Wait 5 seconds
sleep(5);

// LPOP: Remove and print one element from the beginning of the list every 5 seconds
echo "Popping list items..." . PHP_EOL;
while (($value = $redis->listPop($keyList)) !== null) {
    var_dump($value);
    echo PHP_EOL, PHP_EOL;
    sleep(5);
}

// Delete the entire list
echo "Del: ";
var_dump($redis->del($keyListRPUSH));
echo PHP_EOL, PHP_EOL;

// Close the Redis connection
$redis->close();

For more examples, see the example/ file in the repository.

πŸ“ Project Structure

simple-redis/
β”œβ”€β”€ src/
β”‚   └── SimpleRedis.php
β”‚   └── Facades/
β”‚       └── SimpleRedis.php
β”œβ”€β”€ example/
β”‚   └── docker-compose.yml
β”‚   └── script.php
β”‚   └── sub.php
β”‚   └── pub.php
β”œβ”€β”€ composer.json
β”œβ”€β”€ .gitignore
β”œβ”€β”€ LICENSE
└── README.md

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more information.

πŸ‘¨β€πŸ’» Author

Developed by Matheus Johann AraΓΊjo – Pernambuco, Brazil.