问题
I have a Rails application that uses Google Cloud Storage for images. Each view has approximately 8 image and when a user tries to load the page it takes more than 5 seconds to complete because of requests to GCS.
Requests are made by Paperclip with the following config:
config.paperclip_defaults = {
storage: :fog,
fog_credentials: {
google_storage_access_key_id: myAccessKey,
google_storage_secret_access_key: mySecretKey,
provider: 'Google'
},
fog_public: true,
fog_directory: 'mybucket'
}
In my view I call each image this way:
<%= image_tag myAsset.image.url %>
Is there any faster way to achieve the same result?
Lazy load seems to me a workaround rather then a solution, I'm I wrong?
Here New Relic infamous analysis (green belongs Google Cloud Storage requests)
回答1:
are the images for a given request pulled every time from the backend? if so, use a cache for the images so you only take the time hit on the first request. possibly even start pulling images into a cache after user auth's in -- lazy loading like you suggested.
回答2:
I cannot say that this sorted everything out, but at least it speeds it up.
fog_host
seems to avoid the network request.
# config/application.rb
module Parasite
class Application < Rails::Application
config.paperclip_defaults = {
fog_host: 'http://mybucektname.storage.googleapis.com',
# ... other options
}
end
end
If you want to dig more check the code or this thread I opened on the Paperclip Github Page.
来源:https://stackoverflow.com/questions/38750407/google-cloud-storage-requests-are-slow-using-paperclip-and-rails