Should I cache the Carrierwave url?
My webhost is Heroku, which does not allow files to be saved to the local file system. Therefore, I am using Carrierwave to store my files onto Amazon S3.
In the console, I notice when I do:
Photo.last.attachment.url
It returns:
=> "https://foobar.s3.amazonaws.com/uploads/users/1/photos/7/foo.jpg"
As expected. However, this process (of returning the value) in the console takes up 2-3 seconds. My guess is that it it trying to access S3. Even worst, when I load a web page with several photos, it takes quite a while to load.
Someone mentioned that because I am remotely storing my files via S3, I should cache the result from "Photo.last.attachment.url".
This means, in my db I would need to have开发者_如何学运维 two columns:
:attachment and :attachment_url
:attachment would be for the Carrierwave uploader object and :attachment_url would be the link to the S3 file directly.
Is this what I should be doing? Is there a better alternative?
This is fixed in a recent version of Carrierwave. Better not to cache, URL creation is cheap. It was a behavior of Fog to check for the file when creating the URL. Now the behavior is to simply give the link. You can see this discussion: https://github.com/jnicklas/carrierwave/issues/289 and https://github.com/jnicklas/carrierwave/issues/261
I would do the caching. We have done a similar approach using Paperclip.
Alternatively you could be caching the views (partials) where the URLS are being used.
精彩评论