0

如何缓存我的所有查询。

第一个请求:

@user = User.all

第二个请求

@user = User.all # Cached

有什么有用的插件可以做到这一点吗?

我在生产模式。我似乎看不到任何缓存的短语来确认我的 Rails 应用程序正在缓存。

4

1 回答 1

0

in your config/environments/production.rb

config.action_controller.perform_caching = true

caching is disabled by default for development and test, and enabled for production.

Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.

For example:

 class UsersController < ActionController

 def index
   # Run a find query
   @users = User.all

   ...

   # Run the same query again
  @users = User.all

   end

 end

The second time the same query is run against the database, it’s not actually going to hit the database. The first time the result is returned from the query it is stored in the query cache (in memory) and the second time it’s pulled from memory.

Check it at your console

User Load (?ms) SELECT User.* FROM User

Completed 200 OK in ?ms (Views: ?ms | ActiveRecord: ?ms)

You can compare it with first time load and second time load.

You can use Dalli gem from https://github.com/mperham/dalli for store your cache to memcached server.

于 2012-09-19T06:31:13.027 回答