codethesaurus / codethesaur.us

A polyglot developer reference tool to compare programming language concepts side-by-side! Great for learning new languages or using for reference.
https://codethesaur.us
GNU Affero General Public License v3.0
288 stars 167 forks source link

Use django caching to improve TTFB #487

Open NathanBeddoeWebDev opened 2 years ago

NathanBeddoeWebDev commented 2 years ago

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 commented 2 years ago

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.

LittleBigProgramming commented 2 years ago

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 commented 2 years ago

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[bot] commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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.

github-actions[bot] commented 1 year ago

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 coreteam@codethesaur.us.If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

geekygirlsarah commented 1 year ago

Commenting as PR is still open.

github-actions[bot] commented 1 year ago

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 coreteam@codethesaur.us.If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

geekygirlsarah commented 1 year ago

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

github-actions[bot] commented 1 year ago

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 coreteam@codethesaur.us.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] commented 1 year ago

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 commented 1 year ago

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

github-actions[bot] commented 1 year ago

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 coreteam@codethesaur.us.If you are still working on this issue, that's fine. Please comment here to tell the bot to give you more time.

geekygirlsarah commented 1 year ago

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[bot] commented 1 year ago

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 coreteam@codethesaur.us.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] commented 1 year ago

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.