Prestashop: Caching in Modules does not work

Created on 22 Jan 2019  路  13Comments  路  Source: PrestaShop/PrestaShop

Describe the bug
As a module developer i wanted to use variable based Cache (instead of the page wide Smarty cache) by using Cache::getInstance(). Unfortunately, either the value is never set or the check if there is a cached value fails, it always returns false.

To Reproduce

  1. Develop own module against 1.7.5 prestashop
  2. Use snippet as proposed here
$cache = Cache::getInstance();
$cache->set($key, $data, 0);
if($cache->exists($key))
  echo $cache->get($key);
  1. Cache is never retrieved because exists() always returns false.

Screenshots
If applicable, add screenshots or screenrecords to help explain your problem.

Additional information
There is a bugrequest PSCSX-7149 in "the forge" which seems relevant. I cant use the Smarty caching as a workaround since it doesnt feature a TTL yet. Maybe PR 8855 fixes this problem?

PrestaShop version: 1.7.5.0 based on Symfony 3.4.19 from a docker container using prestashop/prestashop:1.7 against MariaDB in a docker container using mariadb/server:10.3
PHP version: 5.6.39

1.7.5.0 Bug Modules No change required Performance

All 13 comments

Hi @x29a,

Could you please provide me with your custom module to test it.
This is a public space.
You can provide me the module by email.
My address mail: khouloud.[email protected]
Let's be safe!

Thanks!

@khouloudbelguith thanks for the quick response, i created a mcve i attached here.

When loading the module, it should show in displayHome or displayRightColumn:

TestCache Template!
Message from Cache system: cache updated with: 'yo this is loaded from cache' !

After a page reload, i would expect:

TestCache Template!
Message from Cache system: cache loaded: 'yo this is loaded from cache' !

yet i always get the first message.

I dont get any PHP errors.

Updated the sample module to use Cache::store() and related static functions, same result :/

Hi @x29a,

Thanks for your feedback.
After installing your module, I checked the positions of this module & it is OK.
image
In the FO, also, I have this text displayed
TestCache Template! Message from Cache system: cache updated with: 'yo this is loaded from cache' !
image
Thanks to check & feedback.

Thank you for trying the module.

Installing, loading and positioning of the module works great. What does not work is using the Cache class as described in my original post. When looking at my second example modules code, there are two different approaches (moduleDisplay2() and moduleDisplay2()) tried and both dont work.

The result is, when you refresh your shops site, the same text will be displayed meaning the value is freshly fetched from source and not from cache. If caching worked, it would show:

TestCache Template!
Message from Cache system: cache loaded: 'yo this is loaded from cache' !

Did you have a look at the modules code or the linked stackoverflow post to see if in general the class is used correctly and should work like this? As i understand this is a seperate mechanism from Smarty's caching mechanism. The goal is fetch data from an external site only e.g. each hour and serve intermediate requests from a local cache.

@x29a, thanks for the clarifications.
Yes, whan I reload the page,

TestCache Template!
Message from Cache system: cache loaded: 'yo this is loaded from cache' !

is not displayed.

ping @eternoendless, @PrestaShop/prestashop-core-developers what do you think?
Thanks!

Might this be related with the removal of CacheFs.php implementation? In 1.6.x it was still present, in 1.7.x it is not, as implemented by this pull request. I found it in this changelog.

What was the reason behind the decision? Is there a substitute?

As mentioned in the linked PR, filebased caching was probably considered less efficient and therefor removed. I still think its a good fallback.

I tried using the 1.7 caching via memcache (which is the default when one enables caching in the shop performance settings), but the extension is not present in the docker image (i verified via this code).

So i think the problem is twofold:

  • Prestashop selects "Memcache" as caching mechanism even when the extension is not installed (this might be due to the nature of the radio button) --> this issue
  • The prestashop docker does not include the default caching extension memcache --> Issue 153

Hello @x29a indeed I agree with you there is something missing:

  • there should be a fallback with a filesystem cache when MemCache is not enabled (which implies adding this option in the Performance page, and adding a few check to warn the user about which cache system he can use)
  • ideally using symfony cache system would be best, and deprecating the legacy Cache class (but it needs to be fixed first)
  • besides when cache is disabled Cache::getInstance should return an ArrayCache (which only stores values for one process), or maybe even a "NullCache" which would always return false when fetching a value

Watch out, don't mix up Memcache and Memcached which are different. More because Memcache is very old and don't work with latest php version.

https://serverfault.com/a/63399

Thank you both for your answers!
@PierreRambaud i was only referring memcache (without the D) because it currently is selected-by-default option. Maybe your comment is an indication that it should be removed along the way.

@jolelievre This just introduced another aspect, so i see a multistep approach

  • Implement your suggested changes for when caching is disabled
  • Revert the CacheFs removal parts from Merge Request #6408 (to provide a safe fallback and default option)
  • Remove memcache (without the D) support as stated by @PierreRambaud (or at least add check for proper working PHP version)
  • Introduce Symphony Cache
  • Maybe remove CacheFs once the proper alternative fallback solution is offered (or let user choose)
  • Maybe remove Cache class altogether (as you suggested, you know the reasons best)

Well, this escalated quickly. How to procede from here?

After a very long time. I investigate on this issue.
The first method moduleDisplay` is using the cache which is settled here:
image

The seconde one moduleDisplay2 is using a local cache, Cache::store and Cache::retrieve used a $local variable.

And everything is working well :thinking:

I'm against using FS as fallback, some websites are not running under system allowing files to be written, it uses external cache server.

This has been a while and the specific problem has been worked around, i accept your decisions and close this issue. Thanks for looking into it!

Was this page helpful?
0 / 5 - 0 ratings