frafferz/geek

# This is a comment

ActiveRecord::QueryCache and Rack Speed

Ignore this unless you’re using a Rack handler and ActiveRecord.

If you’re using a vanilla rack handler or Grape or JSONRPC2 or something similar that accesses your database via ActiveRecord, and you’re mounting it directly in Rack you’ll probably benefit from using the ActiveRecord::QueryCache. Unless you’re going through the rails stack, you don’t get this for free - you have to ask.

1
  use ActiveRecord::QueryCache

It’s just a standard piece of Rack Middleware, but it turns on DB caching for the duration of the request.

e.g.

1
2
3
4
5
map '/foo/bar' do
  use Rack::Logger
  use ActiveRecord::QueryCache # <-- this increased the speed of my API calls by ~20%
  run MyRackHandler
end