https://github.com/matheusjohannaraujo/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.
https://github.com/matheusjohannaraujo/simple-redis
docker key-value list php pubsub redis redis-pubsub redis-queue redis-server
Last synced: 5 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/matheusjohannaraujo/simple-redis
- Owner: matheusjohannaraujo
- License: mit
- Created: 2023-02-21T21:36:45.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-21T09:43:02.000Z (over 1 year ago)
- Last Synced: 2025-05-07T16:16:30.636Z (about 1 year ago)
- Topics: docker, key-value, list, php, pubsub, redis, redis-pubsub, redis-queue, redis-server
- Language: PHP
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [Simple Redis](https://github.com/matheusjohannaraujo/simple-redis)
**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](https://packagist.org/packages/mjohann/simple-redis):
```bash
composer require mjohann/simple-redis
```
## βοΈ Requirements
- PHP 8.0 or higher
## π Features
- SimpleRedis uses [`predis/predis`](https://packagist.org/packages/predis/predis) as a dependency
- Supported:
- `set`
- `get`
- `del`
- `list`
- `pub`
- `sub`
## π§ͺ Usage Example
```php
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/`](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](LICENSE) file for more information.
## π¨βπ» Author
Developed by [Matheus Johann AraΓΊjo](https://github.com/matheusjohannaraujo) β Pernambuco, Brazil.