gpt4 book ai didi

ruby-on-rails - 在 Rails(2.x 或 3.x)中优化 View 性能的提示/技巧?

转载 作者:行者123 更新时间:2023-12-03 15:53:55 26 4
gpt4 key购买 nike

人们可以做些什么来优化 Rails 2.x(和 3.x)中的 View 渲染?

我们的应用程序大部分时间都在渲染 View (最少的数据库调用)。

我们如何加速 View 的性能/渲染?

谢谢!

最佳答案

加速 erb 渲染的推荐方法是避免这样做。尝试使用页面缓存或条件 GET header 以避免重新呈现未更改的内容。

http://guides.rubyonrails.org/caching_with_rails.html

class ProductsController < ApplicationController

def show
@product = Product.find(params[:id])

# If the request is stale according to the given timestamp and etag value
# (i.e. it needs to be processed again) then execute this block
if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
respond_to do |wants|
# ... normal response processing
end
end

# If the request is fresh (i.e. it's not modified) then you don't need to do
# anything. The default render checks for this using the parameters
# used in the previous call to stale? and will automatically send a
# :not_modified. So that's it, you're done.
end

关于ruby-on-rails - 在 Rails(2.x 或 3.x)中优化 View 性能的提示/技巧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5904415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com