Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Caching is not working #68

Open
Hareramrai opened this issue Jul 26, 2016 · 3 comments
Open

Query Caching is not working #68

Hareramrai opened this issue Jul 26, 2016 · 3 comments

Comments

@Hareramrai
Copy link

Hi All,

I am using below two gem for our Sinatra Application.

gem 'sinatra' #  version (1.4.7) 
gem 'sinatra-activerecord' #  version(2.0.9)

We are feeling that active record query caching is not working. Please see below log of our application's console.

10.times{ User.first }
D, [2016-07-26T13:58:31.493868 #13885] DEBUG -- :   User Load (2.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.564411 #13885] DEBUG -- :   User Load (3.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.569058 #13885] DEBUG -- :   User Load (0.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.576557 #13885] DEBUG -- :   User Load (5.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.584894 #13885] DEBUG -- :   User Load (3.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.588594 #13885] DEBUG -- :   User Load (0.9ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.591352 #13885] DEBUG -- :   User Load (0.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.607551 #13885] DEBUG -- :   User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.609229 #13885] DEBUG -- :   User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1
D, [2016-07-26T13:58:31.612887 #13885] DEBUG -- :   User Load (2.6ms)  SELECT  `users`.* FROM `users` WHERE `users`.`active` = 1  ORDER BY `users`.`id` ASC LIMIT 1

It shouldn't load user record for every time. Any help is appreciated.

Thanks,
Hare

@oogali
Copy link

oogali commented Aug 1, 2016

Did you explicitly enabled query caching?

use ActiveRecord::QueryCache

@sskylar
Copy link

sskylar commented Oct 30, 2016

Running into a similar issue, tried to add use ActiveRecord::QueryCache which resulted in the following error:

ArgumentError: wrong number of arguments (given 1, expected 0)
    /Users/skylar/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib/rack/builder.rb:86:in `initialize'

Using:

  • rack (1.6.4)
  • sinatra (1.4.7)
  • sinatra-activerecord (2.0.11)
  • activerecord (5.0.0.1)

This appears to be the same error/issue as described here in a similar project: jhollinger/otr-activerecord#7

Workaround is to add this after a connection has been established:

ActiveRecord::Base.connection.enable_query_cache!

With above, I can confirm that query caching does work.

@oogali
Copy link

oogali commented Oct 30, 2016

My comment is specific for Rails 4, and I think your comment is specific for Rails 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants