Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache full search results. #4382

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions internals/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

MAX_TERMS = 6
DEFAULT_RESULTS_PER_PAGE = 100

SEARCH_CACHE_TTL = 60 * 60 # One hour

def process_exclude_deleted_unlisted_query() -> Future:
"""Return a future for all features, minus deleted and unlisted."""
Expand Down Expand Up @@ -319,9 +319,14 @@ def make_cache_key(
"""Return a redis key string to store cached search results."""
return '|'.join([
KyleJu marked this conversation as resolved.
Show resolved Hide resolved
FeatureEntry.SEARCH_CACHE_KEY,
user_query, str(sort_spec), str(show_unlisted),
str(show_deleted), str(show_enterprise), str(start), str(num),
str(name_only),
user_query,
'sort_spec=' + str(sort_spec),
'show_unlisted=' + str(show_unlisted),
'show_deleted=' + str(show_deleted),
'show_enterprise=' + str(show_enterprise),
'start=' + str(start),
'num=' + str(num),
'name_only=' + str(name_only),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we only cache name_only queries for now. This part of the key is optional

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is best to include it, just in case we decide to broaden what we cache.

])


Expand All @@ -344,6 +349,7 @@ def is_cacheable(user_query: str, name_only: bool):
logging.info('Search query can be cached')
return True

KyleJu marked this conversation as resolved.
Show resolved Hide resolved

def process_query_using_cache(
user_query: str,
sort_spec: str | None = None,
Expand All @@ -362,8 +368,8 @@ def process_query_using_cache(
if is_cacheable(user_query, name_only):
logging.info('Checking cache at %r', cache_key)
cached_result = rediscache.get(cache_key)
if cached_result:
logging.info('Found cached search result')
if cached_result is not None:
logging.info('Found cached search result for %r', cache_key)
return cached_result

logging.info('Computing search result')
Expand All @@ -374,7 +380,7 @@ def process_query_using_cache(

if is_cacheable(user_query, name_only):
logging.info('Storing search result in cache: %r', cache_key)
rediscache.set(cache_key, computed_result)
rediscache.set(cache_key, computed_result, SEARCH_CACHE_TTL)

return computed_result

Expand Down
8 changes: 6 additions & 2 deletions internals/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,14 @@ def test_sort_by_total_order__multiple_items(self):
def test_make_cache_key(self):
"""We can make a search cache key."""
self.assertEqual(
'FeatureSearch||None|True|False|False|0|100|True',
('FeatureSearch||sort_spec=None|show_unlisted=True|'
'show_deleted=False|show_enterprise=False|'
'start=0|num=100|name_only=True'),
search.make_cache_key('', None, True, False, False, 0, 100, True))
self.assertEqual(
'FeatureSearch|canvas|created.when|False|True|True|1|20|False',
('FeatureSearch|canvas|sort_spec=created.when|show_unlisted=False|'
'show_deleted=True|show_enterprise=True|'
'start=1|num=20|name_only=False'),
search.make_cache_key(
'canvas', 'created.when', False, True, True, 1, 20, False))

Expand Down