codeforamerica / ohanakapa-ruby

A Ruby wrapper for https://github.com/codeforamerica/ohana-api
Other
5 stars 5 forks source link

Prevent excessive HTTP requests with caching #1

Open anselmbradford opened 11 years ago

anselmbradford commented 11 years ago

Per recommendations on http://stackoverflow.com/questions/3200207/how-to-design-a-language-binding-to-a-web-api

ycombinator commented 11 years ago

I'm assuming your HTTP API responds with appropriate Cache-Control headers. Make sure the wrapper respects those whenever possible.

ycombinator commented 11 years ago

Another nice optimization that goes hand-in-hand with HTTP caching is conditional GETs.

anselmbradford commented 11 years ago

Oops, is this an issue for the wrapper or the API? Sounds like the API.

ycombinator commented 11 years ago

Its on both sides. The HTTP API has to respond with the right Cache-Control headers but the Ruby binding has to respect them when performing the actual caching.

monfresh commented 11 years ago

The API is already caching requests for 5 minutes, as defined on line 5 of the controller: https://github.com/codeforamerica/ohana-api/blob/master/app/controllers/api/v1/organizations_controller.rb

monfresh commented 11 years ago

Here's a sample header response:

HTTP/1.1 200 OK Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT Access-Control-Allow-Origin: * Access-Control-Expose-Headers: Access-Control-Max-Age: 1728000 Age: 0 Cache-Control: public Connection: keep-alive Content-Length: 1726 Content-Type: application/json; charset=utf-8 Date: Thu, 06 Jun 2013 19:50:30 GMT Etag: "221f7bcd4c7bbdd8990a30af24262472:6f80b4a5b060d69047fe293da20f060f" Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14) X-Content-Digest: 2152939d299185d02ec7e438a07816dee281fed3 X-Rack-Cache: miss, store X-Request-Id: 9085a1723a4b752d9ef14985a7a9325a X-Runtime: 0.083550 X-Ua-Compatible: IE=Edge,chrome=1

monfresh commented 11 years ago

And one for a cached request: HTTP/1.1 200 OK Age: 34 Cache-Control: max-age=300, public Connection: keep-alive Content-Length: 56073 Content-Type: application/json; charset=utf-8 Date: Thu, 06 Jun 2013 19:54:16 GMT Etag: "ce1eddda1c128dda97f5f052d1fa660f" Server: WEBrick/1.3.1 (Ruby/2.0.0/2013-05-14) X-Content-Digest: 9c2426e973bc09bd7ba7ba809edb64f6e290b420 X-Rack-Cache: fresh X-Request-Id: 72f4ca4aee545d05292bb6aada9fb29f X-Runtime: 0.120983 X-Ua-Compatible: IE=Edge,chrome=1