phpsymfonyazuredoctrine-ormredis

Using Redis on Azure with Symfony2


I am trying to connect Symfony2 with Redis server on Azure but can't figure out where to put the hostname, port and key and other details (if required which I am not aware of).

composer.json

"predis/predis": "1.*",
"snc/redis-bundle": "1.1.x-dev"

config.yml configured as per this tutorial

snc_redis:
  # configure predis as client
  clients:
      default:
          type: predis
          alias: default
          dsn: projectrdev.redis.cache.windows.net
      doctrine:
          type: predis
          alias: doctrine
          dsn: projectrdev.redis.cache.windows.net
  # configure doctrine caching
  doctrine:
      metadata_cache:
          client: doctrine
          entity_manager: default
          document_manager: default
      result_cache:
          client: doctrine
          entity_manager: [default]
      query_cache:
          client: doctrine
          entity_manager: default

As per Predis documentation clients can be configured but now sure how that works in Symfony environment.


Solution

  • First of all, we should Enable the non-SSL endpoint for PHP integration. enter image description here

    And you need to combine redis dsn connection string in the following format: redis://[password@]host[:port][/db]

    E.g, in the config.yml file in your symfony2 application, you can config like:

    snc_redis:
        clients:
            default:
                type: predis
                alias: default
                dsn: redis://{password}@{your_redis_name}.redis.cache.windows.net
    

    And have a test in a controller function:

    use Snc\RedisBundle\Doctrine\Cache\RedisCache;
    use Predis\Client;
    ...
    ...
    $redis = $this->container->get('snc_redis.default');
    $val = $redis->incr('foo:bar');
    echo $val;