This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Extend valid link cache time to 30 days and make cache expiration times minimally configurable #878
Merged
sarayourfriend
merged 2 commits into
main
from
add/configurable-link-validation-caching-expiration
Aug 12, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
import django_redis | ||
import grequests | ||
from decouple import config | ||
|
||
from catalog.api.utils.dead_link_mask import get_query_mask, save_query_mask | ||
|
||
|
@@ -18,6 +19,10 @@ def _get_cached_statuses(redis, image_urls): | |
return [int(b.decode("utf-8")) if b is not None else None for b in cached_statuses] | ||
|
||
|
||
def _get_expiry(status, default): | ||
return config(f"LINK_VALIDATION_CACHE_EXPIRY__{status}", default=default, cast=int) | ||
|
||
|
||
def validate_images(query_hash, start_slice, results, image_urls): | ||
""" | ||
Make sure images exist before we display them. Treat redirects as broken | ||
|
@@ -69,14 +74,17 @@ def validate_images(query_hash, start_slice, results, image_urls): | |
# Cache successful links for a day, and broken links for 120 days. | ||
if status == 200: | ||
logger.debug("healthy link " f"key={key} ") | ||
pipe.expire(key, twenty_four_hours_seconds) | ||
expiry = _get_expiry(200, twenty_four_hours_seconds * 30) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a variable called |
||
elif status == -1: | ||
logger.debug("no response from provider " f"key={key}") | ||
# Content provider failed to respond; try again in a short interval | ||
pipe.expire(key, thirty_minutes) | ||
expiry = _get_expiry("_1", thirty_minutes) | ||
else: | ||
logger.debug("broken link " f"key={key} ") | ||
pipe.expire(key, twenty_four_hours_seconds * 120) | ||
expiry = _get_expiry("DEFAULT", twenty_four_hours_seconds * 120) | ||
|
||
pipe.expire(key, expiry) | ||
|
||
pipe.execute() | ||
|
||
# Merge newly verified results with cached statuses | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be bikeshedding but the
__
in the name looks weird.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a not-uncommon separator for environment variables: https://github.com/WordPress/openverse-catalog/blob/f5de3e2a5f9c9da62f7e47e9c86b566e3e345123/env.template#L11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's new to the API. But it's cool 👍. Could you move this to
settings
(so that almost all env reading happens in one place) and also add it to theenv.template
file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the
_get_expiry
function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, my bad. I didn't realise that the
default=
argument toconfig
was set by the caller to_get_expiry
. Please ignore my suggestion.