Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Extend valid link cache time to 30 days and make cache expiration times minimally configurable #878

Merged

Conversation

sarayourfriend
Copy link
Contributor

@sarayourfriend sarayourfriend commented Aug 11, 2022

Fixes

Fixes #867 by @obulat

Description

Makes the link validation cache expiration times minimally configurable and increases the valid link cache time to 30 days.

I have another patch that introduces more flexible configuration by status code, in case that is something we want to explore.

Testing Instructions

Checkout this branch and just down && just up to ensure you get the latest code. Smoke test the app around link validation and confirm that things are getting cached. You can do so by observing the log output. For example, a request to /v1/images/ will output a bunch of warning logs about making insecure requests to staticflickr the first time but should not output any on the second request.

If you want to confirm the cache times more explicitly, you'll need to upgrade redis locally to version 7 so that you can use the EXPIRETIME command. To do this, update the cache image tag from 4.x.x to 7:

diff --git a/docker-compose.yml b/docker-compose.yml
index beed92cc..11ebea67 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -33,7 +33,7 @@ services:
       test: "pg_isready -U deploy -d openledger"
 
   cache:
-    image: redis:4.0.10
+    image: redis:7
     ports:
       - "6379:6379"

Then run redis-cli locally and try the following:

# copy one of these keys
> keys "valid*"
# find the status code cached for that link
> get "<copied key>"
# see the expire time set for the key
> expiretime "<copied key>"

expiretime returns the Unix timestamp from origin so you'll have to do some math to confirm that it matches your expectation based on when the key was inserted. To clear your local Redis, run redis-cli flushall. This is useful for trying this process a couple times to confirm it's working as expected.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@sarayourfriend sarayourfriend requested a review from a team as a code owner August 11, 2022 19:52
@openverse-bot openverse-bot added ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository 🟧 priority: high Stalls work on the project or its dependents labels Aug 11, 2022
@github-actions
Copy link

github-actions bot commented Aug 11, 2022

API Developer Docs Preview: Ready

https://wordpress.github.io/openverse-api/_preview/878

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

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

This is great! All I have are bikeshedding comments.

@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

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 the env.template file?

Copy link
Contributor Author

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?

Copy link
Member

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 to config was set by the caller to _get_expiry. Please ignore my suggestion.

@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

I think a variable called thirty_days_seconds might make this clearer.

@@ -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)
Copy link
Member

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 the env.template file?

Copy link
Contributor

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you for the thorough testing instructions as well, everything worked as expected when I tried locally 🚀

@sarayourfriend sarayourfriend merged commit 82300a3 into main Aug 12, 2022
@sarayourfriend sarayourfriend deleted the add/configurable-link-validation-caching-expiration branch August 12, 2022 22:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟧 priority: high Stalls work on the project or its dependents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Increase the dead links cache expiration timeout
4 participants