X-Cart allows to cache certain widgets that contain mostly (or entirely) static data. This way X-Cart will not need to render this widget from scratch and the store will work faster.
This article explains developers how they can enable widget cache for their custom widgets.
Imagine a situation that we have added a custom sidebar widget. We know that this widget never changes, that is why we can generate its content (HTML code) only once and then insert this HTML code without running PHP process. Let us mark this widget as available for cache.
Before we get started install the module from here.
In order to mark a widget as available for caching you should simply add the
isCacheAvailable()to your viewer class as follows:
protected function isCacheAvailable()
If you want to add some logic whether or not enable caching depending on some condition (e.g. cache widget on home page only and do not cache them on category page), then you can also add
isCacheAllowed()method. Example of its implementation:
protected function isCacheAllowed($template)
// we must call parent's method for backward compatibility
// this way we make sure that we are on some category page. home page is a category page for X-Cart
&& \XLite::getController() instanceof \XLite\Controller\Customer\Category
// if category's ID is root category ID, then we are on home page
&& \XLite::getController()->getCategoryId() == \XLite::getController()->getRootCategoryId();
That is it. Now it is time to re-deploy the store and check the results.
Important: widget's cache works only if you enable the Use view cache option in the System Settings > Look & feel > Performance section in admin area.
If you feel that your widget changes quite often – for instance, you display the last registered customer's info – this approach will not work, because cached widget will always display static content and it will be dynamically updated only if you drop widget's cache.
- use-view-cache.png (image/png)