Reputation: 387
I'm kind of a Rails newbie, so I'm not sure how I should go about building this, and am hoping to get some input.
Basically, there are some images (with predictable filenames) that I want to show to the users of my app. I will get them off a remote host, but of course I don't want to hotlink there for every request, so I'd like to cache them on my server somehow. Ideally, the first time someone request an image, it would be downloaded off the remote server and presented to the user, and for subsequent requests for the same image, it would be served and cached like any other static asset.
Normally I would just download the image to the local filesystem, somewhere in the public
folder, but since I'm running this app on Heroku, I don't have persistent write access. What would be a good strategy of handling all this in my case? Not looking for a complete solution in code, just some pointers. Thanks!
Upvotes: 0
Views: 874
Reputation: 371
Here's how I'd approach it:
First, everywhere you would reference one of these images, instead call an action on a controller in your own app. Then, write that controller action to do the following:
Since you're on Heroku, a better approach might be to have that action kick off a delayed_job to retrieve the image (and store it in S3). Then, in your views, have a polling mechanism to periodically ask that controller action if the image is available. If it isn't, show a loading icon, and if it is, show the image (a URL pointing to the image on S3). Going this route won't tie up one of your dynos while waiting for the image to be cached.
Of course, things can be simplified if you could pre-cache all those remote images yourself. Maybe a rake task that downloads all those images and stores them in S3.
Hope this helps.
Upvotes: 1