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.
Requires
- php: ^8.0
- predis/predis: ^2.3
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.