Skip to content

Instantly share code, notes, and snippets.

@akovalyov
Last active October 9, 2017 21:11
Show Gist options
  • Save akovalyov/3297ed2a071b34f6c27be7c51cea66b4 to your computer and use it in GitHub Desktop.
Save akovalyov/3297ed2a071b34f6c27be7c51cea66b4 to your computer and use it in GitHub Desktop.
PHP Exporter to native Redis protocol.
<?php
namespace App\Infra\Redis;
use Webmozart\Assert\Assert;
class NativeProtocolExporter
{
const NEWLINE = "\r\n";
/**
* @see https://redis.io/topics/mass-insert
*
* @return string
*/
public function __invoke(): string
{
/* @see https://github.com/redis/hiredis/issues/225#issuecomment-40100171 */
Assert::maxLength(func_num_args(), 1024 * 1024 + 1, '1048576 is maximum number of keys in buld insert');
$buf = '';
$buf .= '*'.func_num_args().self::NEWLINE;
foreach (func_get_args() as $arg) {
Assert::string($arg);
$buf .= '$'.strlen($arg).self::NEWLINE;
$buf .= $arg.self::NEWLINE;
}
return $buf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment