您可以选择性地打开(或关闭)代码块的缓存。您可以使用延迟作业使用的方法来执行此操作。
User.cache do
# caching is turned on in this block
User.find(18)
User.find(18)
end
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 18]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 18]]
如果要禁用块的缓存:
User.uncached do
# caching is off for this block
User.find(18)
User.find(18)
end
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]