Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c语言的asp.net中的缓存问题#_C#_Asp.net_Httpcontext_Httpcontext.cache - Fatal编程技术网

C# 使用c语言的asp.net中的缓存问题#

C# 使用c语言的asp.net中的缓存问题#,c#,asp.net,httpcontext,httpcontext.cache,C#,Asp.net,Httpcontext,Httpcontext.cache,我正在使用fileuplaod控件上载图像。为此,我曾将其存储在缓存中 以字节格式显示2小时,并在.ashx文件中使用HttpContext显示此图像。出于某种原因 有时保存在缓存中,有时不保存。我正在使用asp.NET2.0和C语言 我的保存代码: //Name of the Image string strGuid = Guid.NewGuid().ToString(); byte[] byteImage = new byte[ImageUpload.PostedFile.Content

我正在使用fileuplaod控件上载图像。为此,我曾将其存储在缓存中

以字节格式显示2小时,并在.ashx文件中使用HttpContext显示此图像。出于某种原因

有时保存在缓存中,有时不保存。我正在使用asp.NET2.0和C语言

我的保存代码:

//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);
使用.ashx文件渲染图像的代码:

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);
}
在缓存中保存字节映像或其他更好的方法是否有问题

谢谢!
sanjay pal

放在缓存中的项目不能保证在缓存中找到。在某些情况下,框架可能会使缓存中的项过期,例如,如果它开始运行的内存不足。在这种情况下,当您向缓存中添加项时,它将调用您传递的


缓存不是一个可靠的存储,在使用它之前,您需要始终检查该项是否存在,以及它是否不存在。请执行必要的操作以获取它并将其放回那里,以便将来的调用者可以找到它。

放在缓存中的项不一定会在那里找到。在某些情况下,框架可能会使缓存中的项过期,例如,如果它开始运行的内存不足。在这种情况下,当您向缓存中添加项时,它将调用您传递的


缓存不是可靠的存储器,在使用该项之前,您需要始终检查该项是否存在,以及该项是否不存在。请执行必要的操作以获取该项并将其放回该项,以便将来的调用者能够找到它。

您可以指定CacheItemPriority.NotRemovable而不是CacheItemPriority.Normal,以防止在其过期之前将其从缓存中删除


但更传统的方法是允许删除它,如果发现缺少,则重新填充缓存。

可以指定CacheItemPriority.NotRemovable而不是CacheItemPriority.Normal,以防止在其过期之前将其从缓存中删除

但一种更传统的方法是允许删除它,如果发现缺少,则重新填充缓存