Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perfomance of .ashx handlers for retrieving a lot of binary images

I used .ashx handler for getting images from database.I want to retrieve a lot of images (>1000) in this way:

    <img src='GetImage.ashx?id= <%# Eval("id") %>'/>

(why I do this you can understand if read my previous question: bind database image to ItemTemplate in .ascx ).I am afraid that multipiles database querys (first query to get all id's,all others for getting image one by one) will take a lot of time,is it? What are possible solutions?

like image 853
Anton Putov Avatar asked Oct 24 '12 00:10

Anton Putov


2 Answers

First of all the browsers did not ask the images all together, but few at a time.

Second, the handler is not use session, so its not lock the one the other and so a parallel process can be done for the image call.

The extra that I suggest to add it a cache for the browser, so when its load an image to not ask it again.

An example:

context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(120));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 120, 0));

but you can add more aggressive cache.

One similar question: call aspx page to return an image randomly slow

like image 108
Aristos Avatar answered Nov 17 '22 15:11

Aristos


Caching is always a good idea for static'ish content that is asked for frequently.

Also if the images are relatively small you could use data uri's http://css-tricks.com/data-uris/

Say you had an Images table with ID INT, Name VARCHAR(64), MimeType VARCHAR(64), and Data VARBINARY(MAX)

SELECT Name, 'data:' + MimeType + ';base64,' + cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:column("Data")))', 'VARCHAR(MAX)') AS DataURI

<img src='<%# Eval("DataURI") %>' alt='<%# Eval("Name") %> />

In this way you can have 1 database query that will return everything.

like image 38
Louis Ricci Avatar answered Nov 17 '22 13:11

Louis Ricci