Parcourir la source

[Frontend] Home : optimisations / cache

develop
Guillaume Bourgeois il y a 2 ans
Parent
révision
1d1ce7d2b6
3 fichiers modifiés avec 57 ajouts et 2 suppressions
  1. +2
    -0
      Builder/Product/ProductFamilyBuilder.php
  2. +54
    -1
      Repository/Product/ProductFamilyStore.php
  3. +1
    -1
      Solver/Product/ProductFamilySolver.php

+ 2
- 0
Builder/Product/ProductFamilyBuilder.php Voir le fichier

@@ -2,6 +2,8 @@

namespace Lc\CaracoleBundle\Builder\Product;

use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;

class ProductFamilyBuilder
{


+ 54
- 1
Repository/Product/ProductFamilyStore.php Voir le fichier

@@ -11,6 +11,8 @@ use Lc\CaracoleBundle\Solver\Price\PriceSolver;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\CaracoleBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

class ProductFamilyStore extends AbstractStore
{
@@ -19,13 +21,16 @@ class ProductFamilyStore extends AbstractStore

protected ProductFamilyRepositoryQuery $query;
protected PriceSolver $priceSolver;
protected CacheInterface $cache;

public function __construct(
ProductFamilyRepositoryQuery $query,
PriceSolver $priceSolver
PriceSolver $priceSolver,
CacheInterface $cache
) {
$this->query = $query;
$this->priceSolver = $priceSolver;
$this->cache = $cache;
}

public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
@@ -116,6 +121,25 @@ class ProductFamilyStore extends AbstractStore
return $this->getWithReductions($results, $user, false, $organizeByParentCategory);
}

public function getCachedNovelty($user = null, $organizeByParentCategory = true, $query = null): array
{
$productFamilyStore = $this;
return $this->cache->get($this->getCacheKeyNovelty(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) {
$item->expiresAfter(3600);
return $productFamilyStore->getNovelty($user, $organizeByParentCategory, $query);
});
}

public function deleteCacheNovelty(): void
{
$this->cache->delete($this->getCacheKeyNovelty());
}

public function getCacheKeyNovelty()
{
return 'products_novelty_'.$this->section->getId();
}

// getProductFamiliesOrganics
public function getOrganic($user = null, $organizeByParentCategory = true, $query = null)
{
@@ -134,6 +158,31 @@ class ProductFamilyStore extends AbstractStore
return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory, true);
}

public function getCachedDiscount($user = null, $organizeByParentCategory = true, $query = null): array
{
$productFamilyStore = $this;

return $this->cache->get($this->getCacheKeyDiscount(), function (ItemInterface $item) use ($productFamilyStore, $user, $organizeByParentCategory, $query) {
$item->expiresAfter(3600);
return $productFamilyStore->getDiscount($user, $organizeByParentCategory, $query);
});
}

public function deleteCacheDiscount(): void
{
$this->cache->delete($this->getCacheKeyDiscount());
}

public function getCacheKeyDiscount()
{
return 'products_discount_'.$this->section->getId();
}

public function loadAllDatas(ProductFamilyInterface $productFamily)
{
$productFamily->getProductFamilySectionProperties()->toArray();
}

// getProductFamiliesFavorites
public function getFavorite($user = null, $organizeByParentCategory = true, $query = null)
{
@@ -227,6 +276,10 @@ class ProductFamilyStore extends AbstractStore

$productFamiliesToReturn = array();
foreach ($productFamilies as $productFamily) {

// Cache : chargement de toutes les données manquantes (fetch eager ne fonctionne pas)
$this->loadAllDatas($productFamily);

foreach ($reductionCatalogs as $reductionCatalog) {
$conditionProductFamilies = $conditionProductFamily = $conditionProductCategory = false;


+ 1
- 1
Solver/Product/ProductFamilySolver.php Voir le fichier

@@ -152,7 +152,7 @@ class ProductFamilySolver
if ($productFamily->getTaxRate()) {
return $productFamily->getTaxRate();
} else {
return $productFamily->getProductFamilySectionProperties()[0]->getSection()->getMerchant()->getTaxRate();
return $this->getMerchant($productFamily)->getTaxRate();
}
}


Chargement…
Annuler
Enregistrer