sallyx/redis-php-stream-wrapper

使用 redis 实现的 streamWrapper

v0.9.5 2016-03-27 10:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:00:09 UTC


README

Build Status Latest Stable Version License

此包允许您将 redis 服务器注册为 php 流封装器,这样您就可以将 redis 用作流资源,例如 'redis://foo.txt'

安装

1. 安装 phpredis/phpredis

请参阅 phpredis/phpredis

2. 使用 composer 安装包

$ composer require sallyx/redis-php-stream-wrapper

设置

全部

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);

逐步

1. 创建配置

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. 创建路径转换器

use Sallyx\StreamWrappers\Redis\PathTranslator;
$translator = new PathTranslator($prefix = 'www.example.org');

前缀用于 redis 服务器中的键。例如,文件 'redis://foo.txt' 将被保存在 redis 中的键 'www.example.org://foo.txt'。

3. 创建连接器

use Sallyx\StreamWrappers\Redis\Connector;
$connector = new Connector($config, $translator);

4. 创建 redis 文件系统

use Sallyx\StreamWrappers\Redis\FileSystem;
$fs = new FileSystem($connector);

5. 注册为流封装器

use Sallyx\StreamWrappers\Wrapper;
Wrapper::register($fs,'redis');

redis 是封装器的方案名称('redis:// ...')

6. 完成

mkdir('redis://foo');
file_put_contents('redis://foo/bar.txt', 'hello world');
echo file_get_contents('redis://foo/bar.txt');
...

与 Nette 一起使用

如果您不知道 Nette,请查看 www.nette.org 或跳过此部分 :)

首先,将 设置 放入 app/bootstrap.php 或您想要使用 redis 流封装器之前的地方。之后,您就可以使用 redis。例如,对于临时目录

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');
//...

可选地,您可以在 app/config/config.local.neon 中使用 StreamWrappersExtension,在调试器栏中显示诊断面板。

extension:
        streamWrappers: Sallyx\Bridges\StreamWrappers\Nette\DI\StreamWrappersExtension

现在您可以在面板中看到您的 redis 文件系统

diagnostic panel

已知问题

如果您的 PHP 脚本意外终止,所有被锁定的文件将永远锁定。您可以通过此命令在 redis 中解锁它们

HMSET www.example.org://foo/bar.txt lock_ex 0 lock_sh 0

不支持(尚不支持)访问权限。像 chmod()、chgrp()、chown() 这样的函数总是返回 false。

使用 LOCK_EX 选项调用 file_put_contents() 触发 E_WARNING "Exclusive locks may only be set for regular files"(这是一个 PHP bug