PrestaShop Forum

The best place in the world to ask questions about PrestaShop and get advice from our passionate community!

PrestaShop Forum

Jump to content

[MODULE (modified)] Cached Categories - HUGE Speed Increase

63 replies to this topic
#1
RNBCards

    PrestaShop Newbie

  • Members
  • Pip
  • 10 posts
Alright, so my shop was running pretty slow, borderline acceptable, and it is on a VPS. Anyways, after clocking the page generation time (top and bottom of index.php), it came out to 1.5-1.9 seconds to generate the page. Anyways, to cut the story short, I found out the category block was EXTREMELY slow. With it disabled, page generation times dropped to a consistent .31 seconds - over FIVE TIMES FASTER. Then again, my shop has about 100-120 categories, so it might not be as drastic for other people.

Ok, so I know nothing of smarty, however I am good with PHP. And it appears (I think) that the reason it is so slow is because it opens category-tree-branch.tpl again for each separate category when populating the block.

Anyways, attached is a mod of the categories block renamed to "blockcategoriescache". It should be completely independent (renamed files, changed module name, module variable names, etc) so you can just upload and test. Also, as I said I don't know smarty, this is very primitive. You have to update the cache manually every time you change/update your category list. Basically, this SHOULD work fine, but I make no guarantees.

Also, to clock the page generation time to see if there is an increase, add 2 lines in index.php. Change it from this:
<?php
include(dirname(__FILE__).'/config/config.inc.php');

if(intval(Configuration::get('PS_REWRITING_SETTINGS')) === 1)
$rewrited_url = __PS_BASE_URI__;

include(dirname(__FILE__).'/header.php');

$smarty->assign('HOOK_HOME', Module::hookExec('home'));
$smarty->display(_PS_THEME_DIR_.'index.tpl');

include(dirname(__FILE__).'/footer.php');
?>

to
<?php
$dwTimeStart = microtime(true);
include(dirname(__FILE__).'/config/config.inc.php');

if(intval(Configuration::get('PS_REWRITING_SETTINGS')) === 1)
$rewrited_url = __PS_BASE_URI__;

include(dirname(__FILE__).'/header.php');

$smarty->assign('HOOK_HOME', Module::hookExec('home'));
$smarty->display(_PS_THEME_DIR_.'index.tpl');

include(dirname(__FILE__).'/footer.php');
echo(round(microtime(true) - $dwTimeStart, 3) . "s");
?>


Whenever you load the main page, it will display the seconds it took to generate the page on the bottom.

Installation Instructions:
1.) OPTIONAL - Change the index page as indicated above so you can see performance impact.
2.) OPTIONAL - Load main page 3 times and average page generation times
3.) Download attachment, extract
4.) Upload "blockcategoriescache" to www.YOUR-SITE.com/modules/blockcategoriescache
5.) On modules list in prestashop backoffice, uninstall "Categories block" and install "Categories block (Cache)"
6.) Update cache - goto www.YOUR-SITE.com/?debug=cat (You will need to do this each time you update categories)
7.) Load page and see speed increase
8.) OPTIONAL - Load main page 3 times and average page generation times. Post old and new times on this thread.

Anyone very familiar with modules PLEASE look at this to make sure everything is done nicely and stable. Also, I would REALLY appreciate it if people would post old and new page load times here.

Oh, and if you have any issues with it, post here and I will try my best to help.

Thanks!

Attached Files



#2
RNBCards

    PrestaShop Newbie

  • Members
  • Pip
  • 10 posts
17 downloads no responses? Does that mean it is working? I spent about an hour on this so the least spend 3 seconds and post if it worked good or even to just say thanks.

#3
silencespr

    PrestaShop Apprentice

  • Members
  • PipPip
  • 37 posts
thx i will try this when i get a chance.

#4
RNBCards

    PrestaShop Newbie

  • Members
  • Pip
  • 10 posts

From 1286295360:

thx i will try this when i get a chance.


Alright, if you get a chance, use the code above to time it to see the actual increase and post here.

Thanks

#5
My IT Guy

    PrestaShop Newbie

  • Members
  • Pip
  • 9 posts
Wow! I had *just* come to the same conclusion and was going to write my own hack - thanks for doing the leg work!

#6
RNBCards

    PrestaShop Newbie

  • Members
  • Pip
  • 10 posts

From 1286351817:

Wow! I had *just* come to the same conclusion and was going to write my own hack - thanks for doing the leg work!


No problem. If you would like to do me a favor, time it to see the increase. I want to know the affect on other's shops.

#7
sors

    PrestaShop Apprentice

  • Moderators
  • 464 posts
I use caching in smarty template engine. This is example for the manufacturers block
In smarty.config.inc.php
$smarty->force_compile    = false;

In blockmanufacturer.php

function hookLeftColumn($params)
{
global $smarty, $link;
//caching on
$smarty->caching = true;
//check if in cache
if (!$smarty->is_cached(dirname(__FILE__).'/'.'blockmanufacturer.tpl')) {
//query date if in not cache
$smarty->assign(array(
'manufacturers' => Manufacturer::getManufacturers(),
'link' => $link,
));
}
//generating block
$page= $this->display(__FILE__, 'blockmanufacturer.tpl');
//caching off
$smarty->caching = false;
//return block
return $page;
}


This smarty caching work in sitemap.php, category.php and other static blocks and pages

http://www.prestadev.../tema-1068.html

#8
RNBCards

    PrestaShop Newbie

  • Members
  • Pip
  • 10 posts

From 1286433246:

I use caching in smarty template engine. This is example for the manufacturers block
In smarty.config.inc.php
$smarty->force_compile    = false;

In blockmanufacturer.php

function hookLeftColumn($params)
{
global $smarty, $link;
//caching on
$smarty->caching = true;
//check if in cache
if (!$smarty->is_cached(dirname(__FILE__).'/'.'blockmanufacturer.tpl')) {
//query date if in not cache
$smarty->assign(array(
'manufacturers' => Manufacturer::getManufacturers(),
'link' => $link,
));
}
//generating block
$page= $this->display(__FILE__, 'blockmanufacturer.tpl');
//caching off
$smarty->caching = false;
//return block
return $page;
}


This smarty caching work in sitemap.php, category.php and other static blocks and pages

http://www.prestadev.../tema-1068.html


Yea, but the trouble is the category block is not static. For ex. when you expand a category, and click a subcategory, on the new page the category will be expanded. It expands all parent categories of the current one. You can use caching, but I'm fairly certain you won't be able to get it to auto expand.

#9
sors

    PrestaShop Apprentice

  • Moderators
  • 464 posts
In blockmanufacturer.php After
$page= $this->display(__FILE__, 'blockmanufacturer.tpl');

Add

if (isset($_GET['id_category']))
{
$cookie->last_visited_category = intval($_GET['id_category']);
$currentCategoryId=intval($_GET['id_category']);
}
if (isset($_GET['id_product']))
{
if (!isset($cookie->last_visited_category) OR !Product::idIsOnCategoryId(intval($_GET['id_product']), array('0' => array('id_category' => $cookie->last_visited_category))))
{
$product = new Product(intval($_GET['id_product']));
if (isset($product) AND Validate::isLoadedObject($product))
$cookie->last_visited_category = intval($product->id_category_default);
}
$currentCategoryId=intval($cookie->last_visited_category);
}
$page.="[removed]$('#cat_".$currentCategoryId."').addClass('selected');[removed]";

And in category-tree-branch.tpl replace

  • with


  • #10
    gdinari

      PrestaShop Apprentice

    • Members
    • PipPip
    • 50 posts
    Hey great module. I tried step 6, but it didnt create the cache for the module.

    The funny thing is that I tried it on my local server and it work, so I just updated the cache file from the local to the live one.

    This is how I inputted step 6:

    http://www.mysite.ne...site/?debug=cat

    But this didnt work. Any ideas?


    Thanks

    gdinari

    #11
    Crisp

      PrestaShop Apprentice

    • Members
    • PipPip
    • 34 posts
    I have a little language problem with the headertext of the block. I want to change the name Categories to something else but I can't find where to change it. Any idea? Besides that it works perfect and loads 1.5 secs faster for me then the 'old' block.

    #12
    RNBCards

      PrestaShop Newbie

    • Members
    • Pip
    • 10 posts

    From 1287138603:

    I have a little language problem with the headertext of the block. I want to change the name Categories to something else but I can't find where to change it. Any idea? Besides that it works perfect and loads 1.5 secs faster for me then the 'old' block.


    Edit blockcategoriescache.tpl.

    Change "

    {l s='Categories' mod='blockcategoriescache'}

    "
    to "

    {l s='Name Goes Here' mod='blockcategoriescache'}

    "


    From 1287080363:

    Hey great module. I tried step 6, but it didnt create the cache for the module.

    The funny thing is that I tried it on my local server and it work, so I just updated the cache file from the local to the live one.

    This is how I inputted step 6:

    http://www.mysite.ne...site/?debug=cat

    But this didnt work. Any ideas?


    Thanks

    gdinari


    Check "modules/blockcategoriescache" and see if there is a file named "cat_cache.txt". If not, you could also try changing the permissions of the blockcategoriescache folder. If that doesn't work, post here.

    #13
    RNBCards

      PrestaShop Newbie

    • Members
    • Pip
    • 10 posts

    From 1286763155:

    In blockmanufacturer.php After
    $page= $this->display(__FILE__, 'blockmanufacturer.tpl');

    Add

    if (isset($_GET['id_category']))
    {
    $cookie->last_visited_category = intval($_GET['id_category']);
    $currentCategoryId=intval($_GET['id_category']);
    }
    if (isset($_GET['id_product']))
    {
    if (!isset($cookie->last_visited_category) OR !Product::idIsOnCategoryId(intval($_GET['id_product']), array('0' => array('id_category' => $cookie->last_visited_category))))
    {
    $product = new Product(intval($_GET['id_product']));
    if (isset($product) AND Validate::isLoadedObject($product))
    $cookie->last_visited_category = intval($product->id_category_default);
    }
    $currentCategoryId=intval($cookie->last_visited_category);
    }
    $page.="[removed]$('#cat_".$currentCategoryId."').addClass('selected');[removed]";

    And in category-tree-branch.tpl replace

  • with


  • Yea, that would also work. It's just two different ways of going about it. However I still like my way more because you just drop it in, and in the worst case scenario you just disable the module. When possible, I like to keep away from editing core files. Also, in case of prestashop updates, you don't need to remember each and every change you made.

    #14
    Crisp

      PrestaShop Apprentice

    • Members
    • PipPip
    • 34 posts
    thx for that. I found another problem though.

    The module works everywhere except with the "Sendtoafriend module".

    When clicking on the Send to a friend button, the form shows up but the Cached Category Block dissapears and a message appears "PLEASE UPDATE
    CATEGORY CACHE! ".

    I updated the cache before and trying it again doesnt work with the sendtoafriend module.

    Any ideas?

    #15
    hphilg

      PrestaShop Apprentice

    • Members
    • PipPip
    • 36 posts
    Thanks !
    This module can support multilang website ?

    For my website, it only works for one language.

    THanks for your help

    #16
    Willem

      PrestaShop Apprentice

    • Members
    • PipPip
    • 63 posts
    Speed in the old situation was 3.678 s and now it 0.587 s

    Thanx Willem

    #17
    svetaines1

      PrestaShop Apprentice

    • Members
    • PipPip
    • 85 posts
    Dear RNBCards,

    Thank you for improved categories module!

    I think it's speed up my website also. Just one question regarding change in index.php file.
    I add that two lines of code like was written in your post, but load time only show up in home page.
    Is it possible to make it visible in whole website?

    Thank you in advance!

    #18
    Glimpy

      PrestaShop Newbie

    • Members
    • Pip
    • 17 posts
    Thanks for this!
    Often noticed loadtime in the high, but with this its atleast under 2sec. (also my current host is useless :/)

    3,588
    2,577
    1,584 pre-cache

    1,584
    1,549
    1,621 after-cache

    #19
    svetaines1

      PrestaShop Apprentice

    • Members
    • PipPip
    • 85 posts
    I have not big issue with that module. It works ok, but in page Send this page to a friend it's not working. I got such message:

    PLEASE UPDATE
    CATEGORY CACHE!


    Please, help me to figure out that problem, because that cached categories module really helps me improve my site load time

    #20
    Glimpy

      PrestaShop Newbie

    • Members
    • Pip
    • 17 posts
    Sad to say it, the Cache bugs like crazy.
    Keep getting the update cache error msg on like every payment option on checkout page etc etc..
    Have to disable this for now until its fixed :(