How to use redis in Symfony?

Member

by larissa , in category: PHP Frameworks , 2 years ago

How to use redis in Symfony?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by herminia_bruen , 2 years ago

@larissa 

To use Redis in a Symfony project, you'll need to install the php-redis extension and the symfony/redis-pack package.

  1. Install the php-redis extension:
1
sudo apt-get install php-redis


  1. Install the symfony/redis-pack package using Composer:
1
composer require symfony/redis-pack


  1. Configure your Redis server in config/packages/redis.yaml:
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. Create a service for Redis and inject it into your controller or service:
 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:

  • Redis in Symfony
  • Predis

Member

by orpha , a year ago

@larissa 

To use Redis in Symfony, you need to follow these steps:

  1. Install Redis and the PHP Redis extension if you haven't already. You can install Redis using the package manager for your operating system, and the PHP Redis extension can be installed using the pecl command.
  2. Install the Redis bundle for Symfony using the Composer package manager. Open your terminal and navigate to your Symfony project directory, and run the following command: composer require symfony/cache
  3. Configure the Redis connection in your Symfony project. Open the config/packages/framework.yaml file and add the following configuration under the framework.cache section: framework: cache: app: cache.adapter.redis default_redis_provider: "redis://localhost"
  4. You can now use Redis as a caching provider in your Symfony application. Inject the SymfonyComponentCacheAdapterRedisAdapter class into your services, and use it to cache data. For example, in a controller: use SymfonyComponentCacheAdapterRedisAdapter; use SymfonyContractsCacheItemInterface; // ... public function index(RedisAdapter $redisCache) { $cachedItem = $redisCache->getItem('my_key'); if (!$cachedItem->isHit()) { // Data not found in cache, fetch and set it $data = $this->fetchData(); $cachedItem->set($data); $redisCache->save($cachedItem); } else { // Data found in cache, retrieve it $data = $cachedItem->get(); } // ... Use the data }


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.