What should be best approach to keep data in memory temporarily(at user level & for reuse the data) in ASP.NET?

谁说胖子不能爱 提交于 2019-12-25 16:42:42

问题


Currently I am using 'Session' to keep the datatables in memory. But after doing few R&Ds, I came to know the it is not a good practice e.g.

Session("Syllabus") = RegistartionLogic.GetSyllabusInfo(Session("StudentID"))

Requirement:

  • The items of dropdown will be different based on student-type.
  • The dropdown data will be fetched from DB and these controls are used in more than one screen.
  • Multiple DB call is not preferred from different screens for same
    data.
  • So I need to call only one DB call, keep the data in memory and
    then read data from memory next time onward.

I tried with 'cache' as well, but the issue was "Cache is not unique to the user.The scope of the data caching is within the application domain unlike "session". Every user can able to access this objects".

Kindly help me out.


回答1:


For your scenario, HttpContext.Current.Cache should work. Yes cache is not unique to the user. But cache key can be made unique.

var studentId = GetStudentIdFromRequest();
var cacheKey = "SyllabusInfoCacheKey_" + studentId;

Then you can make use of the unique cachekey, to insert and later get values for the particular student.
Session is also at Application level. Every user has a ASP.NET_SessionId cookie that is sent from client side and used at the server side to store/retrieve values.
Note: For Session and Asp.Net Cache to work in a load balanced environment, load balancer should be sticky.



来源:https://stackoverflow.com/questions/43324625/what-should-be-best-approach-to-keep-data-in-memory-temporarilyat-user-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!