# RedisBundle
## About ##
This bundle integrates [Predis](https://github.com/nrk/predis), [PhpRedis](https://github.com/nicolasff/phpredis) and [Relay](https://relay.so/) into your Symfony application.
## Installation ##
Add the `snc/redis-bundle` package to your `require` section in the `composer.json` file.
``` bash
$ composer require snc/redis-bundle
```
If you want to use the `predis` client library, you have to add the `predis/predis` package, too.
``` bash
$ composer require predis/predis
```
Add the RedisBundle to your application's kernel:
``` php
<?php
public function registerBundles()
{
$bundles = [
// ...
new Snc\RedisBundle\SncRedisBundle(),
// ...
];
...
}
```
## Usage ##
Configure the `redis` client(s) in your `config.yml`:
_Please note that passwords with special characters in the DSN string such as `@ % : +` must be urlencoded._
``` yaml
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
```
You have to configure at least one client. In the above example your service
container will contain the service `snc_redis.default` which will return a
`Predis` client.
Available types are `predis`, `phpredis` and `relay`.
A more complex setup which contains a clustered client could look like this:
``` yaml
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
logging: '%kernel.debug%'
cache:
type: predis
alias: cache
dsn: redis://secret@localhost/1
options:
connection_timeout: 10
read_write_timeout: 30
cluster:
type: predis
alias: cluster
dsn:
- redis://localhost/3?weight=10
- redis://localhost/4?weight=5
- redis://localhost/5?weight=1
```
In your code you can now access all your configured clients using dependency
injection or service locators. The services are named `snc_redis.` followed by
the alias name, ie. `snc_redis.default` or `snc_redis.cluster` in the example
above.
A setup using `predis` master-slave replication could look like this:
``` yaml
snc_redis:
clients:
default:
type: predis
alias: default
dsn:
- redis://master-host?role=master
- redis://slave-host1
- redis://slave-host2
options:
replication: predis
```
Please note that the master dsn connection needs to be tagged with the ```master``` role.
If not, `predis` will complain.
A setup using `predis`, `phpredis` or `relay` sentinel replication could look like this:
``` yaml
snc_redis:
clients:
default:
type: "phpredis" # or "predis", or "relay"
alias: default
dsn:
- redis://localhost:26379
- redis://otherhost:26379
options:
replication: sentinel
service: mymaster
parameters:
database: 1
password: pass
sentinel_username: myuser # default to null
sentinel_password: mypass # default to null
```
The `service` is the name of the set of Redis instances.
The optional parameters option can be used to set parameters like the
database number and password for the master/slave connections,
they don't apply for the connection to sentinel.
If you use a password, it must be in the password parameter and must
be omitted from the DSNs. Also make sure to use the sentinel port number
(26379 by default) in the DSNs, and not the default Redis port.
You can find more information about this on [Configuring Sentinel](https://redis.io/topics/sentinel#configuring-sentinel).
A setup using `RedisCluster` from `phpredis` could look like this:
``` yaml
snc_redis:
clients:
default:
type: phpredis
alias: default
dsn:
- redis://localhost:7000
- redis://localhost:7001
- redis://localhost:7002
options:
cluster: true
```
#### Authentication using Redis ACL
Starting with redis 6.0, it is possible to use an [ACL](https://redis.io/docs/manual/security/acl/) system that only allows users with valid username and password to log in.
Using the `phpredis` driver, you can set up an authenticated connection like this:
``` yaml
snc_redis:
clients:
default:
type: phpredis
alias: default
dsn: redis://localhost
# dsn: redis://my_username:my_password@localhost <- username and password can be also set here
options:
parameters:
username: my_userame
password: my_password
```
### Sessions ###
Use Redis sessions by utilizing Symfony built-in Redis session handler like so:
First, define your redis clients:
``` yaml
snc_redis:
clients:
session:
type: predis
alias: session
dsn: redis://localhost/1
```
Then, reference it in your framework.yaml config:
``` yaml
framework:
...
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler
services:
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments: ['@snc_redis.session']
```
Note that this solution does not perform session locking and that you may face race conditions when accessing sessions (see [Symfony docs](https://symfony.com/doc/current/session/database.html#store-sessions-in-a-key-value-database-redis)).
### Monolog logging ###
You can store your logs in a redis `LIST` by adding this to your config:
``` yaml
snc_redis:
clients:
monolog:
type: predis
alias: monolog
dsn: redis://localhost/1
logging: false
options:
connection_persistent: true
monolog:
client: monolog
key: monolog
monolog:
handlers:
main:
type: service
id: snc_redis.monolog.handler
level: debug
```
You can also add a custom formatter to the monolog handler
``` yaml
snc_redis:
clients:
monolog:
type: predis
alias: monolog
dsn: redis://localhost/1
logging: false
options:
connection_persistent: true
monolog:
client: monolog
key: monolog
formatter: my_custom_formatter
```
### Symfony Cache Pools ###
If you want to use one of the client connections for the Symfony App Cache or a Symfony Cache Pool, just use its service name as a cache pool provider:
```yaml
framework:
cache:
app: cache.adapter.redis
# app cache from client config as default adapter/provider
default_redis_provider: snc_redis.default
pools:
some-pool.cache:
adapter: cache.adapter.redis
# a specific provider, e.g. if you have a snc_redis.clients.cache
provider: snc_redis.cache
```
### Complete configuration example ###
``` yaml
snc_redis:
clients:
default:
type: predis
alias: default
dsn: redis://localhost
logging: '%kernel.debug%'
cache:
type: predis
alias: cache
dsn: redis://localhost/1
logging: false
cluster:
type: predis
alias: cluster
dsn:
- redis://127.0.0.1/1
- redis://127.0.0.2/2
- redis://pw@/var/run/redis/redis-1.sock/10
- redis://[email protected]:63790/10
options:
prefix: foo
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
RedisBundle 关于该软件包将Predis和PhpRedis集成到您的 Symfony 4.4+ 应用程序中,为Redis提供快速便捷的接口。建议使用原生 PhpRedis 扩展,因为它速度更快,也是我们的主要开发平台。如果扩展不可用且无法在您的环境中安装,Predis 被认为是一种安全且可移植的替代方案,并且我们的集成在功能上应该相同。安装使用作曲家composer require snc/redis-bundle文档阅读 docs/ 中的文档贡献运行完整的测试套件需要安装 PHP、某些 PHP 扩展和 redis 服务器,以及overmind来启动 redis 进程群。因此,我们使用Nix进行本地开发。安装 Nix后,请确保你位于 SncRedisBundle 目录中。在其中,你可以运行nix shell安装并进入开发环境。进入后,您可以运行composer update # install php package dependenciesovermind start & # start redis fl
资源推荐
资源详情
资源评论





























收起资源包目录














































共 28 条
- 1
资源评论


赵闪闪168
- 粉丝: 1746
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 机房网络安全隐患及网络安全技术和对策的应用分析.docx
- 《福建专业技术人员继续教育信息化能力建设》在线测验考试参考答案(简化版).doc
- 企业档案信息化建设探究.docx
- VMware-Horizon-View7桌面虚拟化部署图文教程.docx
- 2015年中级通信工程师考试综合真题(标准答案)...doc
- 产万水泥粉磨生产线项目管理工程.doc
- 新时期医院人事档案管理信息化建设路径研究.docx
- 基于 Yolov5 算法的目标检测技术研究与应用
- 校园网络系统设计方案.doc
- 汇编实现交通灯控制模拟程序设计.doc
- 以创新创业能力培养为核心的计算机类公共选修课课程教学改革.docx
- 【大学本科设计】PLC的变频调速恒压供水系统自动化等专业.doc
- 加工产品现场检查项目管理.doc
- 单片机多功能电子钟研究报告.doc
- android天气预报系统设计方案.docx
- 并行计算概述-云计算.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
