Fix caching of number of searched records

This commit is contained in:
Yuriy Bakhtin 2024-03-13 15:38:04 +01:00
parent f2973adfff
commit e20d737d63
4 changed files with 24 additions and 17 deletions

View File

@ -44,11 +44,12 @@ abstract class SearchProvider
abstract public function getName(): string;
/**
* Search results
* Run search process, result may be cached
*
* @return SearchRecordInterface[]
* @return array 'totalCount' - Number of total searched records,
* 'results' - Array of searched records SearchRecordInterface[]
*/
abstract public function searchResults(): array;
abstract public function runSearch(): array;
/**
* Run search process and cache results
@ -61,10 +62,13 @@ abstract class SearchProvider
return;
}
$this->results = Yii::$app->cache->getOrSet(
$data = Yii::$app->cache->getOrSet(
static::class . Yii::$app->user->id . ':search:' . $this->keyword,
[$this, 'searchResults'],
[$this, 'runSearch'],
$this->cacheTimeout);
$this->totalCount = $data['totalCount'] ?? 0;
$this->results = $data['results'] ?? [];
}
/**

View File

@ -42,7 +42,7 @@ class ContentSearchProvider extends SearchProvider
/**
* @inheritdoc
*/
public function searchResults(): array
public function runSearch(): array
{
/* @var Module $module */
$module = Yii::$app->getModule('content');
@ -52,13 +52,14 @@ class ContentSearchProvider extends SearchProvider
'pageSize' => $this->pageSize
]));
$this->totalCount = $resultSet->pagination->totalCount;
$results = [];
foreach ($resultSet->results as $content) {
$results[] = new SearchRecord($content);
}
return $results;
return [
'totalCount' => $resultSet->pagination->totalCount,
'results' => $results
];
}
}

View File

@ -43,20 +43,21 @@ class SpaceSearchProvider extends SearchProvider
/**
* @inheritdoc
*/
public function searchResults(): array
public function runSearch(): array
{
$spaceDirectoryQuery = new SpaceDirectoryQuery([
'defaultFilters' => ['keyword' => $this->keyword],
'pageSize' => $this->pageSize
]);
$this->totalCount = $spaceDirectoryQuery->pagination->totalCount;
$results = [];
foreach ($spaceDirectoryQuery->all() as $space) {
$results[] = new SearchRecord($space);
}
return $results;
return [
'totalCount' => $spaceDirectoryQuery->pagination->totalCount,
'results' => $results
];
}
}

View File

@ -43,20 +43,21 @@ class UserSearchProvider extends SearchProvider
/**
* @inheritdoc
*/
public function searchResults(): array
public function runSearch(): array
{
$peopleQuery = new PeopleQuery([
'defaultFilters' => ['keyword' => $this->keyword],
'pageSize' => $this->pageSize
]);
$this->totalCount = $peopleQuery->pagination->totalCount;
$results = [];
foreach ($peopleQuery->all() as $user) {
$results[] = new SearchRecord($user);
}
return $results;
return [
'totalCount' => $peopleQuery->pagination->totalCount,
'results' => $results
];
}
}