在ASP.NET中将数据临时保存在内存中的最佳方法是什么(在用户级别和再次使用数据)? [英] What should be best approach to keep data in memory temporarily(at user level & for reuse the data) in ASP.NET?

查看:104
本文介绍了在ASP.NET中将数据临时保存在内存中的最佳方法是什么(在用户级别和再次使用数据)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用会话"将数据表保留在内存中.但是经过几次研发之后,我才知道这不是一个好习惯例如

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"))

要求:

  • 根据学生类型,下拉菜单项将有所不同.
  • 将从数据库中获取下拉数据,并使用这些控件在一个以上的屏幕中.
  • 从同一屏幕的不同屏幕中,不希望进行多次数据库调用
    数据.
  • 所以我只需要调用一个数据库调用,将数据保留在内存中,然后
    然后下次再从内存中读取数据.

我也尝试使用缓存",但是问题是缓存不是用户唯一的.数据缓存的范围是在应用程序域内,而不是会话".每个用户都可以访问此对象"

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".

请帮助我.

推荐答案

对于您的方案,HttpContext.Current.Cache应该可以工作.是缓存不是用户唯一的.但是可以使缓存键唯一.

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;

然后,您可以使用唯一的cachekey插入并稍后为特定学生获取值.
会话也在应用程序级别.每个用户都有一个ASP.NET_SessionId cookie,该cookie从客户端发送,并在服务器端用于存储/检索值.
注意:为了使Session和Asp.Net Cache在负载平衡的环境中工作,负载平衡器应该是粘性的.

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.

这篇关于在ASP.NET中将数据临时保存在内存中的最佳方法是什么(在用户级别和再次使用数据)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆