Skip to content

Redis

  1. Update the Dockerfile
RUN pecl install redis
RUN docker-php-ext-enable redis
  1. Update the composer.json file
{
"require": {
"ext-redis": "*"
}
}
  1. 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'
    );
ACL SETUSER myusername on >defineAPassword ~pattern* +GET
ACL USERS
ACL GETUSER myusername
SET my_key my_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));