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 fulltext search results #10167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

OutstandingWork
Copy link

@OutstandingWork OutstandingWork commented Dec 18, 2024

Closes #10051

This PR achives reduction in time taken to show no results by about 0.5-0.8 s

Technical

The key differences are:

The modified code added explicit caching headers for successful responses
The modified code added specific no-cache headers for error responses
The modified code had more sophisticated header management

Testing

The above technical changes are done in the file in the path openlibrary\plugins\inside\code.py
The change is simple and just involved understanding the codebase and adding relevant codeline for caching.
It can be implemented by simply building the docker image via docker-compose build and then running it via docker-compose up.

Stakeholders

I have worked on this issue with @jimchamp as lead.

@jimchamp jimchamp changed the title Added explicit caching headers for successful responses Cache fulltext search results Dec 20, 2024
Copy link
Collaborator

@jimchamp jimchamp left a comment

Choose a reason for hiding this comment

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

The requirement was to cache the partially rendered HTML that replaces the loading indicator on "no results found" search pages. You've cached the JSON full-text search results.

Here is where the data is fetched for the partially rendered HTML. If those results are error-free, add the Cache-Control header. That is all that is needed.

@OutstandingWork
Copy link
Author

Should I remove my previous changes and resubmit only the changes needed or should I keep my previous change and the change you suggested?

Comment on lines +44 to +54

# Add caching headers only if there's no error
if 'error' not in results:
# Cache for 5 minutes (300 seconds)
web.header('Cache-Control', 'public, max-age=300')
web.header('Expires', web.http.expires(300))
else:
# No caching for error responses
web.header('Cache-Control', 'no-store, no-cache, must-revalidate')
web.header('Pragma', 'no-cache')

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Add caching headers only if there's no error
if 'error' not in results:
# Cache for 5 minutes (300 seconds)
web.header('Cache-Control', 'public, max-age=300')
web.header('Expires', web.http.expires(300))
else:
# No caching for error responses
web.header('Cache-Control', 'no-store, no-cache, must-revalidate')
web.header('Pragma', 'no-cache')

Copy link
Author

@OutstandingWork OutstandingWork Dec 20, 2024

Choose a reason for hiding this comment

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

Screenshot 2024-12-21 010434
Hi @jimchamp , I have done the added the above lines of code and removed my previous ones. It works as expected.
I guess you can merge this now.

Copy link
Collaborator

@jimchamp jimchamp Dec 20, 2024

Choose a reason for hiding this comment

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

You'll have to push your changes before they can be reviewed and merged. Remove the Expires header before you do so.
Also add a note about how you tested this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, remove the not hits['hits'] part of the conditional.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @jimchamp, I have pushed my changes as you told. Please do check once and let me know if the results are as expected.

@jimchamp
Copy link
Collaborator

Remove the code that you've committed. Only submit what is necessary to close the linked issue.

@jimchamp jimchamp added the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve no search results page by caching "search inside" response
2 participants