Prestashop caching of data
Asked Answered
L

1

6

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

Loony answered 2/11, 2016 at 13:28 Comment(1)
Think you need to check if (!Cache::isStored($cache_id)) and then to store and save data, because like you said it always returns false.Cortico
S
10

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.

Supersaturate answered 2/11, 2016 at 19:6 Comment(2)
not working as expected in prestashop 1.6.1.2 to 1.6.1.19 when using file caching forge.prestashop.com/browse/PSCSX-7149 in my case it was adding the file on every request to the cachefs directory but always returned $cache->exists() false so the cache never worked.Kayak
I created this issue for prestashop 1.7. Please be aware: filebased caching was removed in 1.7. But im struggling with memcache as well.Plessor

© 2022 - 2024 — McMap. All rights reserved.