Redis
Setup Redis for PHP using Docker
Section titled “Setup Redis for PHP using Docker”- Update the Dockerfile
RUN pecl install redisRUN docker-php-ext-enable redis
- Update the
composer.json
file
{ "require": { "ext-redis": "*" } }
- Usage example
- Hello world
$redis = new \Redis(['host' => 'tls://my-domain.com','port' => 1234,'auth' => ['default', 'password']]);$redis->set('test', 'Hello world!');$data = $redis->get('test')echo $data // Hello world!;
- Create a new user with a password and limit access to GET command and keys matching a pattern
$redis->acl('SETUSER','myusername','on','>defineAPassword','~pattern*','+GET');
Redis commands
Section titled “Redis commands”Create a new user
Section titled “Create a new user”ACL SETUSER myusername on >defineAPassword ~pattern* +GET
List users
Section titled “List users”ACL USERS
Get user information
Section titled “Get user information”ACL GETUSER myusername
Set a value
Section titled “Set a value”SET my_key my_value
Get a value
Section titled “Get a value”GET my_key
GET all values by pattern in PHP and have the result as an associative array (value is a JSON string)
Section titled “GET all values by pattern in PHP and have the result as an associative array (value is a JSON string)”$redisKeys = $redis->keys('sport_centers:*');$redis->multi(\Redis::PIPELINE);foreach ($redisKeys as $key) { $redis->get($key);}$values = $redis->exec();
$sportCenters = array_combine($redisKeys, array_map(static fn(string $value) => json_decode($value, true, 512, JSON_THROW_ON_ERROR), $values));