12

ビューを最適化しようとしており、次のコードで 40 枚の写真がロードされているページで:

= image_tag(product.pictures.first.data.url(:gallery))

次のコードに変更すると、ロード時間は 840 ミリ秒になります。

= image_tag("http://bucketname.s3.amazonaws.com/products/#{product.pictures.first.id}/gallery.jpg?1325844462"

ロード時間220msとなっております。

これは、s3_path_url の補間が非常に遅いことを意味します。他の誰かが同じ問題を予期していますか?とりあえず、URL を生成するヘルパーを作成しました。

def picture_url(picture, style)
  "http://bucketname.s3.amazonaws.com/products/#{picture.id}/#{style}.jpg"
end

ここで私が抱えている唯一の問題は、キャッシュキーが存在せず、拡張機能も存在しないことです。

4

1 に答える 1

0

ギャラリー ページに表示される各製品の画像は常に 1 つだけですか?

データベースのキャッシュ列はどうですか。画像を作成または更新するたびに、この image_url を gallery_picture_url としてデータベースに保存し、次のように直接呼び出すことができます

= image_tag(product.gallery_picture_url)

class Product < ActiveRecord::Base
  after_commit: :update_gallery_picture_url

  def update_gallery_picture_url
    self.update(gallery_picture_url: self.pictures.first.data.url(:gallery)) if self.gallery_picture_present?
  end

  def gallery_picture_present?
    (self.pictures.first.data.url(:gallery) rescue false).present?
  end
end
于 2015-04-27T10:24:32.983 に答える