Skip to content

Commit 9d0c6f8

Browse files
authored
Merge pull request #95 from niden/master
Correction in the article regarding view cache
2 parents 93275f2 + f48a6cd commit 9d0c6f8

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

data/posts/2017/03/2017-03-03-building-the-new-phalcon-website-middleware-part-3.md

+21-17
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,28 @@ public function call(Micro $application)
209209
$registry = $application->registry;
210210
$viewName = $registry->view;
211211

212-
if ('production' === $application->config->get('env')) {
212+
if ('production' === $application->config->get('app')->get('env')) {
213213
$application->viewSimple->cache(['key' => $cacheKey]);
214214
}
215215

216-
$application->viewSimple->setVars(
217-
[
218-
'page' => $registry->slug,
219-
'language' => $registry->language,
220-
'imageLanguage' => $registry->imageLanguage,
221-
'contributors' => $registry->contributors,
222-
'languages' => $registry->menuLanguages,
223-
'noindex' => $registry->noindex,
224-
'releases' => $registry->releases,
225-
'version' => $registry->version,
226-
]
227-
);
228-
229-
$contents = $application->viewSimple->render($viewName);
216+
if (true === $application->viewCache->exists($cacheKey)) {
217+
$contents = $application->viewCache->get($cacheKey);
218+
} else {
219+
$application->viewSimple->setVars(
220+
[
221+
'page' => $registry->slug,
222+
'language' => $registry->language,
223+
'imageLanguage' => $registry->imageLanguage,
224+
'contributors' => $registry->contributors,
225+
'languages' => $registry->menuLanguages,
226+
'noindex' => $registry->noindex,
227+
'releases' => $registry->releases,
228+
'version' => $registry->version,
229+
]
230+
);
231+
232+
$contents = $application->viewSimple->render($viewName);
233+
}
230234
$application->response->setContent($contents);
231235
$application->response->send();
232236

@@ -236,9 +240,9 @@ public function call(Micro $application)
236240

237241
We first check where we are. Using a simple `str_replace`, we create a unique file name based on the route, so that we can have a cache file name (or key depending on your cache adapter).
238242

239-
We then check if we are in production mode (set in our `.env` file) and if so, we invoke the cache for the view.
243+
We then check if we are in production mode (set in our `.env` file) and if so, we invoke the cache for the view. If the data has been cached we use that.
240244

241-
Several variables are being sent to the view, which have originally been set in our `EnvironmentMiddleware` or other areas of the site. We then render the view, set the response contents and send the response back.
245+
If we do not have a cache hit, several variables are being sent to the view, which have originally been set in our `EnvironmentMiddleware` or other areas of the site. We then render the view, set the response contents and send the response back.
242246

243247
### CLI Application
244248

0 commit comments

Comments
 (0)