Skip to content

Commit

Permalink
server: Make sync lazy
Browse files Browse the repository at this point in the history
Use streams to save memory

Useful when there is not much space.
  • Loading branch information
jtojnar committed Sep 12, 2020
1 parent 23227f1 commit 208534d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"type": "project",
"require": {
"php": ">= 5.6",
"ext-gd": "*",
"bcosca/fatfree-core": "^3.7.0",
"danielstjules/stringy": "^3.1",
"fossar/tcpdf-parser": "^6.2",
"fossar/guzzle-transcoder": "^0.2.0",
"fossar/htmlawed": "^1.2.4.1",
"fossar/tcpdf-parser": "^6.2",
"guzzlehttp/guzzle": "^6.3",
"guzzlehttp/oauth-subscriber": "^0.4.0",
"guzzlehttp/psr7": "^1.5",
"fossar/htmlawed": "^1.2.4.1",
"http-interop/response-sender": "^1.0",
"j0k3r/graby": "2.0.0-alpha.0",
"level-2/dice": "^2.0",
"lordelph/icofileloader": "^2.0",
Expand All @@ -20,8 +22,8 @@
"php-http/guzzle6-adapter": "^1.0",
"simplepie/simplepie": "^1.3",
"smottt/wideimage": "^1.1",
"willwashburn/phpamo": "^1.0",
"ext-gd": "*"
"violet/streaming-json-encoder": "^1.1",
"willwashburn/phpamo": "^1.0"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.0",
Expand Down
94 changes: 93 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/controllers/Items/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Base;
use helpers\Authentication;
use function helpers\json_response;
use helpers\View;
use helpers\ViewHelper;

Expand Down Expand Up @@ -115,7 +116,9 @@ public function sync(Base $f3) {
}
}

$this->view->jsonSuccess($sync);
$encoder = new \Violet\StreamingJsonEncoder\BufferJsonEncoder($sync);
$stream = new \Violet\StreamingJsonEncoder\JsonStream($encoder);
$this->view->sendResponse(json_response($stream));
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/helpers/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace helpers;

use function Http\Response\send;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

/**
* Helper class for rendering template
*
Expand Down Expand Up @@ -120,4 +124,22 @@ public function jsonSuccess($data) {
header('Content-type: application/json');
die(json_encode($data));
}

/**
* Send a PSR-7 response.
*
* @return void
*/
public function sendResponse(ResponseInterface $response) {
send($response);
die;
}
}

function json_response(StreamInterface $stream) {
$response = new \GuzzleHttp\Psr7\Response();
$response = $response->withHeader('Content-type', 'application/json');
$response = $response->withBody($stream);

return $response;
}

0 comments on commit 208534d

Please sign in to comment.