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

Improve cache when fetching lessons #108

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Conversation

ilicfilip
Copy link
Collaborator

Context

Due to the way how currently our code works fetching lessons from API is done on every init (I get it even on front end, not just in Dashboard).

If request is successful the response is cached, but if it is not our server will be hammered. To prevent that I cached empty array with 5 minutes lifetime in case that request fails.

Another problem is that $url can be filtered, but the cache key is never changed - meaning that even if $url is changed user will keep getting lessons which were fetched from old $url until the cache expires (which can be up to a week).

I used $url in order to make $cache_key unique and left it unchanged in order to make possible (future) debugging easier.

Quality assurance

  • I have tested this code to the best of my abilities.
  • I have added unit tests to verify the code works as intended.
  • I have checked that the base branch is correctly set.

Copy link
Contributor

github-actions bot commented Nov 6, 2024

Test on Playground
Test this pull request on the Playground or download the zip.

'https://progressplanner.com/wp-json/progress-planner-saas/v1/free-lessons'
);

$cache_key = 'lessons-' . $url;
Copy link
Member

Choose a reason for hiding this comment

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

It would probably be safer if we md5() the URL...

Suggested change
$cache_key = 'lessons-' . $url;
$cache_key = 'lessons-' . md5( $url );

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, I left it just because of the debugging - any hash will do the job.

@aristath aristath merged commit 4bb4746 into develop Nov 7, 2024
18 checks passed
@ilicfilip ilicfilip deleted the filip/improve-lessons-caching branch November 7, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants