Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. Cache
  5. Redis Cache Adapter
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Configure the Connection
  • Configure the Options
    • Available Options

Redis Cache Adapter

Edit this page

Warning: You are browsing the documentation for Symfony 4.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Redis Cache Adapter

See also

This article explains how to configure the Redis adapter when using the Cache as an independent component in any PHP application. Read the Symfony Cache configuration article if you are using it in a Symfony application.

This adapter stores the values in-memory using one (or more) Redis server instances.

Unlike the APCu adapter, and similarly to the Memcached adapter, it is not limited to the current server's shared memory; you can store contents independent of your PHP environment. The ability to utilize a cluster of servers to provide redundancy and/or fail-over is also available.

Caution

Requirements: At least one Redis server must be installed and running to use this adapter. Additionally, this adapter requires a compatible extension or library that implements \Redis, \RedisArray, RedisCluster, or \Predis.

This adapter expects a Redis, RedisArray, RedisCluster, or Predis instance to be passed as the first parameter. A namespace and default cache lifetime can optionally be passed as the second and third parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Cache\Adapter\RedisAdapter;

$cache = new RedisAdapter(

    // the object that stores a valid connection to your Redis system
    \Redis $redisConnection,

    // the string prefixed to the keys of the items stored in this cache
    $namespace = '',

    // the default lifetime (in seconds) for cache items that do not define their
    // own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
    // until RedisAdapter::clear() is invoked or the server(s) are purged)
    $defaultLifetime = 0
);

Configure the Connection

The createConnection() helper method allows creating and configuring the Redis client class instance using a Data Source Name (DSN):

1
2
3
4
5
6
use Symfony\Component\Cache\Adapter\RedisAdapter;

// pass a single DSN string to register a single server with the client
$client = RedisAdapter::createConnection(
    'redis://localhost'
);

The DSN can specify either an IP/host (and an optional port) or a socket path, as well as a password and a database index.

Note

A Data Source Name (DSN) for this adapter must use the following format.

1
redis://[pass@][ip|host|socket[:port]][/db-index]

Below are common examples of valid DSNs showing a combination of available values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use Symfony\Component\Cache\Adapter\RedisAdapter;

// host "my.server.com" and port "6379"
RedisAdapter::createConnection('redis://my.server.com:6379');

// host "my.server.com" and port "6379" and database index "20"
RedisAdapter::createConnection('redis://my.server.com:6379/20');

// host "localhost", auth "abcdef" and timeout 5 seconds
RedisAdapter::createConnection('redis://abcdef@localhost?timeout=5');

// socket "/var/run/redis.sock" and auth "bad-pass"
RedisAdapter::createConnection('redis://bad-pass@/var/run/redis.sock');

// a single DSN can define multiple servers using the following syntax:
// host[hostname-or-IP:port] (where port is optional). Sockets must include a trailing ':'
RedisAdapter::createConnection(
    'redis:?host[localhost]&host[localhost:6379]&host[/var/run/redis.sock:]&auth=my-password&redis_cluster=1'
);

Redis Sentinel, which provides high availability for Redis, is also supported when using the Predis library. Use the redis_sentinel parameter to set the name of your service group:

1
2
3
RedisAdapter::createConnection(
    'redis:?host[redis1:26379]&host[redis2:26379]&host[redis3:26379]&redis_sentinel=mymaster'
);

4.2

The option to define multiple servers in a single DSN was introduced in Symfony 4.2.

Note

See the RedisTrait for more options you can pass as DSN parameters.

Configure the Options

The createConnection() helper method also accepts an array of options as its second argument. The expected format is an associative array of key => value pairs representing option names and their respective values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use Symfony\Component\Cache\Adapter\RedisAdapter;

$client = RedisAdapter::createConnection(

    // provide a string dsn
    'redis://localhost:6379',

    // associative array of configuration options
    [
        'compression' => true,
        'lazy' => false,
        'persistent' => 0,
        'persistent_id' => null,
        'tcp_keepalive' => 0,
        'timeout' => 30,
        'read_timeout' => 0,
        'retry_interval' => 0,
    ]

);

Available Options

class (type: string)
Specifies the connection library to return, either \Redis or \Predis\Client. If none is specified, it will return \Redis if the redis extension is available, and \Predis\Client otherwise.
compression (type: bool, default: true)
Enables or disables compression of items. This requires phpredis v4 or higher with LZF support enabled.
lazy (type: bool, default: false)
Enables or disables lazy connections to the backend. It's false by default when using this as a stand-alone component and true by default when using it inside a Symfony application.
persistent (type: int, default: 0)
Enables or disables use of persistent connections. A value of 0 disables persistent connections, and a value of 1 enables them.
persistent_id (type: string|null, default: null)
Specifies the persistent id string to use for a persistent connection.
read_timeout (type: int, default: 0)
Specifies the time (in seconds) used when performing read operations on the underlying network resource before the operation times out.
retry_interval (type: int, default: 0)
Specifies the delay (in milliseconds) between reconnection attempts in case the client loses connection with the server.
tcp_keepalive (type: int, default: 0)
Specifies the TCP-keepalive timeout (in seconds) of the connection. This requires phpredis v4 or higher and a TCP-keepalive enabled server.
timeout (type: int, default: 30)
Specifies the time (in seconds) used to connect to a Redis server before the connection attempt times out.

Note

When using the Predis library some additional Predis-specific options are available. Reference the Predis Connection Parameters documentation for more information.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Michael Squires, a Symfony contributor

    Thanks Michael Squires for being a Symfony contributor

    1 commit • 2 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch