open5e / open5e-api

The api for open5e.com
Other
153 stars 96 forks source link

Set up caching of common queries #281

Open eepMoody opened 1 year ago

eepMoody commented 1 year ago

https://www.django-rest-framework.org/api-guide/caching/ seems to provide an out-of-the-box solution that would serve us nicely, but it would be nice if we could just set it and forget it somewhere central.

https://github.com/Suor/django-cacheops seems to support about the right versions for us, and claims to also just generally make Django faster, might be worth a look.

Implementation thoughts

Sturlen commented 1 year ago
  • Due to the nature of our server, caches never need to expire and everything is safe to cache.

This isn't necessarily true due to errata. No expiration would mean that someone could have cached the wrong number for a saving throw never updating it. Therefore I think a cache time of several hours or possibly days would be better.

For caching in general a CDN could be useful. This would potentially skip the server entirely for common queries such as /monsters. Also requires no manual work, since Cloudflare or another provider would just store requests automatically as serve them as needed.

samuelrey commented 1 year ago

Has a decision been made here about CDN vs server-side caching?

Sturlen commented 1 year ago

For this situation I'd say a CDN is superior since it avoids using any server resources. It will have to be set at the DNS level though.

augustjohnson commented 6 months ago

Yeah strongly recommend just using a CDN, and we currently have cloudflare in-line. I believe this should be our default solution until a problem arises.

Sturlen commented 6 months ago

From what I can see, Cloudflare caching is not currently enabled on the API. Here's a comparison between fetching all monsters from api.open5e.com and my own mirror. The mirror has cf caching enabled. image

As such I don't think this should be marked as completed just yet.

augustjohnson commented 6 months ago

Ok, yes, that is pretty damning. 8 seconds!?!?!? damn. I'll see what I can see.

augustjohnson commented 2 months ago

I poked around in cloudflare and couldn't see anything obv. @Sturlen I may want to sync up with you to troubleshoot this.

augustjohnson commented 4 days ago

Functionally, we treat the api as a static site basically. The issue is that we deploy once in a while, and so our cache would need to be invalidated each deploy. I don't believe we have any deploy hooks into CF at this point.

I think we should be able to edit it using cloudflares caching, but clearly there's some stuff to sort out.