Ruby on rails “应该多长时间?”;"耙路";跑
我刚开始学习Rails,所以请原谅我的基本问题。我已经注意到,Ruby on rails “应该多长时间?”;"耙路";跑,ruby-on-rails,routes,rake,Ruby On Rails,Routes,Rake,我刚开始学习Rails,所以请原谅我的基本问题。我已经注意到,rake routes命令每次运行都需要一段时间才能执行。我有大约20条路由用于3个控制器,执行大约需要40秒 这正常吗?我怎样才能加快速度 注意:我使用的是Windows 7和Rails 3.1.3(使用Rails安装程序设置)。Rails环境在Windows上加载要花费大量的时间。我建议您尝试一下Unix,比如Ubuntu,因为Windows是运行和开发RubyonRails应用程序的最差环境。但是如果您只是在尝试Rails,Wi
rake routes
命令每次运行都需要一段时间才能执行。我有大约20条路由用于3个控制器,执行大约需要40秒
这正常吗?我怎样才能加快速度
注意:我使用的是Windows 7和Rails 3.1.3(使用Rails安装程序设置)。Rails环境在Windows上加载要花费大量的时间。我建议您尝试一下Unix,比如Ubuntu,因为Windows是运行和开发RubyonRails应用程序的最差环境。但是如果您只是在尝试Rails,Windows就足够了:)这看起来有点长,但是您真的需要经常运行
rake routes
?在我的系统OSX Lion/Rails 3.2.0上,rake routes
需要约10秒。rake routes任务取决于加载Rails环境并需要数千个Ruby文件的环境任务
Rails环境的启动时间和相应的rake路由执行时间非常接近(在我的Linux on Stroidors笔记本电脑上,Rails应用程序有大约50条路由):
没有简单的方法可以减少启动时间,因为它依赖于解释器需要脚本文件的方式:我想出了一个解决方案来解决
rake routes
每次运行大约需要8秒。它是一个简单的基于文件的缓存,运行bundle exec rake routes
,将输出存储在tmp下的文件中。文件名是config/routes.rb
的md5散列,因此如果您进行更改并将其更改回来,它将使用旧的缓存文件
我将以下bash函数放在一个可执行文件中,我称之为fastroutes:
if [ ! -f config/routes.rb ]; then
echo "Not in root of rails app"
exit 1
fi
cached_routes_filename="tmp/cached_routes_$(md5 -q config/routes.rb).txt"
function cache_routes {
bundle exec rake routes > $cached_routes_filename
}
function clear_cache {
for old_file in $(ls tmp/cache_routes*.txt); do
rm $old_file
done
}
function show_cache {
cat $cached_routes_filename
}
function show_current_filename {
echo $cached_routes_filename
}
function main {
if [ ! -f $cached_routes_filename ]; then
cache_routes
fi
show_cache
}
if [[ "$1" == "-f" ]]
then
show_current_filename
elif [[ "$1" == "-r" ]]
then
rm $cached_routes_filename
cache_routes
else
main
fi
这也是一个例子
这样,您只需生成一次路由,然后fastroutes
将使用缓存的值。在您的Rakefile中:
#Ouptut stored output of rake routes
task :fast_routes => 'tmp/routes_output' do |t|
sh 'cat', t.source
end
#Update tmp/routes_output if its older than 'config/routes.rb'
file 'tmp/routes_output' => 'config/routes.rb' do |t|
outputf = File.open(t.name, 'w')
begin
$stdout = outputf
Rake.application['routes'].invoke
ensure
outputf.close
$stdout = STDOUT
end
end
您应该运行
rake路由
,否则它将不能作为rake任务使用。如果OP刚开始使用Rails,那么routes任务可能有助于他们理解自动设置的路由。我一直运行rake routes,因为我发现更容易理解路由转换为什么。rake routes任务在开始使用Rails时非常有用。我不是初学者,但经常使用该任务,事实上,每当我需要查看路线的I18n翻译时。我知道Win并不是最好的平台,但我希望我的第一步可以做到(正如你所说,应该足够尝试了)。如果这可能成为一个更持久的事情,我们会考虑切换到Linux上。在Ubuntu上,它已经变得非常的快……这只是需要一段时间的耙路吗?启动服务器或控制台是否也很慢?最好您可以检查特定控制器控制器=控制器名称rake的路由routes@Jan:服务器(大约1:15分钟)和控制台(0:45分钟)也需要一段时间如果服务器正在运行,有目的地得到一个路由错误,你就可以从那里得到你的路由:)至少在开始的时候,当我试图掌握这个概念时,我觉得我必须;-)对于“真实”部分,我的衡量标准非常高。我非常肯定这一点可以降低。我的同事在Ubuntu下使用同一个应用程序时,调用rails或rake几乎可以立即得到结果。真正的0m26.287s用户0m3.668s系统0m1.672s
#Ouptut stored output of rake routes
task :fast_routes => 'tmp/routes_output' do |t|
sh 'cat', t.source
end
#Update tmp/routes_output if its older than 'config/routes.rb'
file 'tmp/routes_output' => 'config/routes.rb' do |t|
outputf = File.open(t.name, 'w')
begin
$stdout = outputf
Rake.application['routes'].invoke
ensure
outputf.close
$stdout = STDOUT
end
end