I have a controller that functions as a conditional image server. Evaluation of the criteria requires my controller to run and a couple of queries to be executed. Depending on the outcome (a simple boolean switch) I read one of two image files and send it as the response. I''m hoping to find a way to cache the image data but allow the controller action to run. Is this something Rails might accommodate via either the cache mechanism or something else? Thanks! -toby
On Tuesday 12 July 2005 21:20, Toby Boudreaux wrote:> I have a controller that functions as a conditional image server. > Evaluation of the criteria requires my controller to run and a couple > of queries to be executed. Depending on the outcome (a simple boolean > switch) I read one of two image files and send it as the response. > > I''m hoping to find a way to cache the image data but allow the > controller action to run. > > Is this something Rails might accommodate via either the cache > mechanism or something else?Write both images onto the filesystem somewhere. Then in your action do your logic and then call send_file to deliver the data. If you have problems with naming these ''cached'' copies consider using the md5/sha1 hash of a piece of unique data, this could be the file data, path, or similar. Assuming the two images you could return are a full size image and its thumbnail, you could organise the data by using the md5 of the image''s original path like so. images/e/1/e18f8840ac2dae299f2b92509d4775e4 thumbnails/e/1/e18f8840ac2dae299f2b92509d4775e4 The checksum is based on the original file path so both of your copies share the same name but they are stored in different directories. The extra /e/1/ directories is a method used by various programs to keep the number of files in a single directory to a manageable level. Note that you don''t trim the characters you use for that from the name of the file itself - or you will risk collisions. If you need/want to save space you could automatically clean out files which haven''t been accessed in a while using find(1) -atime.> Thanks! > > -toby > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsHTH, -- Dominic Marks
Dominic - thanks a ton. I''ll implement this first thing in the morning. -toby On Jul 12, 2005, at 5:57 PM, Dominic Marks wrote:> On Tuesday 12 July 2005 21:20, Toby Boudreaux wrote: > >> I have a controller that functions as a conditional image server. >> Evaluation of the criteria requires my controller to run and a couple >> of queries to be executed. Depending on the outcome (a simple boolean >> switch) I read one of two image files and send it as the response. >> >> I''m hoping to find a way to cache the image data but allow the >> controller action to run. >> >> Is this something Rails might accommodate via either the cache >> mechanism or something else? >> > > Write both images onto the filesystem somewhere. Then in your > action do your logic and then call send_file to deliver the data. > If you have problems with naming these ''cached'' copies consider > using the md5/sha1 hash of a piece of unique data, this could be > the file data, path, or similar. > > Assuming the two images you could return are a full size image > and its thumbnail, you could organise the data by using the md5 > of the image''s original path like so. > > images/e/1/e18f8840ac2dae299f2b92509d4775e4 > thumbnails/e/1/e18f8840ac2dae299f2b92509d4775e4 > > The checksum is based on the original file path so both of your > copies share the same name but they are stored in different > directories. > The extra /e/1/ directories is a method used by various programs to > keep the number of files in a single directory to a manageable level. > Note that you don''t trim the characters you use for that from the name > of the file itself - or you will risk collisions. > > If you need/want to save space you could automatically clean out > files which haven''t been accessed in a while using find(1) -atime. > > >> Thanks! >> >> -toby >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > HTH, > -- > Dominic Marks >