Caching PrestaShop

hello everybody. actually i’m working for a webagency and i used prestashop to develop an ecommerce with over 3700 products and over 250 categories. pretty huge uh?

well prestashop it’s easy to develop and extend, but it’s not so optimized.. so it’s easy to run into slow queries and server freezes with over 1300 visitors per day. So i had to cache some of biggest queries, like categories tree and subcategories, blocks and products. With those expedients i lowered the number of queries from 200 per page to nearly 40 (there are a lot, i know.. but it’s prestashop, not my fault.)
so let’s start with the fixes! :)

first of all, let’s define two variable for our caching system, like the directory path of our cached content. open defines.inc.php and declare a variable, like i did

define('_SQL_CACHING_',  true); //set it to true to enable caching!
define('_PS_CACHE_DIR_',  __PS_BASE_URI__.'cache/'); //cache path!

then make that directory and give it write permissions.

then, let’s introduce some cache functions inside the Tools class file.

	static public function setCache($var,$data,$dir='sql/')
	{
		if (!_SQL_CACHING_) return;

		$path=_PS_CACHE_DIR_;

		$dir.=($var%100).'/'.$var.'/';
		$data=serialize($data);

		$path.=$dir;
		@mkdir($path,0777,true);
		@chmod($path,0777);
		$file=$path.$var.'.cache';

		@file_put_contents($file,$data);
		@chmod($file,0777);
	}

	static public function getCache($var,$dir='sql/')
	{
		if (!_SQL_CACHING_) return false;

		$path=_PS_CACHE_DIR_;

		$dir.=($var%100).'/'.$var.'/';
		$path.=$dir;
		$file=$path.$var.'.cache';

		if (file_exists($file))
		{
			$data=@file_get_contents($file);
			return unserialize($data);
		}
		else return false;
	}

	static public function delCache($var,$dir='sql/')
	{
		if (!_SQL_CACHING_) return false;

		if(!function_exists('rmdirr'))
		{
			function rmdirr($dir)
			{
				if (@file_exists($dir))
				{
					if (@is_dir($dir))
					{
						foreach(glob($dir."/*") as $obj)
							@is_dir($obj)? rmdirr($obj) : @unlink($obj);
						@rmdir($dir);
					}
					else @unlink($dir);
				}
			}
		}

		$path=_PS_CACHE_DIR_.$dir.($var%100).'/'.$var.'*';
		foreach(glob($path) as $filename)
			rmdirr($filename);
	}

let’s examine that functions.. we have:

  • setCache($var,$data,$dir=’sql/’): write cached data to disk
  • getCache($var,$dir=’sql/’): read cached data from disk
  • delCache($var,$dir=’sql/’): delete cached data from disk

how to use those function (parameters explaination)

  • $var: it’s the variable/file-on-disk name. it must be unique or a cached content will replace another one!
  • $data: it’s the data that we want to cache. it can be anything: simple variable, array, object..
  • $dir: it’s the subdirectory of cache path. I use it to differentiate caching path of blocks from main pages, for example.

where to use those functions?
well we have exactly three points where to use these functions: modules (on blocks), pages, single objects.

  • modules: we have to act inside the module files. normally the main file (same name of module’s directory)
  • pages: we have to act inside the files of prestashop’s root, like product.php or category.php and interact with smarty (the template engine)
  • objects: we have to act inside the ObjectModel class

On next posts i will show some examples that describes how to apply these kind of cache!

6 thoughts on “Caching PrestaShop

  1. eBuildy

    Prestashop is very fun but their programmer are just stupid ! Imagine a web site which runs 200 queries per page ! Just ridiculous ….

    I tried many ways to optimize it, something like you (but you MUST generate a PHP file with Php code not a serialized file ..)

    My best solution was to produce (like a squid proxy) static HTML files, so I never all the Prestashop engine (smarty, 200 queries …), trust me, my web site was 10x faster ! And no more problem of scalability etc etc …

    The bigest problem was the modules, I had to ajaxied all the modules so they can refresh their content even if my page is pure HTML.

    I think the futur of the web is like that : You load a HTML template with some AJAX widgets so you can have dynamic content on your web site (just as the basket …)

    I plan to release this module on my web site : http://boutique.ebuildy.com very soon (9/06/2011)

    Hope I help you !

  2. Valerio Riva Post author

    thank you for your contribute. actually i have to post the other 2 way to cache prestashop but i have no time :\

  3. Valerio Riva Post author

    as prestashop 1.3, i put those functions inside Tools class. –> “then, let’s introduce some cache functions inside the Tools class file.”

  4. MIke

    Hey Valerio,

    Nice article. Just curious, have you been able to update the other ways to update methods.

Leave a Reply

Your email address will not be published. Required fields are marked *