Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Performance Em Aplicações Rails - Do jeito certo

luizvarela
October 14, 2015

Performance Em Aplicações Rails - Do jeito certo

luizvarela

October 14, 2015
Tweet

More Decks by luizvarela

Other Decks in Technology

Transcript

  1. 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.cache_key) 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 end
  2. class ProductsController < ApplicationController def show @product = Product.find(params[:id]) if

    stale?(@product) respond_to do |wants| # ... normal response processing end end end end
  3. <% Order.find_recent.each do |o| %> <%= o.buyer.name %> bought <%=

    o.product.name %> <% end %> <% cache do %> All available products: <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %> <% end %>
  4. <% cache("top_products", :expires_in => 1.hour) do %> <div id="topSellingProducts"> <%

    @recent_product = Product.order("units_sold DESC").limit(20) %> <%= render :partial => "product", :collection => @recent_products %> </div> <% end %>
  5. def Product.out_of_stock Rails.cache.fetch("out_of_stock_products", :expires_in => 5.minutes) do Product.all.joins(:inventory).conditions.where("inventory.quantity = 0")

    end end def competing_price Rails.cache.fetch("/product/#{id}-#{updated_at}/comp_price", :expires_in => 12.hours) do Competitor::API.find_price(id) end end
  6. class ProductsController < ApplicationController def show @product = Product.find(params[:id]) #

    Sugar syntax fresh_when @product # Hash syntax fresh_when etag: @product, last_modified: @product.updated_at, public: true end end
  7. 1

  8. 2

  9. # app/views/users/_form.html.slim = form_for(@user) do |user_form| = user_form.text_field(:name) = attachinary_file_field_tag

    ‘user[avatar]', @user, :avatar = attachinary_file_field_tag ‘user[photos]’, @user, :photos = user_form.submit("Save")
  10. # app/views/users/show.html.slim if user.avatar.present? = cl_image_tag(user.avatar.path, width: 80, height: 100,

    crop: :thumb, gravity: :face) - user.photos.each do |photo| = cl_image_tag(photo.path, size: '70x50', crop: :fill, radius: 20)
  11. CDN

  12. N + 1 QUERIES #Articles model class Article < ActiveRecord::Base

    belongs_to :author end #Authors model class Author < ActiveRecord::Base has_many :posts end
  13. N + 1 QUERIES #In our controller @recent_articles = Article.order(published_at:

    :desc).limit(5) #in our view file @recent_articles.each do |article| Title: <%= article.title %> Author:<%= article.author.name %> end
  14. N + 1 QUERIES #In our controller #Using includes(:authors) will

    include authors model. @recent_articles = Article.order(published_at: :desc).includes(:authors).limit(5) #in our view file @recent_articles.each do |article| Title: <%= article.title %> Author:<%= article.author.name %> end