Browse Source

[Frontend] Home : optimisations / cache

develop
Guillaume Bourgeois 2 years ago
parent
commit
1d1ce7d2b6
3 changed files with 57 additions and 2 deletions
  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 View File



namespace Lc\CaracoleBundle\Builder\Product; namespace Lc\CaracoleBundle\Builder\Product;


use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;

class ProductFamilyBuilder class ProductFamilyBuilder
{ {



+ 54
- 1
Repository/Product/ProductFamilyStore.php View File

use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\CaracoleBundle\Repository\AbstractStore; use Lc\CaracoleBundle\Repository\AbstractStore;
use Lc\SovBundle\Repository\RepositoryQueryInterface; use Lc\SovBundle\Repository\RepositoryQueryInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;


class ProductFamilyStore extends AbstractStore class ProductFamilyStore extends AbstractStore
{ {


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


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


public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface public function orderByDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
return $this->getWithReductions($results, $user, false, $organizeByParentCategory); 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 // getProductFamiliesOrganics
public function getOrganic($user = null, $organizeByParentCategory = true, $query = null) public function getOrganic($user = null, $organizeByParentCategory = true, $query = null)
{ {
return $this->getWithReductions($this->getOnline($query), $user, false, $organizeByParentCategory, true); 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 // getProductFamiliesFavorites
public function getFavorite($user = null, $organizeByParentCategory = true, $query = null) public function getFavorite($user = null, $organizeByParentCategory = true, $query = null)
{ {


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

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

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



+ 1
- 1
Solver/Product/ProductFamilySolver.php View File

if ($productFamily->getTaxRate()) { if ($productFamily->getTaxRate()) {
return $productFamily->getTaxRate(); return $productFamily->getTaxRate();
} else { } else {
return $productFamily->getProductFamilySectionProperties()[0]->getSection()->getMerchant()->getTaxRate();
return $this->getMerchant($productFamily)->getTaxRate();
} }
} }



Loading…
Cancel
Save