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

Use django caching to improve TTFB #487

Open
NathanBeddoeWebDev opened this issue May 18, 2022 · 20 comments · May be fixed by #646
Open

Use django caching to improve TTFB #487

NathanBeddoeWebDev opened this issue May 18, 2022 · 20 comments · May be fixed by #646
Labels
Experience: Backend good first issue A good issue for anyone new to open source to work on hacktoberfest Good issue for someone to work on for Hacktoberfest help wanted We'd love to have help working on this issue Type: Optimization

Comments

@NathanBeddoeWebDev
Copy link

Description

To improve Core Web Vitals, we can cache pages that aren't regularly updated using Django's caching. In Memory caching is available so you don't have to run up a redis server or anything.
https://docs.djangoproject.com/en/4.0/topics/cache/#local-memory-caching-1

Requirements

Set up caching config as per the docs above.
Make a decision on what views can be cached, and which pages shouldn't be.
How long should it cache in memory for?

@geekygirlsarah geekygirlsarah added help wanted We'd love to have help working on this issue good first issue A good issue for anyone new to open source to work on Experience: Backend Priority: Low Type: Optimization labels May 18, 2022
@geekygirlsarah
Copy link
Member

This is a great idea, thanks for adding it! Let me know if you're interested in working on it too and I can add you to the issue.

@geekygirlsarah geekygirlsarah added hacktoberfest Good issue for someone to work on for Hacktoberfest and removed Priority: Low labels Sep 17, 2022
@LittleBigProgramming
Copy link
Contributor

What views would this be worth adding too?

The default is 300 which is 5 minutes. Is this the timeout that we would want to use? Or would there be a preferred time.

I’d be interested working on this as it sounds like a good suggestion. :)

@geekygirlsarah
Copy link
Member

I'll give it to you then!

As for views: All of them? I think they're all pretty cachable.

As for time, you can probably do longer. Content doesn't change for the most part. Could probably be an hour to a day even.

@github-actions
Copy link

github-actions bot commented Oct 17, 2022

This issue has been inactive for 344 hours (14.33 days) and will be unassigned after 64 more hours (2.67 days). If you have questions, let @geekygirlsarah know by comment, email, or Twitter.

If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@LittleBigProgramming
Copy link
Contributor

@geekygirlsarah When trying to use the local memory caching with the following settings:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-snowflake',
        'TIMEOUT': 300,
        'OPTIONS': {
            'MAX_ENTRIES': 1000
        }
    }
}

I am getting this, presumably being the queryset is being stored and not being able to be parsed out.

Screenshot 2022-10-17 at 13 28 29

I managed to get site wide caching (https://docs.djangoproject.com/en/4.1/topics/cache/#the-per-site-cache) working using the FileBasedCache

Screenshot 2022-10-17 at 13 36 41

Please let me know if this works for you and I will raise a PR for it 😄

@geekygirlsarah
Copy link
Member

geekygirlsarah commented Oct 17, 2022

Uh... I'm not sure. I can look into it, but what's the default that's not a FileBasedCache? I wonder if it makes sense since Heroku's instances are ephimeral, so the caches may not last if they're stored on a file system. Database-based one?

@LittleBigProgramming
Copy link
Contributor

The default that isn't is https://docs.djangoproject.com/en/4.1/topics/cache/#local-memory-caching. That is the one which throws the error. It also says

This also means the local memory cache isn’t particularly memory-efficient, so it’s probably not a good choice for production environments. It’s nice for development.

The file based one would belong on the Heroku instance for it's lifecycle. Which if you use one instance and it creates another would cache on the first visit pointed to that new instance. (As far as I know).

The other options are Redis, Memcached or Database caching as you mentioned which looks at though it would all go in one cache table. https://docs.djangoproject.com/en/4.1/topics/cache/#database-caching I'm not sure

What do you think?

@LittleBigProgramming
Copy link
Contributor

@geekygirlsarah I've been thinking if it runs on one instance then File Based Caching might be okay for the per site caching.

Or would you rather I looked into the database caching?

I'm not familiar with the infrastructure of the app so I'm happy to follow your lead! 😄

@geekygirlsarah
Copy link
Member

Yeah, it runs on one instance, but that instance is destroyed after every PR added in. So like today it might get destroyed after 2-3 PRs that I merge in. During slower months, the cache would build up. During October, the file-based cache probably wouldn't help much.

I am thinking the DB caching might make more sense. Local development uses SQLite, production uses Heroku. Either way, Django knows about the DB, so I think it's mostly a matter of asking Django to make the migration to make the right table, then set up the caching to store it there.

@LittleBigProgramming LittleBigProgramming linked a pull request Oct 27, 2022 that will close this issue
4 tasks
@github-actions
Copy link

github-actions bot commented Nov 6, 2022

This issue has been inactive for 346 hours (14.42 days) and will be unassigned after 62 more hours (2.58 days). If you have questions, please leave a comment, message @codethesaurus or @geekygirlsarah on Twitter, or email [email protected].If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@geekygirlsarah
Copy link
Member

Commenting as PR is still open.

@geekygirlsarah geekygirlsarah removed the hacktoberfest Good issue for someone to work on for Hacktoberfest label Nov 10, 2022
@github-actions
Copy link

This issue has been inactive for 345 hours (14.38 days) and will be unassigned after 63 more hours (2.63 days). If you have questions, please leave a comment, message @codethesaurus or @geekygirlsarah on Twitter, or email [email protected].If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@geekygirlsarah
Copy link
Member

Commenting as PR is still open and I haven't tested it yet

@github-actions
Copy link

github-actions bot commented Dec 5, 2022

This issue has been inactive for 347 hours (14.46 days) and will be unassigned after 61 more hours (2.54 days). If you have questions, please leave a comment, message @codethesaurus or @geekygirlsarah on Twitter, or email [email protected].If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@github-actions
Copy link

github-actions bot commented Dec 8, 2022

This issue has been inactive for 419 hours (17.46 days) and is past the limit of 408 hours (17.00 days) so is being unassigned.You’ve just been unassigned from this ticket due to inactivity – but feel free to pick it back up (or a new one!) in the future! Thank you for your interest in contributing to this project.

@geekygirlsarah
Copy link
Member

I still haven't tested this. Oops. I should really do that...

@github-actions
Copy link

This issue has been inactive for 347 hours (14.46 days) and will be unassigned after 61 more hours (2.54 days). If you have questions, please leave a comment, message @codethesaurus or @geekygirlsarah on Twitter, or email [email protected].If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@geekygirlsarah
Copy link
Member

This is on my to-do list for this upcoming week, I swear! I even have it on my to-do list for my week off work!

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

This issue has been inactive for 346 hours (14.42 days) and will be unassigned after 62 more hours (2.58 days). If you have questions, please leave a comment, message @codethesaurus or @geekygirlsarah on Twitter, or email [email protected].If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

@github-actions
Copy link

github-actions bot commented Jan 9, 2023

This issue has been inactive for 418 hours (17.42 days) and is past the limit of 408 hours (17.00 days) so is being unassigned.You’ve just been unassigned from this ticket due to inactivity – but feel free to pick it back up (or a new one!) in the future! Thank you for your interest in contributing to this project.

@geekygirlsarah geekygirlsarah added the hacktoberfest Good issue for someone to work on for Hacktoberfest label Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience: Backend good first issue A good issue for anyone new to open source to work on hacktoberfest Good issue for someone to work on for Hacktoberfest help wanted We'd love to have help working on this issue Type: Optimization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants