diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-23 12:29:52 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-27 20:11:30 +0100 |
commit | c609944cb906a2f5002cd86a808aa36d8deb2afd (patch) | |
tree | bdac2c4ddffb524336582ba9a94f4fb0b2c952a0 | |
parent | 25e90d8d75382721ff7473fa1686090fcfeb46ff (diff) | |
download | Shaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.tar.gz Shaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.tar.zst Shaarli-c609944cb906a2f5002cd86a808aa36d8deb2afd.zip |
Bulk creation: improve performances using memoization
Reduced additional processing time per links from ~40ms to ~5ms
-rw-r--r-- | application/front/controller/admin/ShaarePublishController.php | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/application/front/controller/admin/ShaarePublishController.php b/application/front/controller/admin/ShaarePublishController.php index fd680ea0..65fdcdee 100644 --- a/application/front/controller/admin/ShaarePublishController.php +++ b/application/front/controller/admin/ShaarePublishController.php | |||
@@ -6,6 +6,7 @@ namespace Shaarli\Front\Controller\Admin; | |||
6 | 6 | ||
7 | use Shaarli\Bookmark\Bookmark; | 7 | use Shaarli\Bookmark\Bookmark; |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | 8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; |
9 | use Shaarli\Formatter\BookmarkFormatter; | ||
9 | use Shaarli\Formatter\BookmarkMarkdownFormatter; | 10 | use Shaarli\Formatter\BookmarkMarkdownFormatter; |
10 | use Shaarli\Render\TemplatePage; | 11 | use Shaarli\Render\TemplatePage; |
11 | use Shaarli\Thumbnailer; | 12 | use Shaarli\Thumbnailer; |
@@ -15,6 +16,16 @@ use Slim\Http\Response; | |||
15 | class ShaarePublishController extends ShaarliAdminController | 16 | class ShaarePublishController extends ShaarliAdminController |
16 | { | 17 | { |
17 | /** | 18 | /** |
19 | * @var BookmarkFormatter[] Statically cached instances of formatters | ||
20 | */ | ||
21 | protected $formatters = []; | ||
22 | |||
23 | /** | ||
24 | * @var array Statically cached bookmark's tags counts | ||
25 | */ | ||
26 | protected $tags; | ||
27 | |||
28 | /** | ||
18 | * GET /admin/shaare - Displays the bookmark form for creation. | 29 | * GET /admin/shaare - Displays the bookmark form for creation. |
19 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. | 30 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. |
20 | */ | 31 | */ |
@@ -72,7 +83,7 @@ class ShaarePublishController extends ShaarliAdminController | |||
72 | return $this->redirect($response, '/'); | 83 | return $this->redirect($response, '/'); |
73 | } | 84 | } |
74 | 85 | ||
75 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | 86 | $formatter = $this->getFormatter('raw'); |
76 | $link = $formatter->format($bookmark); | 87 | $link = $formatter->format($bookmark); |
77 | 88 | ||
78 | return $this->displayForm($link, false, $request, $response); | 89 | return $this->displayForm($link, false, $request, $response); |
@@ -110,7 +121,7 @@ class ShaarePublishController extends ShaarliAdminController | |||
110 | $this->container->bookmarkService->addOrSet($bookmark, false); | 121 | $this->container->bookmarkService->addOrSet($bookmark, false); |
111 | 122 | ||
112 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | 123 | // To preserve backward compatibility with 3rd parties, plugins still use arrays |
113 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | 124 | $formatter = $this->getFormatter('raw'); |
114 | $data = $formatter->format($bookmark); | 125 | $data = $formatter->format($bookmark); |
115 | $this->executePageHooks('save_link', $data); | 126 | $this->executePageHooks('save_link', $data); |
116 | 127 | ||
@@ -198,7 +209,7 @@ class ShaarePublishController extends ShaarliAdminController | |||
198 | ]; | 209 | ]; |
199 | } | 210 | } |
200 | 211 | ||
201 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | 212 | $formatter = $this->getFormatter('raw'); |
202 | $link = $formatter->format($bookmark); | 213 | $link = $formatter->format($bookmark); |
203 | $link['linkIsNew'] = false; | 214 | $link['linkIsNew'] = false; |
204 | 215 | ||
@@ -207,20 +218,43 @@ class ShaarePublishController extends ShaarliAdminController | |||
207 | 218 | ||
208 | protected function buildFormData(array $link, bool $isNew, Request $request): array | 219 | protected function buildFormData(array $link, bool $isNew, Request $request): array |
209 | { | 220 | { |
210 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); | ||
211 | if ($this->container->conf->get('formatter') === 'markdown') { | ||
212 | $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | ||
213 | } | ||
214 | |||
215 | return escape([ | 221 | return escape([ |
216 | 'link' => $link, | 222 | 'link' => $link, |
217 | 'link_is_new' => $isNew, | 223 | 'link_is_new' => $isNew, |
218 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', | 224 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', |
219 | 'source' => $request->getParam('source') ?? '', | 225 | 'source' => $request->getParam('source') ?? '', |
220 | 'tags' => $tags, | 226 | 'tags' => $this->getTags(), |
221 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), | 227 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), |
222 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), | 228 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), |
223 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), | 229 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), |
224 | ]); | 230 | ]); |
225 | } | 231 | } |
232 | |||
233 | /** | ||
234 | * Memoize formatterFactory->getFormatter() calls. | ||
235 | */ | ||
236 | protected function getFormatter(string $type): BookmarkFormatter | ||
237 | { | ||
238 | if (!array_key_exists($type, $this->formatters) || $this->formatters[$type] === null) { | ||
239 | $this->formatters[$type] = $this->container->formatterFactory->getFormatter($type); | ||
240 | } | ||
241 | |||
242 | return $this->formatters[$type]; | ||
243 | } | ||
244 | |||
245 | /** | ||
246 | * Memoize bookmarkService->bookmarksCountPerTag() calls. | ||
247 | */ | ||
248 | protected function getTags(): array | ||
249 | { | ||
250 | if ($this->tags === null) { | ||
251 | $this->tags = $this->container->bookmarkService->bookmarksCountPerTag(); | ||
252 | |||
253 | if ($this->container->conf->get('formatter') === 'markdown') { | ||
254 | $this->tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | ||
255 | } | ||
256 | } | ||
257 | |||
258 | return $this->tags; | ||
259 | } | ||
226 | } | 260 | } |