@larissa
To use Redis in a Symfony project, you'll need to install the php-redis
extension and the symfony/redis-pack
package.
1
|
sudo apt-get install php-redis
|
1
|
composer require symfony/redis-pack |
1 2 3 4 5 6 7 |
# config/packages/redis.yaml redis: client: 'predis' connection: # host, port, database, password - { host: 'localhost', port: 6379, database: 0 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// src/Service/MyService.php namespace AppService; use SymfonyComponentCacheAdapterRedisAdapter; class MyService { private $redis; public function __construct(RedisAdapter $redis) { $this->redis = $redis; } // ... } |
You can then use the $redis
service to interact with the Redis server.
1 2 |
$this->redis->set('key', 'value'); $value = $this->redis->get('key'); |
For more information, you can check out the official documentation:
@larissa
To use Redis in Symfony, you need to follow these steps:
That's it! You have now configured and used Redis as the caching provider in your Symfony project. You can also use other features of Redis, such as pub/sub messaging or data storage, by using the Redis extension directly.