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'
See phpredis/phpredis.
$ 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);
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
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'.
use Sallyx\StreamWrappers\Redis\Connector;
$connector = new Connector($config, $translator);
use Sallyx\StreamWrappers\Redis\FileSystem;
$fs = new FileSystem($connector);
use Sallyx\StreamWrappers\Wrapper;
Wrapper::register($fs,'redis');
redis is a scheme name of the wrapper ('redis:// ...')
mkdir('redis://foo');
file_put_contents('redis://foo/bar.txt', 'hello world');
echo file_get_contents('redis://foo/bar.txt');
...
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:
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)