1

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);
}

バイトイメージをキャッシュに保存する際に問題はありますか、またはこれを行うための他のより良い方法はありますか?

ありがとう!サンジェイ・パル

4

2 に答える 2

4

キャッシュに入れられたアイテムは、そこにあるとは限りません。メモリが不足し始めた場合など、状況によっては、フレームワークがキャッシュからアイテムを期限切れにすることがあります。この場合、アイテムをキャッシュに追加するときに渡すonRemoveCallbackが呼び出されます。

キャッシュは信頼できるストレージではありません。アイテムを使用する前にアイテムが存在するかどうかを常に確認する必要があります。アイテムが存在しない場合は、必要なアクションを実行してフェッチし、そこに戻して、将来の呼び出し元が見つけられるようにする必要があります。

于 2009-11-07T13:46:05.203 に答える