chore(*): remove duplicate cache rule + cleanup cache headers; cleanup + formatting
This commit is contained in:
parent
425fed6ff6
commit
0e565970a5
42 changed files with 223 additions and 217 deletions
|
@ -24,10 +24,7 @@ class SearchHandler extends BaseHandler
|
|||
$offset = ($page - 1) * $pageSize;
|
||||
$cacheKey = $this->generateCacheKey($query, $types, $page, $pageSize);
|
||||
$results = [];
|
||||
|
||||
$results =
|
||||
$this->getCachedResults($cacheKey) ??
|
||||
$this->fetchSearchResults($query, $types, $pageSize, $offset);
|
||||
$results = $this->getCachedResults($cacheKey) ?? $this->fetchSearchResults($query, $types, $pageSize, $offset);
|
||||
|
||||
if (empty($results) || empty($results["data"])) {
|
||||
$this->sendResponse(["results" => [], "total" => 0, "page" => $page, "pageSize" => $pageSize], 200);
|
||||
|
@ -35,7 +32,6 @@ class SearchHandler extends BaseHandler
|
|||
}
|
||||
|
||||
$this->cacheResults($cacheKey, $results);
|
||||
|
||||
$this->sendResponse(
|
||||
[
|
||||
"results" => $results["data"],
|
||||
|
@ -57,13 +53,8 @@ class SearchHandler extends BaseHandler
|
|||
|
||||
$query = trim($query);
|
||||
|
||||
if (strlen($query) > 255) throw new \Exception(
|
||||
"Invalid 'q' parameter. Exceeds maximum length of 255 characters."
|
||||
);
|
||||
|
||||
if (!preg_match('/^[a-zA-Z0-9\s\-_\'"]+$/', $query)) throw new \Exception(
|
||||
"Invalid 'q' parameter. Contains unsupported characters."
|
||||
);
|
||||
if (strlen($query) > 255) throw new \Exception("Invalid 'q' parameter. Exceeds maximum length of 255 characters.");
|
||||
if (!preg_match('/^[a-zA-Z0-9\s\-_\'"]+$/', $query)) throw new \Exception("Invalid 'q' parameter. Contains unsupported characters.");
|
||||
|
||||
$query = preg_replace("/\s+/", " ", $query);
|
||||
|
||||
|
@ -84,10 +75,7 @@ class SearchHandler extends BaseHandler
|
|||
);
|
||||
$invalidTypes = array_diff($types, $allowedTypes);
|
||||
|
||||
if (!empty($invalidTypes)) throw new Exception(
|
||||
"Invalid 'type' parameter. Unsupported types: " .
|
||||
implode(", ", $invalidTypes)
|
||||
);
|
||||
if (!empty($invalidTypes)) throw new Exception("Invalid 'type' parameter. Unsupported types: " . implode(", ", $invalidTypes));
|
||||
|
||||
return $types;
|
||||
}
|
||||
|
@ -98,17 +86,14 @@ class SearchHandler extends BaseHandler
|
|||
int $pageSize,
|
||||
int $offset
|
||||
): array {
|
||||
$typesParam =
|
||||
$types && count($types) > 0 ? "%7B" . implode(",", $types) . "%7D" : "";
|
||||
$typesParam = $types && count($types) > 0 ? "%7B" . implode(",", $types) . "%7D" : "";
|
||||
$endpoint = "rpc/search_optimized_index";
|
||||
$queryString =
|
||||
"search_query=" .
|
||||
urlencode($query) .
|
||||
"&page_size={$pageSize}&page_offset={$offset}" .
|
||||
($typesParam ? "&types={$typesParam}" : "");
|
||||
|
||||
$data = $this->makeRequest("GET", "{$endpoint}?{$queryString}");
|
||||
|
||||
$total = count($data) > 0 ? $data[0]["total_count"] : 0;
|
||||
$results = array_map(function ($item) {
|
||||
unset($item["total_count"]);
|
||||
|
@ -125,6 +110,7 @@ class SearchHandler extends BaseHandler
|
|||
int $pageSize
|
||||
): string {
|
||||
$typesKey = $types ? implode(",", $types) : "all";
|
||||
|
||||
return sprintf(
|
||||
"search:%s:types:%s:page:%d:pageSize:%d",
|
||||
md5($query),
|
||||
|
@ -138,6 +124,7 @@ class SearchHandler extends BaseHandler
|
|||
{
|
||||
if ($this->cache instanceof \Redis) {
|
||||
$cachedData = $this->cache->get($cacheKey);
|
||||
|
||||
return $cachedData ? json_decode($cachedData, true) : null;
|
||||
} elseif (is_array($this->cache)) {
|
||||
return $this->cache[$cacheKey] ?? null;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue