Last active
October 9, 2017 21:11
-
-
Save akovalyov/3297ed2a071b34f6c27be7c51cea66b4 to your computer and use it in GitHub Desktop.
PHP Exporter to native Redis protocol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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