Rails Cache

今天插一脚,介绍一下Rails的缓存机制以及memcached的使用
Rails的Cache分四种:
1,Page Cache - Fastest
2,Action Cache - Next Fastest
3,Fragment Cache - Least Fastest
4,ActiveRecord Cache - Only available in Edge Rails
下面一一介绍上面四种Cache以及Rails如何使用memcached

一、Page Cache
如果开发阶段要使用cache,则需要先设置好config/environments/development.rb:
[code]
config.action_controller.perform_caching = true
[/code]
而production环境下默认是开启cache功能的
Page Cache是Rails中最快的cache机制,使用Page Cache的前提一般为:
1,需要cache的page对所有用户一致
2,需要cache的page对public可访问,不需要authentication
Page Cache使用起来很简单:
[code]
class BlogController < ApplicationController
caches_page :list, :show

def list
Post.find(:all, \:order => "created_on desc", :limit => 10)
end

def show
@post = Post.find(params[:id])
end
end
[/code]
这样我们就对BlogController的list和show页面进行了缓存
这样做的效果是第一次访问list和show页面时生成了public/blog/list.html和public/blog/show/5.html这两个html页面
对于分页情况下的cache,我们需要把url的page参数改写成"blog/list/:page"这种形式,而不是"blog/list?page=1"这种形式
这样cache的html页面即为public/blog/list/1.html
当数据更改时我们需要清除旧的缓存,我们采用Sweepers来做是非常不错的选择,这把在BlogController里清除缓存的代码分离出来
首先编辑config/environment.rb:
[code]
Rails::Initializer.run do |config|
# ...
config.load_paths += %w(#{RAILS_ROOT}/app/sweepers)
# ...
[/code]
这告诉Rails加载#{RAILS_ROOT}/app/sweepers目录下的文件
我们为BlogController定义app/sweepers/blog_sweeper.rb:
[code]
class BlogSweeper < ActionController::Caching::Sweeper
observe Post # This sweeper is going to keep an eye on the Post model

# If our sweeper detects that a Post was created call this
def after_create(post)
expire_cache_for(post)
end

# If our sweeper detects that a Post was updated call this
def after_update(post)
expire_cache_for(post)
end

# If our sweeper detects that a Post was deletedcall this
def after_destroy(post)
expire_cache_for(post)
end

private
def expire_cache_for(record)
# Expire the list page now that we posted a new blog entry
expire_page(:controller => 'blog', :action => 'list')

# Also expire the show page, in case we just edit a blog entry
expire_page(:controller => 'blog', :action => 'show', :id => record.id)
end
end
[/code]
然后我们在BlogController里加上该sweeper即可:
[code]
class BlogController < ApplicationController
caches_page :list, :show
cache_sweeper :blog_sweeper, \:only => [:create, :update, :destroy]
# ...
end
[/code]
我们可以配置cache的静态html文件的存放位置,这在config/environment.rb里设置:
[code]
config.action_controller.page_cache_directory = RAILS_ROOT + "/public/cache/"
[/code]
然后我们设置Apache/Lighttpd对于静态html文件render时不接触Rails server即可
所以Page Cache就是最快的Cache,因为它不与Rails server打交道,直接load静态html

二、Action Cache
Action Cache相关的helper方法是caches_action和expire_action,其他基本和Page Cache一样
另外我们还可以运行[b]rake tmp:cache:clear[/b]来清空所有的Action Cache和Fragment Cache
[code]
class BlogController < ApplicationController
before_filter :authentication
caches_action :list, :show
cache_sweeper :blog_sweeper, \:only => [:create, :update, :destroy]
[/code]
如上代码所示,我们将authentication这个filter放在caches_action之前声明,这样我们的Action Cache在执行之前会先访问authentication方法
这样可以弥补Page Cache不能对需要登录认证的Page进行Cache的缺点
生成的cache文件为tmp/cache/localhost:3000/blog/list.cache,这样对不同subdomain的访问页面可以cache到不同的目录
由于每次访问Action Cache时都需要与Rails server打交道,并且要先运行filters,所以比Page Cache的效率稍低

三、Fragment Cache
Fragment Cache用于处理rhtml页面中的部分需要cache的模块,如app/views/blog/list.rhtml:
[code]
<strong>My Blog Posts</strong>
<% cache do %>
<ul>
<% for post in @posts %>
<li><%= link_to post.title, :controller => 'blog', :action => 'show', :id => post %></li>
<% end %>
</ul>
<% end %>
[/code]
生成的cache文件为/tmp/cache/localhost:3000/blog/list.cache
我们需要在BlogController的list方法里加上一行判断,如果是读取Fragment Cache,则不必再查询一次数据库:
[code]
def list
unless read_fragment({})
@post = Post.find(:all, \:order => 'created_on desc', :limit => 10)
end
end
[/code]
Fragment分页时的Cache:
[code]
def list
unless read_fragment({:page => params[:page] || 1}) # Add the page param to the cache naming
@post_pages, @post = paginate :posts, :per_page => 10
end
end
[/code]
rhtml页面也需要改写:
[code]
<% cache ({:page => params[:page] || 1}) do %>
... All of the html to display the posts ...
<% end %>
[/code]
生成的cahce文件为/tmp/cache/localhost:3000/blog/list.page=1.cache
从分页的Fragment Cache可以看出,Fragment Cache可以添加类似名字空间的东西,用于区分同一rhtml页面的不同Fragment Cache,如:
[code]
cache ("turkey") => "/tmp/cache/turkey.cache"
cache (:controller => 'blog', :action => 'show', :id => 1) => "/tmp/cache/localhost:3000/blog/show/1.cache"
cache ("blog/recent_posts") => "/tmp/cache/blog/recent_posts.cache"
cache ("#{request.host_with_port}/blog/recent_posts") => "/tmp/cache/localhost:3000/blog/recent_posts.cache"
[/code]
清除Fragment Cache的例子:
[code]
expire_fragment(:controller => 'blog', :action => 'list', :page => 1)
expire_fragment(%r{blog/list.*})
[/code]

四、ActiveRecord Cache
Rails Edge中ActiveRecord已经默认使用SQl Query Cache,对于同一action里面同一sql语句的数据库操作会使用cache

五、memcached
越来越多的大型站点使用memcached做缓存来加快访问速度
memcached是一个轻量的服务器进程,通过分配指定数量的内存来作为对象快速访问的cache
memcached就是一个巨大的Hash表,我们可以存取和删除key和value:
[code]
@tags = Tag.find :all
Cache.put 'all_your_tags', @tags
Cache.put 'favorite_skateboarder', 'Tom Penny'
skateboarder = Cache.get 'favorite_skateboarder'
Cache.delete 'all_your_tags'
[/code]
我们可以使用memcached做如下事情:
1,自动缓存数据库的一行作为一个ActiveRecord对象
2,缓存render_to_string的结果
3,手动存储复杂的数据库查询作为缓存
cached_model让我们轻松的缓存ActiveRecord对象,memcahed-client包含在cached_model的安装里,memcahed-client提供了缓存的delete方法
[code]
sudo gem install cached_model
[/code]
我们在config/environment.rb里添加如下代码来使用memcached:
[code]
require 'cached_model'

memcache_options = {
:c_threshold => 10_000,
:compression => true,
:debug => false,
:namespace => 'my_rails_app',
:readonly => false,
:urlencode => false
}

CACHE = MemCache.new memcache_options
CACHE.servers = 'localhost:11211'
[/code]
对于production环境我们把上面的代码挪到config/environments/production.rb里即可
然后让我们的domain model集成CachedModel而不是ActiveRecord::Base
[code]
class Foo < CachedModel
end
[/code]
然后我们就可以使用memcached了:
[code]
all_foo = Foo.find :all
Cache.put 'Foo:all', all_foo
Cache.get 'Foo:all'
[/code]
需要注意的几点:
1,如果你查询的item不在缓存里,Cache.get将返回nil,可以利用这点来判断是否需要重新获取数据并重新插入到缓存
2,CachedModel的记录默认15分钟后expire,可以通过设置CachedModel.ttl来修改expiration time
3,手动插入的对象也会过期,Cache.put('foo', 'bar', 60)将在60秒后过期
4,如果分配的内存用尽,则older items将被删除
5,可以对每个app server启动一个memcached实例,分配128MB的内存对一般的程序来说足够

注:本文参考[url=http://www.railsenvy.com/2007/2/28/rails-caching-tutorial]Ruby on Rails Caching Tutorial[/url]和[url=http://nubyonrails.com/articles/2006/08/17/memcached-basics-for-rails]memcached Basics for Rails[/url]两篇网文
BTW:[url=http://www.railsenvy.com/]Rails Envy[/url]上面有几个Ruby On Rails的广告视频,是关于与PHP/Java的比较,比较生动搞笑
* file[/var/log/gitlab/gitlab-kas/current] action touch (skipped due to only_if) Recipe: gitlab::database_migrations * ruby_block[check remote PG version] action nothing (skipped due to action :nothing) * rails_migration[gitlab-rails] action run[2025-07-14T15:04:58+08:00] WARN: gitlab-rails does not have a log_group or default logdir mode defined. Setting to 0700. * bash_hide_env[migrate gitlab-rails database] action run ================================================================================ Error executing action `run` on resource 'bash_hide_env[migrate gitlab-rails database]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 Cookbook Trace: (most recent call first) ---------------------------------------- /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb 20: bash_hide_env "migrate #{new_resource.name} database" do 21: code <<-EOH 22: set -e 23: log_file="#{logging_settings[:log_directory]}/#{new_resource.logfile_prefix}-$(date +%Y-%m-%d-%H-%M-%S).log" 24: umask 077 25: /opt/gitlab/bin/gitlab-rake #{new_resource.rake_task} 2>& 1 | tee ${log_file} 26: STATUS=${PIPESTATUS[0]} 27: chown #{account_helper.gitlab_user}:#{account_helper.gitlab_group} ${log_file} 28: echo $STATUS > #{new_resource.helper.db_migrate_status_file} 29: exit $STATUS 30: EOH 31: 32: environment new_resource.environment if new_resource.property_is_set?(:environment) 33: new_resource.dependent_services.each do |svc| 34: notifies :restart, svc, :immediately 35: end 36: 37: not_if { new_resource.helper.migrated? } 38: sensitive false 39: end 40: end Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' bash_hide_env("migrate gitlab-rails database") do action [:run] default_guard_interpreter :default interpreter "bash" declared_type :bash_hide_env cookbook_name "gitlab" recipe_name "database_migrations" code " set -e\n log_file=\"/var/log/gitlab/gitlab-rails/gitlab-rails-db-migrate-$(date +%Y-%m-%d-%H-%M-%S).log\"\n umask 077\n /opt/gitlab/bin/gitlab-rake gitlab:db:configure 2>& 1 | tee ${log_file}\n STATUS=${PIPESTATUS[0]}\n chown git:git ${log_file}\n echo $STATUS > /var/opt/gitlab/gitlab-rails/upgrade-status/db-migrate-c6697a9cd49e1cfa45cc31b1ecdb90ae-bd824d1abb2\n exit $STATUS\n" environment "*sensitive value suppressed*" not_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client ================================================================================ Error executing action `run` on resource 'rails_migration[gitlab-rails]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 Cookbook Trace: (most recent call first) ---------------------------------------- /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/resources/rails_migration.rb:20:in `block in class_from_file' Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/database_migrations.rb 51: rails_migration "gitlab-rails" do 52: rake_task 'gitlab:db:configure' 53: logfile_prefix 'gitlab-rails-db-migrate' 54: helper migration_helper 55: 56: environment env_variables 57: dependent_services dependent_services 58: notifies :run, "execute[clear the gitlab-rails cache]", :immediately 59: notifies :run, "ruby_block[check remote PG version]", :immediately 60: 61: only_if { migration_helper.attributes_node['auto_migrate'] } 62: end Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/database_migrations.rb:51:in `from_file' rails_migration("gitlab-rails") do action [:run] default_guard_interpreter :default declared_type :rails_migration cookbook_name "gitlab" recipe_name "database_migrations" rake_task "gitlab:db:configure" logfile_prefix "gitlab-rails-db-migrate" helper "*sensitive value suppressed*" environment "*sensitive value suppressed*" only_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client [2025-07-14T15:05:48+08:00] INFO: Running queued delayed notifications before re-raising exception [2025-07-14T15:05:48+08:00] INFO: templatesymlink[Create a gitlab.yml and create a symlink to Rails root] sending run action to execute[clear the gitlab-rails cache] (delayed) Recipe: gitlab::gitlab-rails * execute[clear the gitlab-rails cache] action run ================================================================================ Error executing action `run` on resource 'execute[clear the gitlab-rails cache]' ================================================================================ Mixlib::ShellOut::ShellCommandFailed ------------------------------------ Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned Resource Declaration: --------------------- # In /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/gitlab-rails.rb 543: execute "clear the gitlab-rails cache" do 544: command "/opt/gitlab/bin/gitlab-rake cache:clear" 545: action :nothing 546: not_if { omnibus_helper.not_listening?('redis') || !node['gitlab']['gitlab_rails']['rake_cache_clear'] } 547: end 548: Compiled Resource: ------------------ # Declared in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/gitlab-rails.rb:543:in `from_file' execute("clear the gitlab-rails cache") do action [:nothing] default_guard_interpreter :execute command "/opt/gitlab/bin/gitlab-rake cache:clear" declared_type :execute cookbook_name "gitlab" recipe_name "gitlab-rails" not_if { #code block } end System Info: ------------ chef_version=18.3.0 platform=centos platform_version=7.6.1810 ruby=ruby 3.1.5p253 (2024-04-023 revision 1945f8dc0e) [x86_64-linux] program_name=/opt/gitlab/embedded/bin/cinc-client executable=/opt/gitlab/embedded/bin/cinc-client [2025-07-14T15:06:56+08:00] INFO: version_file[Create version file for Gitaly] sending hup action to runit_service[gitaly] (delayed) Recipe: gitaly::enable * runit_service[gitaly] action hup[2025-07-14T15:06:56+08:00] INFO: runit_service[gitaly] signalled (HUP) [2025-07-14T15:06:56+08:00] INFO: runit_service[gitaly] sent hup - send hup to runit_service[gitaly] [2025-07-14T15:06:56+08:00] INFO: file[create /opt/gitlab/embedded/etc/90-omnibus-gitlab-kernel.shmmax.conf kernel.shmmax] sending run action to execute[reload all sysctl conf] (delayed) Recipe: package::sysctl * execute[reload all sysctl conf] action run [execute] * Applying /usr/lib/sysctl.d/00-system.conf ... net.bridge.bridge-nf-call-ip6tables = 0 net.bridge.bridge-nf-call-iptables = 0 net.bridge.bridge-nf-call-arptables = 0 * Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ... kernel.yama.ptrace_scope = 0 * Applying /usr/lib/sysctl.d/50-default.conf ... kernel.sysrq = 16 kernel.core_uses_pid = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.promote_secondaries = 1 net.ipv4.conf.all.promote_secondaries = 1 fs.protected_hardlinks = 1 fs.protected_symlinks = 1 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.sem.conf ... kernel.sem = 250 32000 32 275 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.shmall.conf ... kernel.shmall = 4194304 * Applying /etc/sysctl.d/90-omnibus-gitlab-kernel.shmmax.conf ... kernel.shmmax = 17179869184 * Applying /etc/sysctl.d/99-sysctl.conf ... net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-arptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_local_reserved_ports = 30000-32767 vm.max_map_count = 262144 vm.swappiness = 1 fs.inotify.max_user_instances = 524288 kernel.pid_max = 65535 vm.overcommit_memory = 1 net.core.somaxconn = 511 * Applying /etc/sysctl.d/lvs_dr.conf ... net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 * Applying /etc/sysctl.conf ... net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-arptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_local_reserved_ports = 30000-32767 vm.max_map_count = 262144 vm.swappiness = 1 fs.inotify.max_user_instances = 524288 kernel.pid_max = 65535 vm.overcommit_memory = 1 net.core.somaxconn = 511 [2025-07-14T15:06:56+08:00] INFO: execute[reload all sysctl conf] ran successfully - execute sysctl -e --system Running handlers: [2025-07-14T15:06:56+08:00] ERROR: Running exception handlers There was an error running gitlab-ctl reconfigure: Multiple failures occurred: * Mixlib::ShellOut::ShellCommandFailed occurred in Cinc Client run: rails_migration[gitlab-rails] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 * Mixlib::ShellOut::ShellCommandFailed occurred in delayed notification: execute[clear the gitlab-rails cache] (gitlab::gitlab-rails line 543) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned Notes: Default admin account has been configured with following details: Username: root Password: You didn't opt-in to print initial root password to STDOUT. Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours. NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. Running handlers complete [2025-07-14T15:06:56+08:00] ERROR: Exception handlers complete Infra Phase failed. 319 resources updated in 02 minutes 41 seconds Notes: Default admin account has been configured with following details: Username: root Password: You didn't opt-in to print initial root password to STDOUT. Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours. NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password. [2025-07-14T15:06:56+08:00] FATAL: Stacktrace dumped to /opt/gitlab/embedded/cookbooks/cache/cinc-stacktrace.out [2025-07-14T15:06:56+08:00] FATAL: --------------------------------------------------------------------------------------- [2025-07-14T15:06:56+08:00] FATAL: PLEASE PROVIDE THE CONTENTS OF THE stacktrace.out FILE (above) IF YOU FILE A BUG REPORT [2025-07-14T15:06:56+08:00] FATAL: --------------------------------------------------------------------------------------- [2025-07-14T15:06:56+08:00] FATAL: Chef::Exceptions::MultipleFailures: Multiple failures occurred: * Mixlib::ShellOut::ShellCommandFailed occurred in Cinc Client run: rails_migration[gitlab-rails] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: bash_hide_env[migrate gitlab-rails database] (gitlab::database_migrations line 20) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '137' ---- Begin output of "bash" ---- STDOUT: STDERR: ---- End output of "bash" ---- Ran "bash" returned 137 * Mixlib::ShellOut::ShellCommandFailed occurred in delayed notification: execute[clear the gitlab-rails cache] (gitlab::gitlab-rails line 543) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '' ---- Begin output of /opt/gitlab/bin/gitlab-rake cache:clear ---- STDOUT: STDERR: ---- End output of /opt/gitlab/bin/gitlab-rake cache:clear ---- Ran /opt/gitlab/bin/gitlab-rake cache:clear returned
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值