mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-23 16:36:12 +01:00
57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Codappix\SearchCore\Domain\Search;
|
|
|
|
use Codappix\SearchCore\Connection\SearchRequestInterface;
|
|
use Codappix\SearchCore\Connection\SearchResultInterface;
|
|
use TYPO3\CMS\Core\SingletonInterface;
|
|
|
|
/**
|
|
* Service: Cached Search
|
|
* @package Codappix\SearchCore\Domain\Search
|
|
*/
|
|
class CachedSearchService implements SingletonInterface
|
|
{
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $results = [];
|
|
|
|
/**
|
|
* @var SearchService
|
|
*/
|
|
protected $searchService;
|
|
|
|
/**
|
|
* @param SearchService $searchService
|
|
*/
|
|
public function __construct(SearchService $searchService)
|
|
{
|
|
$this->searchService = $searchService;
|
|
}
|
|
|
|
/**
|
|
* @param SearchRequestInterface $searchRequest
|
|
* @return SearchResultInterface
|
|
*/
|
|
public function search(SearchRequestInterface $searchRequest): SearchResultInterface
|
|
{
|
|
$hash = $this->getHash($searchRequest);
|
|
if (isset($this->results[$hash]) && $this->results[$hash] instanceof SearchResultInterface) {
|
|
return $this->results[$hash];
|
|
}
|
|
return $this->results[$hash] = $this->searchService->search($searchRequest);
|
|
}
|
|
|
|
/**
|
|
* @param SearchRequestInterface $searchRequest
|
|
* @return string
|
|
*/
|
|
protected function getHash(SearchRequestInterface $searchRequest): string
|
|
{
|
|
if (is_callable([$searchRequest, 'getRequestHash'])) {
|
|
return (string)$searchRequest->getRequestHash();
|
|
}
|
|
return sha1(serialize($searchRequest));
|
|
}
|
|
}
|