kamnev / laravel-redis-helper
Laravel Redis Helper package of Predis\Predis
dev-main
2021-06-28 12:34 UTC
Requires
- php: ^7.3|^8.0
- ext-json: *
- illuminate/redis: ^8.48
- predis/predis: ^1.1
Requires (Dev)
- orchestra/testbench: v6.13.0
This package is auto-updated.
Last update: 2024-04-29 04:27:55 UTC
README
Laravel Redis Helper package of Predis\Predis
Use Redis more effectively with Laravel!
Installation
composer require kamnev/laravel-redis-helper
Laravel
Add from .env redis settings
REDIS_HOST=your_redis_host REDIS_PASSWORD=your_redis_password REDIS_PORT=your_redis_port REDIS_CLIENT=predis
Lumen
Add from .env redis settings
REDIS_HOST=your_redis_host REDIS_PASSWORD=your_redis_password REDIS_PORT=your_redis_port REDIS_CLIENT=predis
Register it in bootstrap/app.php
$app->register(Illuminate\Redis\RedisServiceProvider::class);
Usage
You can now using the Facades:
RedisString
working with strings in Redis:
- Method
set
takes 4 parametersset(string $key, string $value, string $option = "KEEPTTL", int $time = 600)
$option
supports a set of options that modify its behavior: "EX" or "EXAT" or "PXAT" or "NX" or "XX" or "KEEPTTL" or "GET". - Method
get
have 1 required parameterkey
and 1 optionaldecode
if you set json (default = false).
//Set string value in cache RedisString::set('products', json_encode(Product::all()), 'EX', 6000); //Get decoded products RedisString::get('products', true);
RedisHash
working with hash in Redis:
- Method
getAll
- get all values of key. If you set models collection in json, then you can decoded passing the second parametertrue
(default = false), the fird parameter convert to array or object StdClass return Collection - Method
getAllVal
- get all values. - Method
getAllKeys
- get all keys. - Method
get
- Get value on key. The first parameter is key, the second parameter is field(maybe model id), the third parameter is decoding json data (default = false). - Method
set
- Set the string value of a hash field. - Method
del
- Del one or more hash fields. The second parameter can be string or array.
//Get get all values of key RedisHash::getAll('products', true, true); //Get all values. RedisHash::getAllValue('products', true, true); //Get all keys. RedisHash::getAllKeys('products'); //Get value on key RedisHash::get('products', 2, true, false); //Set the string value of a hash field RedisHash::set('products', 2, json_encode(Product::find(3))); //Del one or more hash fields RedisHash::del('products', [1,2,3,4, etc..]));