perlamutr/php-rdb-parser

Redis RDB file parser in pure PHP

0.0.1 2019-11-26 17:36 UTC

This package is auto-updated.

Last update: 2024-10-27 04:33:33 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP Implementation of Redis RDB parser

Installation

Use composer :

composer require perlamutr/php-rdb-parser

Usage

All you need is two objects: ReaderFile to read from rdb-file and Parser to fetch its keys and values

<?php
use Perlamutr\Reader\ReaderFile;
use Perlamutr\Parser;

$reader = new ReaderFile('filename.rdb');
$parser = new Parser($reader);
$generator = $parser->parseRDB();

foreach ($generator as $key => $value) {
    if (is_object($key)) {
        //  if parser meets command it returns $key as object of type Command
        continue;
    }
    //  Otherwise it contains the key
    echo "Key = '$key'\n";
    //  And value is a Generator with key-value pairs (or single value)
    foreach ($value as $k => $v) {
        echo "\t$k => $v\n";
    }
}

If may call setSkipData before parseRDB method with true argument. Parser will skip as much as he can and will return Generator with keys and additional information for each of them such as wasted bytes, type of key and file position

<?php
use Perlamutr\Reader\ReaderFile;
use Perlamutr\Parser;

$reader = new ReaderFile('filename.rdb');
$parser = new Parser($reader);
$parser->setSkipData(true);
$generator = $parser->parseRDB();
foreach ($generator as $key => $value) {
    //  $key is always string with key name
    echo "Key = '$key'\tType = '{$value['type']}\tBytes = '{$value['skip']}'\tPosition = '{$value['position']}'\n";    
}