codethesaur.us
codethesaur.us copied to clipboard
Use django caching to improve TTFB
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?
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.
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. :)
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.
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.
@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.
I managed to get site wide caching (https://docs.djangoproject.com/en/4.1/topics/cache/#the-per-site-cache) working using the FileBasedCache
Please let me know if this works for you and I will raise a PR for it 😄
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?
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?
@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! 😄
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.
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.
Commenting as PR is still open.
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.
Commenting as PR is still open and I haven't tested it yet
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.
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.
I still haven't tested this. Oops. I should really do that...
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.
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!
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.
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.