Developed with love by KnpLabs Hire us for your project!
29

RedisBundle

by M6Web

Symfony2 Bundle over predis

RedisBundle Build Status

symfony2 Bundle on top of predis

see predis/predis

features

  • semantic configuration
  • sf2 event dispatcher integration
  • session handler with redis storage : M6Web\Bundle\RedisBundle\Redis\RedisSessionHandler
  • redis adapter for guzzle cache : M6Web\Bundle\RedisBundle\Guzzle\RedisCacheAdapter
  • dataCollector for sf2 web profiler toolbar

usage

configuration

in config.yml for a simple cache service :

m6web_redis:
    servers:
        default:
            host:   'localhost'
            port: 6379
            reconnect: 1
    clients:
        default:
            servers:   ["default"]     # list of servers to use
            prefix:    raoul\          # prefix to use
            timeout:   2               # timeout in second
            read_write_timeout: 2      # read-write timeout in second

for a multiple clients :

m6web_redis:
    servers:
        first:
            host:   'localhost'
            port: 6379
            reconnect: 1
        second:
            host:   'xxxxxxxx'
    clients:
        default:
            servers:   ["first"]     # list of servers to use
            prefix: raoul\           # prefix to use
            timeout:   2             # timeout in second (float)
            read_write_timeout: 1.2  # read write timeout in seconds (float)
            compress: true           # compress/uncompress data sent/retrieved from redis using gzip, only method SET, SETEX, SETNX and GET are supported
        sharded:
            servers: ["first", "second"]
            prefix: raaaoul\
            timeout:   1

$this->get('m6web_redis') send the default client. this->get('m6web_redis.sharded') the sharded one.

list of options in servers configuration

  • host: IP address or hostname of Redis.
  • port: CP port on which Redis is listening to. Default value 6379
  • database: Database index (see the SELECT command).
  • scheme: Connection scheme, such as 'tcp' or 'unix'. Default value tcp
  • async_connect: Performs the connect() operation asynchronously. Default value false
  • persistent: Leaves the connection open after a GC collection. Default value false
  • timeout: Timeout for the connect() operation. Default value 10
  • read_write_timeout: Timeout for read() and write() operations
  • reconnect: Number of reconnection attempt if a redis command fail, only for tcp
m6web_redis:
    servers:
        server1:
            host:   'localhost'
            port: 6379

server configuration via wildcard

m6web_redis:
    servers:
        server1:
            host:   'localhost'
            port: 6379
        server2:
            host:   'xxxxxxxx'
    clients:
        default:
            servers:   ["server*"]     # all servers matching server*
            prefix: raoul\
            timeout:   2

event dispatcher

The event M6Web\Bundle\RedisBundle\EventDispatcher\RedisEvent is automaticly dispatched when a command is executed. Events are fired with the redis.command label.

You can customize the event name through the client configuration :

 m6web_redis:
    clients:
        default:
            eventname: myEventName

session handler

# app/config/config.yml
framework:
  session:
    # ...
    handler_id: session.handler.redis

m6web_redis:
  servers:
    first:
      ip: 'localhost'
      port: 6379
  clients:
    sessions:
      servers: ["first"]
      prefix: sessions\
      timeout: 1

services:
  session.handler.redis:
    class: M6Web\Bundle\RedisBundle\Redis\RedisSessionHandler
    public:    false
    arguments:
      - '@m6web_redis.sessions'
      - 3600

dataCollector

Datacollector is available when the Symfony profiler is enabled. The collector allow you to see the following Redis data:

  • Command name
  • Execution time
  • Command arguments

overwriting base class

m6web_redis:
    clients:
        default:
            servers: ["first"]
            type: ["db"]
            timeout: 0.5
            class: \MyCompany\Redis

guzzle redis cache adapter

m6web_redis:
  clients:
    guzzlehttp:
      servers: ["first"]
      prefix: GuzzleHttp\
      class: M6Web\Bundle\RedisBundle\CacheAdapters\M6WebGuzzleHttp
      timeout: 1

Launch Unit Tests

bin/atoum

Launch php cs

    make cs-ci
    make cs-fix
Copyright (c) 2014 M6Web

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • Merge pull request #49 from M6Web/fix/connection-one-server
    By web-flow, 1 year ago
  • fix connection type when only one server
    By Héléna Hiraux, 1 year ago
  • remove return type as it can be object or int (#48)
    By web-flow, 1 year ago
  • fix doc for reconnect (#45)
    By web-flow, 1 year ago
  • add reset function (#47)
    By web-flow, 1 year ago
  • fix default configuration type (#46)
    By pabedu, 1 year ago
  • add symfony 4 (#44)
    By web-flow, 1 year ago
  • Merge pull request #41 from M6Web/feature/use-predis
    By web-flow, 1 year ago
  • use predis directly
    By Héléna Hiraux, 1 year ago
  • Merge pull request #39 from M6Web/feature/add-redis-cache-item-pool-interface
    By web-flow, 2 years ago
  • remove php 5 compatibility and add php 7 to travis
    By Héléna Hiraux, 2 years ago
  • add redis cache item pool adapter
    By Héléna Hiraux, 2 years ago
  • Merge pull request #38 from M6Web/feature/doc-compress
    By web-flow, 2 years ago
  • add documentation for "compress" option
    By web-flow, 2 years ago
  • Merge pull request #36 from M6Web/feature/maj-redis-component
    By web-flow, 2 years ago
  • Merge pull request #37 from COil/patch-1
    By web-flow, 2 years ago
  • doc: Missing quotes on service
    By web-flow, 2 years ago
  • allow the user to customize the event name per client
    By omansour, 2 years ago
  • cache_reseter is no longer availble
    By omansour, 2 years ago
  • Merge pull request #35 from M6Web/feature/symfony-3-compatibilities
    By cedvan, 3 years ago
  • Symfony 3 compatibilities
    By Cédric Vanet, 3 years ago
  • Merge pull request #34 from agallou/doc_session
    By omansour, 4 years ago
  • add documentation to use session handler with RedisBundle
    By agallou, 4 years ago
  • Improve code formating in README
    By mikaelrandy, 4 years ago
  • Merge pull request #33 from M6Web/feature/guzzlehttp-adapter
    By mojoLyon, 4 years ago
  • Add adapter for M6WebGuzzleHttp bundle
    By mojoLyon, 4 years ago
  • removed option
    By omansour, 4 years ago
  • Merge pull request #30 from M6Web/feature/redis-component-3
    By omansour, 4 years ago
  • allow php 5.4
    By omansour, 4 years ago
  • Merge branch 'master' into feature/redis-component-3
    By omansour, 4 years ago