ASP.NET Webアプリケーションでは、サイトのすべてのユーザーがアクセス可能なデータの配列を格納したいが、ログに記録されているユーザーこのための最良のアプローチは何ですか?私はおそらくアプリケーションワイド変数を使用するルートに行くことができると理解していますが、より良い選択肢がありますか?また、データベースに状態を格納し、各要求を取得することも考えました。状態は永続的である必要はありません場合は イアンユーザー間でセッションまたはアプリケーションデータを共有する - ベストプラクティス
0
A
答えて
2
1
私の経験は、System.Web.HttpContext.Current.Cache
とキャッシュファイルを使用しています。 OSがメモリ不足の場合、IISはすべてのキャッシュをクリアします。すべてのユーザーの状態を保持したい場合は、状態コピーをファイルに保存する必要があります(ファイル内のオブジェクトのシリアル化)。
例:多くのユーザーがキャッシュリストを取得したい場合、キャッシュリストをキャッシュに保存します.IISがキャッシュオブジェクトをリセットすると、キャッシュファイルで読み取ることができます。
Stack<string> list = null;
List<string> returnlist = new List<string>();
try
{
if (System.Web.HttpContext.Current.Cache["PushQueueList"] != null)
{
try
{
list = (Stack<string>)System.Web.HttpContext.Current.Cache["PushQueueList"];
}
catch (Exception ex)
{
list = null;
//Log.LogToFile("PushQueueListError1:" + ex.Message);
}
}
if (list == null)//memory cache is null ,then read cache file
{
try
{
list = (Stack<string>)Serialize.DeSerializeObj("pushqueue");//Deserialize object from file
}
catch (Exception ex)
{
list = null;
//Log.LogToFile("PushQueueListError2:" + ex.Message);
}
}
if (list == null || list.Count == 0)
{
if (list!=null && list.Count == 0)
{
return new List<string>();
}
try
{
System.Web.HttpContext.Current.Cache.Remove("PushQueueList");
}
catch (Exception)
{
}
//Log.LogToFile("PushQueueList is empty,reload it");
DataSet ds = DB.GetSendQueueUserIDList(); ;
if (ds != null)
{
list = new Stack<string>();
DataView dv = ds.Tables[0].DefaultView;
for (int i = 0; i < dv.Count; i++)
{
list.Push(dv[i].Row["UserID"].ToString());
}
dv.Dispose();
ds.Dispose();
}
}
}
catch (Exception ex)
{
//Log.LogToFile("PushQueueListError:" + ex.Message);
}
if (list != null && list.Count > 0)
{
for (int i = 0; i < ReturnCount; i++)
{
if (list.Count > 0)
{
returnlist.Add(list.Pop());
}
else
{
Log.LogToFile("PushQueueList OK");
break;
}
}
System.Web.HttpContext.Current.Cache.Add("PushQueueList", list, null, DateTime.Now.AddDays(1),
TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default,
null);
Serialize.SerializeObj(list, "pushqueue");
}
return returnlist;
}
シリアル方法は以下の通りです:
public static bool SerializeObj(object obj, string FileName)
{
string LogFileDir = System.Configuration.ConfigurationManager.AppSettings["LogFile"];
if (!System.IO.Directory.Exists(LogFileDir))
{
System.IO.Directory.CreateDirectory(LogFileDir);
}
string FilePath = LogFileDir + FileName + "_" + DateTime.Now.ToString("yyyyMMdd") + ".bin";
try
{
IFormatter _formatter = new BinaryFormatter();
using(Stream _stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
_formatter.Serialize(_stream, obj);
_stream.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public static object DeSerializeObj(string FileName)
{
try
{
string LogFileDir = System.Configuration.ConfigurationManager.AppSettings["LogFile"];
if (!System.IO.Directory.Exists(LogFileDir))
{
System.IO.Directory.CreateDirectory(LogFileDir);
}
object objStd = null;
string FilePath = LogFileDir + FileName + "_" + DateTime.Now.ToString("yyyyMMdd") + ".bin";
using(Stream _stream = File.Open(FilePath, FileMode.Open))
{
BinaryFormatter _b = new BinaryFormatter();
objStd = _b.Deserialize(_stream);
_stream.Close();
}
return objStd;
}
catch (Exception ex)
{
return null;
}
}
関連する問題
- 1. マルチテナント環境でテナント間でユーザーを共有するためのベストプラクティス
- 2. CakePHPの開発者間で必要なアプリケーションデータを共有
- 3. ノードアプリケーション間の共有セッション
- 4. ページ間でデータを共有するためのベストプラクティス
- 5. Rails 3.1アプリケーション間でセッションを共有
- 6. セッション内のサーブレット間でアップロードされたファイルを共有する
- 7. RailsとSpring MVC間のセッションの共有
- 8. Play 2.Xのサブドメイン間でセッションまたはCookieを共有する
- 9. リアルタイムゲームのスレッド間でオブジェクトを共有するためのベストプラクティスは何ですか
- 10. ベストプラクティス - Asp.net MVCの階層間でUnityContainerを共有しますか?
- 11. ベストプラクティス:Eclipseでプロジェクト間でファイルを共有する
- 12. 共有NSDateFormatter - ベストプラクティス?
- 13. Rails内の悪意のあるユーザー間のセッション共有を停止する
- 14. 共有セッションは、全体で
- 15. ユーザー空間とカーネルスレッド間の共有メモリ
- 16. Androidアプリ:ユーザー間でデータを共有する
- 17. IIS7:状態サーバーを持つアプリケーション間でセッションを共有する
- 18. 2台のサーバー間でセッションを共有する
- 19. JSFビューはユーザー間で共有されていますか?
- 20. 異なるアプリケーションであるサブドメイン間でセッションを共有できますか?
- 21. Googleドライブでアプリケーションデータを共有することは可能ですか?
- 22. PHP ASP共有セッション
- 23. RichFaces:セッション間でJSFビューを共有する必要があります
- 24. phpBBとレールアプリ間のセッションを共有する
- 25. IISサーバー上の.aspxと.phpの間のセッションを共有する
- 26. PHPの異なるセッション間で共有オブジェクトを作成するには?
- 27. ARMテンプレート間で変数、または定数を共有する
- 28. Sharepointサイト間でデータを共有する - ロールアップまたはプルダウン?
- 29. アプリケーション間でユーザー/クライアント/グループデータを共有する
- 30. iPhoneとiPad間で開発コードを共有するためのベストプラクティスはありますか
私は、このようなクエスト用のXMLファイルを使用します。 – Bastardo