Skip to content

Commit

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

Useful when there is not much memory and the articles are huge.
  • Loading branch information
jtojnar committed Sep 12, 2020
1 parent f72114e commit b3d8dd0
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 12 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"guzzlehttp/guzzle": "^6.3",
"guzzlehttp/oauth-subscriber": "^0.4.0",
"guzzlehttp/psr7": "^1.5",
"http-interop/response-sender": "^1.0",
"j0k3r/graby": "2.0.0-alpha.0",
"level-2/dice": "^2.0",
"lordelph/icofileloader": "^2.0",
Expand All @@ -21,6 +22,7 @@
"php-http/guzzle6-adapter": "^1.0",
"simplepie/simplepie": "^1.3",
"smottt/wideimage": "^1.1",
"violet/streaming-json-encoder": "^1.1",
"willwashburn/phpamo": "^1.0"
},
"require-dev": {
Expand Down
93 changes: 92 additions & 1 deletion composer.lock

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

21 changes: 10 additions & 11 deletions 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 @@ -86,16 +87,14 @@ public function sync(Base $f3) {
2 * $itemsHowMany);
}

$sync['newItems'] = [];
foreach ($this->itemsDao->sync($sinceId, $notBefore, $since, $itemsHowMany)
as $newItem) {
$sync['newItems'][] = ViewHelper::preprocessEntry($newItem, $this->tagsController);
}
if ($sync['newItems']) {
$sync['lastId'] = $this->itemsDao->lastId();
} else {
unset($sync['newItems']);
}
$sync['newItems'] = function() use ($sinceId, $notBefore, $since, $itemsHowMany) {
foreach ($this->itemsDao->sync($sinceId, $notBefore, $since, $itemsHowMany)
as $newItem) {
yield ViewHelper::preprocessEntry($newItem, $this->tagsController);
}
};

$sync['lastId'] = $this->itemsDao->lastId();
}
}

Expand All @@ -115,7 +114,7 @@ public function sync(Base $f3) {
}
}

$this->view->jsonSuccess($sync);
$this->view->sendResponse(json_response($sync));
}

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

namespace helpers;

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

/**
* Helper class for rendering template
*
Expand Down Expand Up @@ -104,4 +107,32 @@ 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;
}
}

/**
* Create a PSR-7 response for given JSON-encodable data.
*
* @param mixed $data
*
* @return ResponseInterface
*/
function json_response($data) {
$encoder = new \Violet\StreamingJsonEncoder\BufferJsonEncoder($data);
$stream = new \Violet\StreamingJsonEncoder\JsonStream($encoder);

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

return $response;
}

0 comments on commit b3d8dd0

Please sign in to comment.