0

Is it possible to cache once produced response on server-side and then redeliver it in response to the same request? Let me explain: I have an endloint that takes about 5 seconds to generate a response - this includes going to the database and fetching data, processing it, performing some computations on it, serealizing and gzipping the response - the entire thing takes 5 seconds. Once this is done for the first time I want the result to be available for all the requests coming from all the users. In my views client side caching, when you either cache the result on the client and do not hit the server at all for some time or when you hit the server but get 304 not-changed instead of the data is not good enough. What i want is to hit the sever and if this enndpoint (with the same set of parameters) was already called by anyone then get the full response. Is it possible at all?

Bashir Magomedov
  • 2,841
  • 4
  • 28
  • 44

1 Answers1

0

You have a number of options for this.

One option is API level caching, you create a key using the parameters required to generate the response, you go and fetch the data and save the pair in the cache. Then next time a request comes in, you recreate the key and go and check your cache first. If it's there, happy days, return it, if not, go fetch it and store it.

This of course depends on the amount of data you have, too muchm data or too big data and this will not work. You could also store it for a while, say 10 minutes, 1 hour etc.

If you have a lot of data and caching like this isn't possible then consider something else. Maybe create your own no-sql cache store ( using something like MongoDB maybe ),store it and retrieve it from there, without the need for any changes so it's a straight retrieve, thus very quick.

You could also use something like Redis Cache. Lots of options, just choose whatever is appropriate.

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32