open5e-api icon indicating copy to clipboard operation
open5e-api copied to clipboard

Set up caching of common queries

Open eepMoody opened this issue 2 years ago • 8 comments

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

  • Due to the nature of our server, caches never need to expire and everything is safe to cache.
  • A job to generate some subset of common caches could be run at startup, but this is probably unnecessary.
  • In particular, the list views would be highly valuable to cache. Individual items are accessed far more rarely, and respond very quickly.

eepMoody avatar Jun 26 '23 14:06 eepMoody

  • 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.

Sturlen avatar Jun 26 '23 17:06 Sturlen

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

samuelrey avatar Oct 02 '23 00:10 samuelrey

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.

Sturlen avatar Oct 02 '23 11:10 Sturlen

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.

augustjohnson avatar Mar 30 '24 12:03 augustjohnson

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.

Sturlen avatar Mar 30 '24 13:03 Sturlen

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

augustjohnson avatar Mar 30 '24 16:03 augustjohnson

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

augustjohnson avatar Aug 08 '24 22:08 augustjohnson