# 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
- 粉丝: 1745
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 新浪网的网络广告.ppt
- 基于51单片机的自动往返小车.doc
- 项目管理的发展、特点及其在我国的应用研究.doc
- 火车票管理系统C语言程序设计实训报告.docx
- 区工业信息化和商务局2021年工作总结及2022年重点工作安排.docx
- Coreldraw平面教学计划.pdf
- 卫浴企业网站策划方案概要.doc
- tpflow-PHP资源
- 网络监控小区设计及方案海康.doc
- 清华大学-Matlab-GUI设计.ppt
- 《项目管理》笔记.doc
- 最新国家开放大学电大《言语交际》网络核心课形考网考作业及答案.pdf
- 网络大学自我鉴定表范文.doc
- 智慧交通管理和服务平台系统技术推广方案.pdf
- 教师课堂ppt第八章薪酬管理MicrosoftPowerPoint.pptx
- 计算机协会招新总结.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



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