caching view fragments, and replace dynamic parts

I've two problems/question to view caching:

  • is it possible to cache only the action fragement and not the whole html? I've tried this (but it will cache everything...)

    $this->view->cache( array( 'key' => $cacheID, 'level' => \Phalcon\Mvc\View::LEVEL_ACTION_VIEW ) );

  • is it possible to replace some dynamic parts in cached content (something like votings)?

Thank you very much for your help

for #2 - a) use custom cache before and after or b) use javascript (rest or from footer)



2.1k

we're using ajax for some dynamic parts, but sometimes i has to be in html (eg starvoting for google)... I don't unsterstand how to cache before and after?

Something like that, or you can use partials

<?php  if (!cache->get() )  : ?>
put_this_inti_cache
<?php endif; ?>

dynamic part

<?php  if (!cache->get() )  : ?>
put_this_inti_cache
<?php endif; ?>


2.1k
edited Feb '19

Thank you for your example, but how i cache the view content?

If you have dynamic part then You should not cache complete View, but only partials instead (my example above). Cache in Phalcon means when something is rendered then it is written to Cache backend.

If You want to cache big view then You can make placeholders inside pre-cached view data and later replace with dynamic content, but it will consume a lot of CPU time. Javascript will do it much faster because of client CPU resources.

JS implementation can be:

  1. Cache header
  2. calculate variables and add Javascript KV array of calculated variables in the HEAD or footer
  3. dump cached view content (if exists of cause)
  4. add JS handlers on DOM page load to invoke KV-replacement on DOM level - <div id="#myVar"></div> or whatever You want to display.
  5. best part of it can be that variable mapping can have some kind of AJAX URIs so You can implement simple Ajax widgets also.

I am using this Ajax approach to integrate KnockoutJS forms/view-models. Phalcon View has nothing to do with PHP variables on request scope

  1. load form viewModel (my-form.ko.js)
  2. display form, without any <?php echo $this->view->var; ?>
  3. bind KnockoutJS library - var vm = new MyForm(); ko.applyBindings(). vm.loadById( ... );