Thinking Piece of Cake 🍰

Today I’m ready to add a feature which is caching HOOLAY.cn index page . You know that web page service powered by Phalcon .

Why it ? Maybe Phalcon is highly decoupled ? C extension ? Full Stack framework ? …balala

Actually i just want to solve my problem . Aha . so i did these code as below .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public function indexAction()
{
$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
$cachePageExists = $this->view->getCache()->exists(IAppCacheKeysManager::KEY_VIEW_CACHE_HOME);
if (!$cachePageExists) {
// do sth
}
// cache page
$this->view->cache([
'key' => IAppCacheKeysManager::KEY_VIEW_CACHE_HOME,
]);
}

Are you kidding me ? It’s not working , but official docs as it is ! So my first thought is debugging by Breakpoints to know cache how to work .
You know i can’t do this . These all be compiled ! Or any ?Trace tools could help me ? No . Read the Fucking Source !

Absolutely i need to check out View Component. Then i found something .
phalcon/mvc/view.zep

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function cache(var options = true) -> <View>
{
var viewOptions, cacheOptions, key, value, cacheLevel;
if fetch cacheLevel, cacheOptions["level"] {
let this->_cacheLevel = cacheLevel;
} else {
let this->_cacheLevel = self::LEVEL_MAIN_LAYOUT;
}
}
protected function _engineRender(engines, string viewPath, bool ...) {
/* codes */
if renderLevel >= cacheLevel {
if cache->isStarted() == false {
let cachedView = cache->start(key, lifetime);
}
}
/* codes */
}

You can see pivotal code above . There is a level option . We did not settle it & if renderLevel < cacheLevel Cache won’t be started ! So we need set cacheLevel less than or equal renderLevel . Codes as below :

1
2
3
4
5
// cache page
$this->view->cache([
'key' => IAppCacheKeysManager::KEY_VIEW_CACHE_HOME,
'level' => View::LEVEL_ACTION_VIEW,
]);

Yep . The problem was solved and an issue was associated 🍰 . But something not finished for me . I drawn a diagram about web application request flow :

/images/web_request.png

PS:oh… Where is “middleware” ? Everything is “middleware” when it on a line ? That Is Really Important ?

I am thinking the Phalcon View Component how to render & cache these view fragments . So i got these fake code (Just Logic) below .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$view->start();
$view->render() {
$view->_engineRender() {
$cache->start(); // turn on output buffering
$engineTemplate->render() {
require $compiledTemplate; // output to buffering
$view->setContent(ob_get_content());
}
}
$cache->save(); // save output buffering
$cache->stop(); // stop collect buffer content
}
$view->finish();
$response->setConent($view->getContent());
$response->send();

Done . That’s all :)