ASP.NET 웹 응용 프로그램에서 사이트의 모든 사용자가 액세스 할 수 있지만 기록 된 사용자에 의해 수정 가능한 단일 상태를 유지하는 데이터 배열을 저장하려고합니다. 이것에 대한 최선의 접근 방법은 무엇입니까? 아마도 응용 프로그램 전체 변수를 사용하는 경로를 찾을 수는 있지만 더 나은 대안이 있습니까? 또한 단순히 데이터베이스에 상태를 저장하고 각 요청을 검색하는 방법을 고려했습니다. 상태가 지속 될 필요가없는 경우 이안사용자간에 세션 또는 응용 프로그램 데이터 공유 - 모범 사례
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. 응용 프로그램 구성을위한 모범 사례
- 3. ASP.net 응용 프로그램 세션 캐시 모범 사례 및 패턴
- 4. 메트로 응용 프로그램의 사용자간에 데이터 공유
- 5. 세션 모범 사례
- 6. Windows에서 사용자간에 응용 프로그램 데이터 읽기/쓰기 (컴퓨터)
- 7. 웹 응용 프로그램 구성 설정 - 모범 사례
- 8. db 인덱스 응용 프로그램 - 모범 사례
- 9. 국제화 모범 사례/레일 응용 프로그램
- 10. 모범 사례, Mac 응용 프로그램 설계
- 11. 이미지 크기 모바일 응용 프로그램 모범 사례
- 12. 모범 사례 또는 불량 사례
- 13. 웹 응용 프로그램 마법사 모범 사례 - 세션 변수 또는 인라인 SQL?
- 14. 데스크톱과 웹 응용 프로그램 간의 데이터 교환 모범 사례
- 15. 서버 호출 애플릿 (또는 WebStart 응용 프로그램) : 모범 사례?
- 16. 데이터 영역 모범 사례
- 17. 모범 사례 : 응용 프로그램 풀 ID 계정
- 18. J2EE 응용 프로그램/Bean 구성 모범 사례?
- 19. Java EE 응용 프로그램 배포 모범 사례
- 20. 응용 프로그램 전체를 사용하는 모범 사례 TraceSource
- 21. 응용 프로그램 종속성을 관리하기위한 도구/모범 사례?
- 22. 관리 Salesforce 응용 프로그램 개발 모범 사례?
- 23. 모범 사례 GWTP 응용 프로그램 보안
- 24. 가격 추적 응용 프로그램 : 모범 사례
- 25. ASP.NET MVC의 다국어 응용 프로그램 - 모범 사례?
- 26. Windows의 응용 프로그램 아이콘 모범 사례
- 27. 모든 응용 프로그램 사용자에게 iCloud 데이터 공유
- 28. WCF 모범 사례 데이터 계약, 공유 클래스 및 DTO들에 대한
- 29. Windows 7 레지스트리로 사용자간에 데이터 공유
- 30. PHP에서 사용자간에 즉시 파일 공유
내가 그런 퀘스트 XML 파일을 사용합니다. – Bastardo