codethesaur.us icon indicating copy to clipboard operation
codethesaur.us copied to clipboard

Use django caching to improve TTFB

Open NathanBeddoeWebDev opened this issue 2 years ago • 20 comments

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?

NathanBeddoeWebDev avatar May 18 '22 07:05 NathanBeddoeWebDev

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 avatar May 18 '22 14:05 geekygirlsarah

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. :)

LittleBigProgramming avatar Oct 02 '22 16:10 LittleBigProgramming

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.

geekygirlsarah avatar Oct 02 '22 16:10 geekygirlsarah

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.

github-actions[bot] avatar Oct 17 '22 00:10 github-actions[bot]

@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 😄

LittleBigProgramming avatar Oct 17 '22 12:10 LittleBigProgramming

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?

geekygirlsarah avatar Oct 17 '22 16:10 geekygirlsarah

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 avatar Oct 17 '22 16:10 LittleBigProgramming

@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! 😄

LittleBigProgramming avatar Oct 20 '22 18:10 LittleBigProgramming

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.

geekygirlsarah avatar Oct 22 '22 15:10 geekygirlsarah

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[bot] avatar Nov 06 '22 01:11 github-actions[bot]

Commenting as PR is still open.

geekygirlsarah avatar Nov 06 '22 03:11 geekygirlsarah

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.

github-actions[bot] avatar Nov 20 '22 12:11 github-actions[bot]

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

geekygirlsarah avatar Nov 20 '22 14:11 geekygirlsarah

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[bot] avatar Dec 05 '22 01:12 github-actions[bot]

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.

github-actions[bot] avatar Dec 08 '22 01:12 github-actions[bot]

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

geekygirlsarah avatar Dec 08 '22 01:12 geekygirlsarah

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[bot] avatar Dec 22 '22 12:12 github-actions[bot]

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!

geekygirlsarah avatar Dec 23 '22 02:12 geekygirlsarah

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[bot] avatar Jan 06 '23 12:01 github-actions[bot]

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.

github-actions[bot] avatar Jan 09 '23 12:01 github-actions[bot]