sallyx/redis-php-stream-wrapper
Composer 安装命令:
composer require sallyx/redis-php-stream-wrapper
包简介
Implementation of streamWrapper using redis
README 文档
README
This package allows you to register redis server as php stream wrapper, so you will be able to use redis as as stream resource, i.e. 'redis://foo.txt'
Install
1. Install phpredis/phpredis
See phpredis/phpredis.
2. Install packages by composer
$ composer require sallyx/redis-php-stream-wrapper
Setup
All together
use Sallyx\StreamWrappers\Redis\ConnectorConfig; use Sallyx\StreamWrappers\Redis\PathTranslator; use Sallyx\StreamWrappers\Redis\Connector; use Sallyx\StreamWrappers\Redis\FileSystem; use Sallyx\StreamWrappers\Wrapper; $config = new ConnectorConfig; $translator = new PathTranslator('www.sallyx.org'); $connector = new Connector($config, $translator); $fs = new FileSystem($connector); Wrapper::register($fs);
Step by step
1. Create configuration
use Sallyx\StreamWrappers\Redis\ConnectorConfig; $config = new ConnectorConfig( // all parameters are optional '127.0.0.1', $port = 6379, $timeout = 0, $persistent_id = NULL, $retry_interval = NULL ); $config = new ConnectorConfig('/tmp/redis.sock'); // socket connection
2. Create path translator
use Sallyx\StreamWrappers\Redis\PathTranslator; $translator = new PathTranslator($prefix = 'www.example.org');
Prefix is used for keys in redis server. For example file 'redis://foo.txt' will be saved in redis under key 'www.example.org://foo.txt'.
3. Create connector
use Sallyx\StreamWrappers\Redis\Connector; $connector = new Connector($config, $translator);
4. Create redis file system
use Sallyx\StreamWrappers\Redis\FileSystem; $fs = new FileSystem($connector);
5. Register as stream wrapper
use Sallyx\StreamWrappers\Wrapper; Wrapper::register($fs,'redis');
redis is a scheme name of the wrapper ('redis:// ...')
6. Profit
mkdir('redis://foo'); file_put_contents('redis://foo/bar.txt', 'hello world'); echo file_get_contents('redis://foo/bar.txt'); ...
Using with Nette
If you do not know Nette, have a look at www.nette.org or skip this block :)
First put setup into app/bootstrap.php or anywhere before you want to use redis stream wrapper. After that you can use redis. For example for temp directory:
use Sallyx\StreamWrappers\Redis\Connector; use Sallyx\StreamWrappers\Redis\ConnectorConfig; use Sallyx\StreamWrappers\Redis\PathTranslator; use Sallyx\StreamWrappers\Redis\FileSystem; use Sallyx\StreamWrappers\Wrapper; $cc = new ConnectorConfig(); $con = new Connector($cc, new PathTranslator('www.example.org')); Wrapper::register(new FileSystem($co)); //... $configurator->enableDebugger('redis://log'); $configurator->setTempDirectory('redis://temp'); //...
Optionally, you can use StreamWrappersExtension in app/config/config.local.neon, which show diagnostic panel in debugger bar.
extension:
streamWrappers: Sallyx\Bridges\StreamWrappers\Nette\DI\StreamWrappersExtension
Now you could see your redis filesystem in the panel:
Known issues
If your PHP script ends unexpectedly, all locked files stay locked forever. You can unlock them in redis by this command:
HMSET www.example.org://foo/bar.txt lock_ex 0 lock_sh 0
Access rights are not supported (yet?). Functions like chmod(), chgrp(), chown() return always false.
Calling file_put_contents() with LOCK_EX option triggers E_WARNING "Exclusive locks may only be set for regular files" (This is a PHP bug)
sallyx/redis-php-stream-wrapper 适用场景与选型建议
sallyx/redis-php-stream-wrapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sallyx/redis-php-stream-wrapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sallyx/redis-php-stream-wrapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 44
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GNU
- 更新时间: 2015-11-01
