Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prestashop caching of data

We have created some custom pages, where we are forced to run a lot of sql statements. The data can be cached for a couple of days or untill we clear the cache.

My prolem is it don't store the cache. Everytime the funciton Cache::isStored returns false.

$cache_id = "test";
if (Cache::isStored($cache_id)) {
   /// Query data
   Cache::store($cache_id, $data);
}

$data = Cache::retrieve($cache_id);
echo json_encode($data);

I think i'm using the cache function wrong. But I can't seem to find any documentation for it.

Can anybody help either with documentation or some example code

like image 624
Anders Andersen Avatar asked Nov 02 '16 13:11

Anders Andersen


1 Answers

Cache::store() and Cache::retrieve() will store data 'locally' in a class static variable which means data is lost next time you refresh a page.

Caching data this way is useful if you have multiple modules requesting same data in one request but you need data to refresh everytime you refresh a page.

One example of this is the price calculation of a product. Lets say you have on home page a module which displays few products from each category, a module for new products, a module for best sellers and each shop has a cart block module.

For each product shown the modules will call price calculation method so it's displayed to customers and because some products can be the same in multiple modules, this is a way to cache prices 'locally' or for one request.

To cache data beyond one request you should use

$cache = Cache::getInstance();
$cache->set($key, $data, $ttl);
$cache->get($key);
$cache->exists($key);

Set $ttl to time in seconds for how long you want the data to persist or by default its 0 so it stays in cache until you manually clear it.

like image 87
TheDrot Avatar answered Oct 03 '22 23:10

TheDrot