Skip to content

Commit

Permalink
Merge pull request #260 from BitBagCommerce/op-310-performance-fix
Browse files Browse the repository at this point in the history
OP-310: Cache queries
  • Loading branch information
GracjanJozefczyk authored Jun 25, 2024
2 parents c1fcaf3 + f7caa3a commit 7152d14
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/Repository/WishlistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $na
->getOneOrNullResult()
;
}

public function findAllByShopUserAndChannel(
ShopUserInterface $shopUser,
ChannelInterface $channel,
): array {
return $this->createQueryBuilder('o')
->where('o.shopUser = :shopUser')
->andWhere('o.channel = :channel')
->setParameter('shopUser', $shopUser)
->setParameter('channel', $channel)
->getQuery()
->getResult()
;
}
}
5 changes: 5 additions & 0 deletions src/Repository/WishlistRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public function findAllByAnonymousAndChannel(?string $token, ChannelInterface $c
public function findOneByTokenAndName(string $token, string $name): ?WishlistInterface;

public function findOneByShopUserAndName(ShopUserInterface $shopUser, string $name): ?WishlistInterface;

public function findAllByShopUserAndChannel(
ShopUserInterface $shopUser,
ChannelInterface $channel,
): array;
}
67 changes: 53 additions & 14 deletions src/Twig/WishlistExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace BitBag\SyliusWishlistPlugin\Twig;

use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
Expand All @@ -22,8 +21,22 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class WishlistExtension extends AbstractExtension
final class WishlistExtension extends AbstractExtension
{
private const WISHLIST_USER_TOKEN_CACHE_PATTERN = 'user_id_%s_token_%s';

private const WISHLIST_USER_CHANNEL_CACHE_PATTERN = 'user_id_%s_channel_%s';

private const WISHLIST_ANONYMOUS_CHANNEL_CACHE_PATTERN = 'anonymous_channel_id_%s';

private const WISHLIST_ANONYMOUS_CACHE_KEY = 'anonymous_token_%s';

private const WISHLIST_USER_CACHE_KEY = 'user_id_%s';

private const WISHLIST_ALL_CACHE_KEY = 'all';

private array $wishlists = [];

public function __construct(
private WishlistRepositoryInterface $wishlistRepository,
private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver,
Expand All @@ -44,10 +57,11 @@ public function getFunctions(): array

public function getWishlists(): ?array
{
/** @var WishlistInterface[] $wishlists */
$wishlists = $this->wishlistRepository->findAll();
if (false === isset($this->wishlists[self::WISHLIST_ALL_CACHE_KEY])) {
$this->wishlists[self::WISHLIST_ALL_CACHE_KEY] = $this->wishlistRepository->findAll();
}

return $wishlists;
return $this->wishlists[self::WISHLIST_ALL_CACHE_KEY];
}

public function findAllByShopUser(UserInterface $user = null): ?array
Expand All @@ -56,25 +70,40 @@ public function findAllByShopUser(UserInterface $user = null): ?array
throw new UnsupportedUserException();
}

return $this->wishlistRepository->findAllByShopUser($user->getId());
$cacheKey = sprintf(self::WISHLIST_USER_CACHE_KEY, $user->getId());
if (false === isset($this->wishlists[$cacheKey])) {
$this->wishlists[$cacheKey] = $this->wishlistRepository->findAllByShopUser($user->getId());
}

return $this->wishlists[$cacheKey];
}

public function findAllByShopUserAndToken(UserInterface $user = null): ?array
public function findAllByAnonymous(): ?array
{
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
$cacheKey = sprintf(self::WISHLIST_ANONYMOUS_CACHE_KEY, $wishlistCookieToken);

if (!$user instanceof ShopUserInterface) {
throw new UnsupportedUserException();
if (false === isset($this->wishlists[$cacheKey])) {
$this->wishlists[$cacheKey] = $this->wishlistRepository->findAllByAnonymous($wishlistCookieToken);
}

return $this->wishlistRepository->findAllByShopUserAndToken($user->getId(), $wishlistCookieToken);
return $this->wishlists[$cacheKey];
}

public function findAllByAnonymous(): ?array
public function findAllByShopUserAndToken(UserInterface $user = null): ?array
{
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();

return $this->wishlistRepository->findAllByAnonymous($wishlistCookieToken);
if (!$user instanceof ShopUserInterface) {
throw new UnsupportedUserException();
}

$cacheKey = sprintf(self::WISHLIST_USER_TOKEN_CACHE_PATTERN, $user->getId(), $wishlistCookieToken);
if (false === isset($this->wishlists[$cacheKey])) {
$this->wishlists[$cacheKey] = $this->wishlistRepository->findAllByShopUserAndToken($user->getId(), $wishlistCookieToken);
}

return $this->wishlists[$cacheKey];
}

public function findAllByShopUserAndChannel(UserInterface $user = null, ChannelInterface $channel = null): ?array
Expand All @@ -86,13 +115,23 @@ public function findAllByShopUserAndChannel(UserInterface $user = null, ChannelI
throw new ChannelNotFoundException();
}

return $this->wishlistRepository->findAllByShopUser($user->getId());
$cacheKey = sprintf(self::WISHLIST_USER_CHANNEL_CACHE_PATTERN, $user->getId(), $channel->getCode());
if (false === isset($this->wishlists[$cacheKey])) {
$this->wishlists[$cacheKey] = $this->wishlistRepository->findAllByShopUserAndChannel($user, $channel);
}

return $this->wishlists[$cacheKey];
}

public function findAllByAnonymousAndChannel(ChannelInterface $channel): ?array
{
$wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
$cacheKey = sprintf(self::WISHLIST_ANONYMOUS_CHANNEL_CACHE_PATTERN, $channel->getCode());

if (false === isset($this->wishlists[$cacheKey])) {
$this->wishlists[$cacheKey] = $this->wishlistRepository->findAllByAnonymousAndChannel($wishlistCookieToken, $channel);
}

return $this->wishlistRepository->findAllByAnonymousAndChannel($wishlistCookieToken, $channel);
return $this->wishlists[$cacheKey];
}
}

0 comments on commit 7152d14

Please sign in to comment.