Caching POST request and responses
MattNguyen opened this issue · 4 comments
Does Garner support caching post requests? Sorry, I didn't know where else to post this question. Thanks!
It depends. Can you describe a scenario, please?
My application makes an idempotent POST request to one endpoint on a server, since the parameters being sent over exceed the parameter limits on GET requests. On the server, resources are constructed through fairly complex joins and are served through Grape API. This POST endpoint searches for a single resource as well as batch searches for multiple resources, depending on the parameters. I'd like to cache these resources regardless of whether a single resource or batch of resources were requested. I'd like to preface that I'm a neophyte to HTTP caching and rack middleware, so I'd love to be enlightened. Thanks.
@MattNguyen Garner should handle caching within a POST request just as it would in a GET request. Under ordinary circumstances this could be a dangerous thing, but if your endpoint is in fact nullipotent (I don't think idempotence is sufficient), then caching should be safe and should function just as in a GET request.
As to your comment
I'd like to cache these resources regardless of whether a single resource or batch of resources were requested.
Do you mean that you want to cache a single, identical result regardless of query parameters? By default, Garner's RequestGet
strategy will only consider GET parameters (in the query string) and not POST parameters (in the body). This means that two requests made to the same path will return identical results for the same cache
block (assuming identical bind
arguments), regardless of any variation in the POST parameters supplied to the two requests.
If this is not what you want, and you want to cache different results based on different POST parameters, you can create a custom strategy, along the lines of the RequestGet
strategy code. Just replace GET
with POST
, and make sure to include your custom strategy in the Garner::Cache::ObjectIdentity::KEY_STRATEGIES
array.
Does that help answer your question?
Absolutely. Thank you very much. I haven't gotten a chance to actually implement Garnet, so I'll give it a try and see what happens.