Monday 15 August 2011

Caching dynamic images in Rails -


I am using a rmagick gem to create dynamic images from a controller. The controller takes an ID as an absolute, monitors a model, writes text to an existing image, and outputs it.

I have created a disc vs. write for every request to run some criteria and if it is already present then using send_data to output it I have not seen much difference between the requests between these two methods.

Is there any best practice for writing on disk rather than caching the image or dynamically generating it for every request? Once generated, these images are more stable but I would like the option to regenerate it after a certain time interval.

The best practice is for cached images and allows to serve the webserver.

Use webserver like Apache or Nginx in front of your rail app, and make sure that you write a picture where the webserver can serve it, so if your train route / dynamic_images / 3.png evaluates for (which calls dynamic_images_controller verb show id = 3 and format = png], that image to be public / Dynamic_image / 3.png and send it to the controller in the send_file sub

The next time that the file is requested ( /dynamic_images/3.png ), the webserver will happily serve it (cached) and the Rail app Also will not hit.

For advanced needs,, and cleaning your controller code, have a look at the gem.


No comments:

Post a Comment