Magento Forum

Page 1 of 2
How does Magento cache system work ? 
 
jsperri
Sr. Member
 
Total Posts:  126
Joined:  2007-08-31
Fistufle
 

Hello,

Moshe, when you have a minute, could you please give us some details on how the cache system works in Magento ?
For a category/product/home page, which parts of the page are actually cached ?

Let’s assume I have activated the cache for all the settings in the cache control admin, how much does the cache actually improve the load time for a specific page, considering this page has already been loaded previously ? Can we expect zero SQL queries to be executed for the page (apart from the visitor logging queries) ?

I have checked the var/cache directory on my setup and found just a few files, even if my catalog is quite big (thousands active products), and did get visits on hundreds of products after a cache refresh, how comes I don’t get more cached items ?

Thank you,

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Hi, currently the caching is implemented in core components mostly.

* Configuration
Here we cache merged config.xml files from app/etc/, all the modules and custom configuration saved in the database.

* Layouts
Compiling layout updates from app/design/[package]/[theme]/layout/*.xml files into layouts cache for each page

* Blocks HTML output
Every block can be cacheable by setting cache_lifetime and cache_key.
This could involve pretty sophisticated logic to avoid representation inconsistencies between different blocks.
Currently only admin top navigation block is cached.

* EAV types and attributes
EAV (entity-attribute-value model) requires configuration to be loaded from database. To speed up the initialization we cache this configuration.

* Translations
Every module and every theme can supply it’s own translation files (currently .csv) We cache all of them to avoid wasting time on recompilation.

You could play with setCacheLifetime and setCacheKey for blocks that display product data and see how it works for you smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
jsperri
Sr. Member
 
Total Posts:  126
Joined:  2007-08-31
Fistufle
 

Thanks for that, I’ll give it a try

 
Magento Community Magento Community
Magento Community
Magento Community
 
yogabasics
Jr. Member
 
Total Posts:  19
Joined:  2008-04-07
 

Hi Moshe,

This sounds interesting “Every block can be cacheable by setting cache_lifetime and cache_key.” Where do I find these settings or make these changes?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Adam_Shuy
Member
 
Avatar
Total Posts:  51
Joined:  2008-02-20
San Jose, CA
 

Is there a way to schedule a cache refresh in Magento?

 
Magento Community Magento Community
Magento Community
Magento Community
 
perteweb
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-07-24
 

If you want to clean cache in the code, you only have to write:
Mage::app()->getCache()->clean ;

 
Magento Community Magento Community
Magento Community
Magento Community
 
finch8243
Sr. Member
 
Total Posts:  79
Joined:  2007-08-31
 

@perteweb—does this also refresh the Catalog Rewrites, Images Cache, and Layered Nav Indices (i.e., everything in the lower box on the /system_cache/ admin page)?  If not, any idea how to clean those out with either a cron or line of code?

 
Magento Community Magento Community
Magento Community
Magento Community
 
zadpro
Sr. Member
 
Avatar
Total Posts:  247
Joined:  2007-12-10
FL, USA
 

Did any body figure out or have any idea how to refresh the cache, specially, layered navigation and image with a cron command?

 
Magento Community Magento Community
Magento Community
Magento Community
 
finch8243
Sr. Member
 
Total Posts:  79
Joined:  2007-08-31
 

I did.  Someone from Varien please correct me if I’m wrong, but I think you can run this as a cron—just comment out the parts you don’t want to clear out:

// you need this in a file to start up Magento
$mageFilename '/{{YOUR PATH TO MAGENTO}}/app/Mage.php';
require_once 
$mageFilename;
umask(0);
Mage::run();

// clean overall cache
Mage::app()->cleanCache();

// clear 'refresh_catalog_rewrites':
Mage::getSingleton('catalog/url')->refreshRewrites();
echo 
'Catalog Rewrites was refreshed succesfuly<br>';

//  clear 'clear_images_cache':
Mage::getModel('catalog/product_image')->clearCache();
echo 
'Image cache was cleared succesfuly<br>';

//  clear 'refresh_layered_navigation':
Mage::getSingleton('catalogindex/indexer')->plainReindex();
echo 
'Layered Navigation Indices was refreshed succesfuly<br>';
 
Magento Community Magento Community
Magento Community
Magento Community
 
zadpro
Sr. Member
 
Avatar
Total Posts:  247
Joined:  2007-12-10
FL, USA
 

Thanks for your reply, but with the above code I get the following error message:

<br />
<
b>Fatal error</b>:  Uncaught exception 'Exception' with message 'Notice: Undefined index:  SCRIPT_NAME  in /home/XXXX/public_html/app/Mage.php on line 574' in /home/XXXX/public_html/app/code/core/Mage/Core/functions.php:240
Stack trace
:
#0 /home/XXXX/public_html/app/Mage.php(574): mageCoreErrorHandler(8, 'Undefined index...', '/home/XXXX/...', 574, Array)
#1 /home/XXXX/public_html/app/Mage.php(432): Mage::printException(Object(Zend_Controller_Response_Exception))
#2 /home/XXXX/public_html/var/import/process/clear_cache.php(6): Mage::run()
#3 {main}
  
thrown in <b>/home/XXXX/public_html/app/code/core/Mage/Core/functions.php</bon line <b>240</b><br />
 
Magento Community Magento Community
Magento Community
Magento Community
 
finch8243
Sr. Member
 
Total Posts:  79
Joined:  2007-08-31
 

you already got it installed and running I assume?  And obvious question here, but you don’t actually have the “XXXX” in there right?

 
Magento Community Magento Community
Magento Community
Magento Community
 
zadpro
Sr. Member
 
Avatar
Total Posts:  247
Joined:  2007-12-10
FL, USA
 

The XXX is my hosting username that I just removed in my reply. I tried to use your code, but it is not working. As I mentioned, I’m getting the above error message.

 
Magento Community Magento Community
Magento Community
Magento Community
 
setanta
Member
 
Total Posts:  33
Joined:  2007-11-01
 

@ perteweb I tried creating a cron with the code you recommended to clean my cache but it emails me this response

/bin/sh: -c: line 0: syntax error near unexpected token `-’
/bin/sh: -c: line 0: `Mage::app()->getCache()->clean ;’

Can you offer any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Gabriel Queiroz
Sr. Member
 
Total Posts:  135
Joined:  2008-05-28
Brasília, Brazil
 

Your cron task should be something like:

/path/to/bin/php /path/to/script.php

And inside that php script you add the command to clean the cache. Remember this script should include the magento core files. Take a look at magento’s /index.php, it will be something like that whithout the last line, Mage::run(); and some other unnecessary code.

Also, I suppose the correct code is:

Mage::app()->getCache()->clean();

since ‘clean’ is a method. I haven’t tested it, though.

Cheers,

 
Magento Community Magento Community
Magento Community
Magento Community
 
biondo
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2008-07-24
Italy
 

Hello, I’m trying to clean the cache using:

Mage::app()->cleanCache(); 
or

Mage::getSingleton('catalog/url')->refreshRewrites();
Mage::getModel('catalog/product_image')->clearCache();
Mage::getSingleton('catalogindex/indexer')->plainReindex();
or

Mage::app()->getCache()->clean();
Mage::app()->cleanAllSessions();
But it doesn’t work!
 
Magento Community Magento Community
Magento Community
Magento Community
 
amartinez
Guru
 
Avatar
Total Posts:  347
Joined:  2008-11-14
Spain
 

Please find my refresh function here: http://www.magentocommerce.com/boards/viewreply/156277/

It works fine with 1.3.2.2

Hope it help! smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2