m6web / redis-bundle

本包最新版本(v7.0.1)没有提供许可证信息。

基于 predis 的 bundle

安装次数: 337,276

依赖关系: 0

建议者: 1

安全: 0

星标: 8

关注者: 48

分支: 9

开放问题: 0

类型:symfony-bundle

v7.0.1 2022-05-23 06:43 UTC

README

Build Status Latest Stable Version Total Downloads License PHP Version Require

基于 predis 的 symfony Bundle

查看 predis/predis

特性

  • 语义化配置
  • sf 事件调度器集成
  • 带有 redis 存储的会话处理程序: M6Web\Bundle\RedisBundle\Redis\RedisSessionHandler
  • guzzle 缓存用 redis 适配器: M6Web\Bundle\RedisBundle\Guzzle\RedisCacheAdapter
  • sf2 网页分析工具栏的数据收集器

用法

配置

config.yml 中为简单的缓存服务配置

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

对于多个客户端

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, GET, MGET and MSET are supported
        sharded:
            servers: ["first", "second"]
            prefix: raaaoul\
            timeout:   1

$this->get('m6web_redis') 发送默认客户端。 this->get('m6web_redis.sharded') 发送分片客户端。

服务器配置中的选项列表

  • host:Redis 的 IP 地址或主机名。
  • port:Redis 监听的 CP 端口。默认值 6379
  • database:数据库索引(参见 SELECT 命令)。
  • scheme:连接方案,例如 'tcp' 或 'unix'。默认值 tcp
  • async_connect:异步执行 connect() 操作。默认值 false
  • persistent:在 GC 收集后保持连接打开。默认值 false
  • timeout:connect() 操作的超时时间。默认值 10
  • read_write_timeout:read() 和 write() 操作的超时时间
  • reconnect:如果 Redis 命令失败,重新连接尝试的次数,仅适用于 tcp
m6web_redis:
    servers:
        server1:
            host:   'localhost'
            port: 6379

通过通配符进行服务器配置

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

事件调度器

当执行命令时,会自动分发 M6Web\Bundle\RedisBundle\EventDispatcher\RedisEvent 事件。事件以 redis.command 标签触发。

您可以通过客户端配置自定义事件名称

m6web_redis:
   clients:
       default:
           eventname: myEventName

会话处理程序

# 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

数据收集器

当启用 Symfony 分析器时,数据收集器可用。收集器允许您查看以下 Redis 数据

  • 命令名称
  • 执行时间
  • 命令参数

覆盖基类

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

guzzle redis 缓存适配器

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

启动单元测试

bin/atoum

启动 php cs

    make cs-ci
    make cs-fix