shopware-php-sdk icon indicating copy to clipboard operation
shopware-php-sdk copied to clipboard

Don't cache entities

Open ckilb opened this issue 2 years ago • 4 comments

Right now hydrated entities are cached in \Vin\ShopwareSdk\Repository\Traits\EntityHydrator::hydrateEntity

$cacheKey = $entityRaw['type'] . '-' . $entityRaw['id']; if (array_key_exists($cacheKey, $this->cache)) { return $this->cache[$cacheKey]; }

This should imo get removed without replacement. I'd not expect the SDK to do caching for me. In case I want caching I could easily implement it on my own.

There are some downsides of caching:

  • If you search for an entity without associations and later on search for the same entity with association, the "new" hydrated entity is actually the old one without the associations added -> this is what took me quite some time to figure out.

  • If you use some web server that doesn't end the PHP progress after the request has been finished this value will be cached forever. Even if the next request comes in after minutes/days/weeks the old value of that entity is still cached. I know common webservers like Apache or Nginx with PHP-FPM start a new process with each request - but new approaches are coming and get more and more popular (Swoole, Roadrunner).

Like I said I don't think caching is necessary (the request to actually get the entity data from Shopware is probably much slower than hydrating the entity) so I'd remove it. In case you think caching should still be present it would be nice to be able to enable/disable it (via Context flag?) or at least to clear it (via method call).

=== Another proposal related: For now I'd like to replace the method hydrateEntity with my own. If hydrateEntity would be public, EntityHydrator would be a class that get's initialized by some factory and injected to the EntityRepository this would be quite easy. But as hydrateEntity is a private trait method inside a Trait which is hardly coupled to EntityRepository this is a bit tricky to do. In general I'd suggest - at least for a library - to avoid Traits and rely on DI instead.

=== PR: https://github.com/vienthuong/shopware-php-sdk/pull/47

ckilb avatar Aug 29 '22 16:08 ckilb

I'd agree with your suggestion 👍🏽 Thanks for your contribution :) The MR looks good but please add the changelog for what you have changed in the new version

vienthuong avatar Aug 30 '22 16:08 vienthuong

hey @vienthuong thanks for your feedback. just pushed the changelog 👍

ckilb avatar Sep 05 '22 21:09 ckilb

Hi @ckilb Can you resolve the conflict :) after that I can merge

vienthuong avatar Sep 07 '22 07:09 vienthuong

Hey @vienthuong thanks for merging! Could you tag the new version so Composer is able to find it? Thanks!

ckilb avatar Sep 07 '22 19:09 ckilb

@ckilb Sorry for the late response, Hi it should available on latest version 1.7.1 and 1.7.2 Feel free to reopen if you encounter any issue

vienthuong avatar Nov 16 '22 16:11 vienthuong