c# 中的 asp.net 缓存问题

发布于 2024-08-10 13:36:02 字数 1503 浏览 8 评论 0原文

我正在使用 fileuplaod 控件上传图像。为此,我曾经将其以

字节格式存储在缓存中 2 小时,并使用 .ashx 文件中的 HttpContext 显示该图像。由于某种原因,它

有时会保存在缓存中,有时则不会。我正在使用 asp.net 2.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);
}

字节图像保存在缓存中是否有任何问题或任何其他更好的方法?

谢谢! 桑杰·帕尔

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?

Thanks!
sanjay pal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-08-17 13:36:02

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

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

Items put in a cache are not guaranteed to be found there. In some situations the framework might expire items from the cache, for example if it starts running low on memory. In this case it will call the onRemoveCallback you pass along when you add an item to the cache.

The cache is not a reliable storage, you need to always check if the item is there before using it and if it is not perform the necessary actions to fetch it and put it back there so that future callers could find it.

与往事干杯 2024-08-17 13:36:02

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

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

You could specify CacheItemPriority.NotRemovable instead of CacheItemPriority.Normal to prevent it being removed from the Cache before its expiration is up.

But a more conventional approach is to allow it to be removed, and repopulate the cache if you find it's missing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文