Skip to content

Commit

Permalink
Merge pull request #1050 from plentymarkets/optimize/caching_search
Browse files Browse the repository at this point in the history
use local caching for getVariation
  • Loading branch information
stentrop authored Nov 12, 2021
2 parents d36faf5 + 5ee5ff1 commit 32d53e3
Showing 1 changed file with 8 additions and 53 deletions.
61 changes: 8 additions & 53 deletions src/Services/ItemService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
use Plenty\Plugin\Application;
use Plenty\Modules\Webshop\ItemSearch\Helpers\ResultFieldTemplate;



/**
* Service Class ItemService
*
Expand All @@ -68,6 +66,9 @@ class ItemService
/** @var array */
private $additionalItemSortingMap = [];

/** @var array */
private static $singleVariations = [];

/**
* ItemService constructor.
* @param Application $app
Expand Down Expand Up @@ -177,10 +178,13 @@ public function getItemImage(int $itemId = 0): string
*/
public function getVariation(int $variationId = 0)
{
if (isset(self::$singleVariations[$variationId])) {
return self::$singleVariations[$variationId];
}
/** @var ItemSearchService $itemSearchService */
$itemSearchService = pluginApp(ItemSearchService::class);

return $itemSearchService->getResults([SingleItem::getSearchFactory(['variationId' => $variationId])])[0];
self::$singleVariations[$variationId] = $itemSearchService->getResults([SingleItem::getSearchFactory(['variationId' => $variationId])])[0];
return self::$singleVariations[$variationId];
}

/**
Expand Down Expand Up @@ -301,7 +305,6 @@ public function getVariationList($itemId, bool $withPrimary = false): array
array_push($variationIds, $variation->variationBase->id);
}
}

return $variationIds;
}

Expand Down Expand Up @@ -333,7 +336,6 @@ public function getVariationImage(int $variationId = 0, string $imageAccessor =
$itemImageFilter = pluginApp(ItemImagesFilter::class);
return $itemImageFilter->getFirstItemImageUrl($variation['documents'][0]['data']['images'], $imageAccessor);
}

return '';
}

Expand Down Expand Up @@ -468,7 +470,6 @@ function () use ($itemId) {
return $variations;
}
);

return $attributeMap;
}

Expand Down Expand Up @@ -524,7 +525,6 @@ public function getVariationIsSalable($variationId = 0): bool
$record = $this->itemRepository->search($columns, $filter, $params)->current();

$isSalable = $record['variationBase']['limitOrderByStockSelect'] == 1 && $record['variationStock']['stockPhysical'] <= 0;

return $isSalable;
}

Expand Down Expand Up @@ -636,7 +636,6 @@ function () use ($unitCombinationId, $unitCombinationRepo) {
}
}
}

return [
'attributes' => $attributeList,
'units' => $unitList
Expand Down Expand Up @@ -702,7 +701,6 @@ function () use ($attributeId) {
return $name;
}
);

return $attributeName;
}

Expand Down Expand Up @@ -731,7 +729,6 @@ function () use ($attributeValueId) {
return $name;
}
);

return $attributeValueName;
}

Expand Down Expand Up @@ -786,8 +783,6 @@ public function getItemCrossSellingList($itemId = 0, string $crossSellingType =
}
}
}


return $crossSellingItems;
}

Expand Down Expand Up @@ -839,7 +834,6 @@ public function getLatestItems(int $limit = 5, int $categoryId = 0)
->withParam(ItemColumnsParams::ORDER_BY, ["orderBy.variationCreateTimestamp" => "desc"])
->withParam(ItemColumnsParams::LIMIT, $limit)
->build();

return $this->itemRepository->search($columns, $filter, $params);
}

Expand Down Expand Up @@ -878,7 +872,6 @@ public function searchItems(string $searchString, $params = [], int $page = 1):
->addFilter($variationFilter)
->addFilter($searchFilter)
->setPage($page, $params['itemsPerPage']);

return $elasticSearchRepo->execute();
}

Expand All @@ -904,42 +897,4 @@ public function addAdditionalItemSorting($key, $translationKey)
{
$this->additionalItemSortingMap[$key] = $translationKey;
}

/**
* @param string $searchString
* @return array
*/
/*public function searchItemsAutocomplete(string $searchString):array
{
/** @var IncludeSource $includeSource */
/*$includeSource = pluginApp(IncludeSource::class);
$includeSource->activate('test', 'test');
$documentProcessor = pluginApp(DocumentProcessor::class);
$documentSearch = pluginApp(DocumentSearch::class, [$documentProcessor]);
/** @var VariationElasticSearchSearchRepositoryContract $elasticSearchRepo */
/*$elasticSearchRepo = pluginApp(VariationElasticSearchSearchRepositoryContract::class);
$elasticSearchRepo->addSearch($documentSearch);
/** @var VariationBaseFilter $variationFilter */
/*$variationFilter = pluginApp(VariationBaseFilter::class);
$variationFilter->isActive();
/** @var ClientFilter $clientFilter */
/*$clientFilter = pluginApp(ClientFilter::class);
$clientFilter->isVisibleForClient($this->app->getPlentyId());
/** @var SearchFilter $searchFilter */
/*$searchFilter = pluginApp(SearchFilter::class);
$searchFilter->setSearchString($searchString, ElasticSearch::SEARCH_TYPE_AUTOCOMPLETE);
$documentSearch
->addFilter($clientFilter)
->addFilter($variationFilter)
->addFilter($searchFilter)
->addSource($includeSource);
return $elasticSearchRepo->execute();
}*/
}

0 comments on commit 32d53e3

Please sign in to comment.