cache problem
hi,
I am using the fileuplaod control to upload the images. For this I used to stored it in the cache
for 2 hours in byte format and show this image using the HttpContext in the .ashx file. For some Reason it
is sometime saving in the cache and sometime not. I am using asp.net 2.0 and C# language.
My code For Saving :
//Name of the Image
string strGuid = Guid.NewGuid().ToString();
byte[] byteImage = new byte[ImageUpload.PostedFile.ContentLength];
//file upload control ID "ImageUpload" and read it and save it in cache.
ImageUpload.PostedFile.InputStream.Read(byteImage, 0, byteImage.Length);
//Saving Byte in the cache
Cache.Add(strGuid, byteImage, null, DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
//Saving Image Format in cache
Cache.Add(string.Format("{0}_Type", strGuid), strContentType, null, DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
UserImage.ImageUrl = string.Format("~/UserControl/ImageHander.ashx?imgId={0}", strGuid);
Code For Rendering Image using .ashx file:
public void ProcessRequest (HttpContext context)
{
string strImageGuid = context.Request.QueryString["imgId"].ToString();
string strContentTypeID = string.Format("{0}_Type", context.Request.QueryString["imgId"].ToString());
byte[] byteImage =(byte []) context.Cache[strImageGuid];
string strContentType = (string)context.Cache[strContentTypeID];
context.Response.ContentType = strContentType;
context.Response.OutputStream.Write(byteImage, 0, byteImage.Length);
}
Is there any Problem In saving the byte image in cache Or any other better way of doing this?. I dont want to save image bye on the disk.
Thanks!
Respostas
- I am not certain of this (and haven't been able to confirm with MSDN documentation), but I don't think that cache items are guaranteed to live until their expiration, they are only guaranteed not to live longer than their expiration. If these images are large, it may not take many of them to cause the cache to flush the older items. You could try to diagnose this by logging when items are removed from the cache similarly to this:
Hopefully that will provide you with some more information.Cache.Add(string.Format("{0}_Type", strGuid), strContentType, null, DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, HandleCacheItemRemoved); void HandleCacheItemRemoved(string key, object value, CacheItemRemovedReason reason) { // possible concurrency issues here if you are sending multiple simultaneous requests using(StreamWriter sw = File.AppendText("C:\LogFile.txt")) { sw.WriteLine("Cache item ({0}) was removed. Reason: ({1}).", key, reason); } }
- Editadoccbristo domingo, 8 de novembro de 2009 23:03reformatting code
- Marcado como RespostaeryangMSFT, Moderatorsegunda-feira, 16 de novembro de 2009 3:27
Todas as Respostas
- I am not certain of this (and haven't been able to confirm with MSDN documentation), but I don't think that cache items are guaranteed to live until their expiration, they are only guaranteed not to live longer than their expiration. If these images are large, it may not take many of them to cause the cache to flush the older items. You could try to diagnose this by logging when items are removed from the cache similarly to this:
Hopefully that will provide you with some more information.Cache.Add(string.Format("{0}_Type", strGuid), strContentType, null, DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.Normal, HandleCacheItemRemoved); void HandleCacheItemRemoved(string key, object value, CacheItemRemovedReason reason) { // possible concurrency issues here if you are sending multiple simultaneous requests using(StreamWriter sw = File.AppendText("C:\LogFile.txt")) { sw.WriteLine("Cache item ({0}) was removed. Reason: ({1}).", key, reason); } }
- Editadoccbristo domingo, 8 de novembro de 2009 23:03reformatting code
- Marcado como RespostaeryangMSFT, Moderatorsegunda-feira, 16 de novembro de 2009 3:27

