Skip to content

Commit 394acb1

Browse files
authored
Don't cache search queries where the cache hit chance is low. (#8659)
1 parent 62bba2c commit 394acb1

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Important changes to data models, configuration, and migrations between each
22
AppEngine version, listed here to ease deployment and troubleshooting.
33

44
## Next Release (replace with git tag when deployed)
5+
* Note: search queries with multiple components are not cached.
56

67
## `20250320t094500-all`
78
* Bump runtimeVersion to `2025.03.18`.

app/lib/package/search_adapter.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class SearchAdapter {
3535
///
3636
/// When the `search` service fails, it falls back to use the name tracker to
3737
/// provide package names and perform search in that set.
38-
Future<SearchResultPage> search(SearchForm form,
39-
{required String? rateLimitKey}) async {
38+
Future<SearchResultPage> search(
39+
SearchForm form, {
40+
required String? rateLimitKey,
41+
}) async {
4042
final result = (await _searchOrFallback(form, rateLimitKey, true))!;
4143
final views = await _getPackageViewsFromHits([...result.packageHits]);
4244
return SearchResultPage(

app/lib/search/search_client.dart

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ class SearchClient {
6060

6161
final serviceUrlParams = Uri(queryParameters: query.toUriQueryParameters());
6262

63+
// Don't use cache in cases where there is a high chance of cache miss.
64+
// This is a rough heuristic, counting the distinct components in the
65+
// user-provided query. Such components are:
66+
// - free form text (counts as 1 regardless of word count)
67+
// - `sdk:...`, `platform:...` and other tags (counts as 1 each)
68+
if (query.parsedQuery.componentCount > 2) {
69+
skipCache = true;
70+
}
71+
6372
// returns null on timeout (after 5 seconds)
6473
Future<http.Response?> doCallHttpServiceEndpoint({String? prefix}) async {
6574
final httpHostPort = prefix ?? activeConfiguration.searchServicePrefix;

pkg/_pub_shared/lib/search/search_form.dart

+7
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ class ParsedQueryText {
324324
!hasAnyDependency &&
325325
tagsPredicate.isEmpty;
326326

327+
int get componentCount =>
328+
(text == null || text!.isEmpty ? 0 : 1) +
329+
(packagePrefix == null ? 0 : 1) +
330+
refDependencies.length +
331+
allDependencies.length +
332+
tagsPredicate._values.length;
333+
327334
@override
328335
String toString() {
329336
if (hasOnlyFreeText) return text!;

0 commit comments

Comments
 (0)