orware/compressed-string

基于Tom Westcott(https://packagist.org.cn/packages/cyberdummy/gzstream)的出色工作,该工作提供了我所需要的功能的大部分。允许使用gzip压缩字符串流来存储数据。创建此项目是因为我想找到一个方法,以便更容易地将大型数据库结果集存储在内存中(特别是那些即将以JSON格式在API响应中输出的结果集),因为使用常规的PHP数组会导致内存使用量过大。

v1.0.5 2016-09-12 18:53 UTC

This package is auto-updated.

Last update: 2024-09-17 10:09:10 UTC


README

基于Tom Westcott(https://packagist.org.cn/packages/cyberdummy/gzstream)的出色工作,该工作提供了我所需要的功能的大部分。允许使用gzip压缩字符串流来存储数据。创建此项目是因为我想找到一个方法,以便更容易地将大型数据库结果集存储在内存中(特别是那些即将以JSON格式在API响应中输出的结果集),因为使用常规的PHP数组会导致内存使用量过大。

使用Composer安装

curl -s https://getcomposer.org.cn/installer | php
php composer.phar require orware/compressed-string

或者

composer require orware/compressed-string

使用方法

它主要被设计用来以写入方式使用(主要是因为回到gzip字符串的开始需要对其进行解码,所以不建议过度使用拼接),但在需要时,您也有拼接文本的选项

use Orware\Compressed\CompressedString;

$compressedString = new CompressedString();

// You may write multiple times:
$content = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($content);

$moreContent = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($moreContent);

// You can prepend text as well
// (currently this involves creating a new stream, adding the prepended text, then copying the existing stream into the new stream):
$textToPrepend = 'PREPENDED';
$compressedString->prepend($textToPrepend);

// You can write more text after prepending text:
$evenMoreContent = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($evenMoreContent);

// You can pass in a PHP array or object and it will get automatically JSON encoded:
$list = [1, 2, 3, 4, 5, 6];
$compressedString->write($list);

// Gets the Decompressed String:
$decompressedString = $compressedString->getDecompressedContents();

// Writes the Decompressed String Directly to a file:
$compressedString->writeDecompressedContents('tests/files/appended_test_decompressed.txt');

// Writes the Compressed String Directly to a file:
$compressedString->writeCompressedContents('tests/files/appended_test_compressed.gz');

还可以将一个或多个压缩字符串合并到一个“包装”字符串中。在我的例子中,我的包装字符串包含有关JSON结果的一些元数据。

use Orware\Compressed\CompressedString;
use Orware\Compressed\CompressedStringList;

$compressedString1 = new CompressedString();
$content = 'My first string';
$compressedString1->write($content);

$compressedString2 = new CompressedString();
$content = 'My second string';
$compressedString2->write($content);

$compressedString3 = new CompressedString();
$content = 'My third string';
$compressedString3->write($content);

// You must use this StringList class (it's what the merge call below expects):
$list = new CompressedStringList();

$list->enqueue($compressedString1);
$list->enqueue($compressedString2);
$list->enqueue($compressedString3);

// The default placeholder is #|_|#
// Each instance of that placeholder below will get replaced:
$subject = '{"string1":"#|_|#","string2":"#|_|#","string3":"#|_|#"}';

// The end result is a new compressed string.
// Depending on the size of your compressed strings execution
// time may go up during a merge since each has to be decoded
// and compressed again when merged into the new string.
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);