Rails 缓存简介
前言
合理的使用缓存可以很大程度上提高网站性能,是网站性能优化必不可少的一部分。
缓存
Model 层缓存
通过手动设置可以将部分查询结果存储在对应的缓存系统中。
1
2
3Rails.cache.fetch('all_products', expires_in: 1.days) do
Product.all.to_a
end需要确认执行结果是否为最终的结果集
Controller 层缓存
Action 缓存
缓存 Action Response,借助 Fragement Cache 和 Callback 实现。
可通过 before_action 加入各种验证机制。
1
2before_action :authentication, only: :show
cache_action :show, expires_in: 1.hour该方法在 Rails4 已经移除了,可通过Gem包开启
Page 缓存 (页面缓存)
将页面缓存成静态页面,无法进行权限认证等。
1
2
3class ProductsController < ActionController
caches_page :index
end该方法在 Rails4 已经移除了,可通过Gem包开启
View 层缓存
SQL 缓存
这是 Rails 框架自带的一个特性,会缓存每一次查询的结果集。
1
2
3
4
5
6
7
8class ProductsController < ActionController
def index
# First Query
@products = Product.all
# Second Query (Cache)
@products = Product.all
end当第二次查询时,会直接从内存中读取第一次查询缓存入内存的结果集。
Tips: 缓存的有效时间是 action 的生命周期
配置
1 | # 开启缓存 |