Forums

Caching External API requests for faster loading

I would like to cache external API requests with memcache so I don't have to wait for the API to return the data for every request. I have several APIs that I use within one function in a django view. Since this takes a long time to work; is it possible to cache it. Do i need to install anything else to make caching work?

My other option is to break the function apart to speed it up, I am wondering if I can use one web worker that can be assigned to an API request in the view and how to assign the webworker to the task.

Finally, Icing on the cake if you could answer this. Is it possible to have one webworker for each external API request and aggregate it into a dictionary to return for a view? Otherwise, I am thinking I need AJAX for loading a page and I hate javascript.

Thanks!

You can do caching, on a per worker basis (that is, each worker will have its own cache) by putting things you want to cache in global variable structures. You could also write to /dev/shm which is an in-memory filesystem that will be shared between your workers.

Web workers are dynamically assigned to respond to requests, there is no way to turn them into background workers like you seem to want to do.

Thanks for the info glenn; I would definitely use the /dev/shm file system to use file based cache. I have a couple follow up questions.

  1. How often does /dev/shm go down or reset?
  2. How long are webworkers assigned to your webapp? Is it the same couple of webworkers that are used for your webapp, but that pool is dynamically assigned?

Also, is there a size limit for the /dev/shm that can be used?

Oops. I meant to include that in the original response: It's 1Mb

Can you pay to increase it?

Not at the moment, no. But that's not a bad idea.