经过身份验证的用户之间的 ASP.NET 2 会话状态

发布于 2024-09-16 00:44:05 字数 295 浏览 4 评论 0原文

我正在为客户开发一个网站(ASP.NET、T-SQL)。它是一个数据输入网站,允许许多用户登录并操作同一数据库中的记录。

整个表单都有说明(基本上是字符串列表),告诉用户每个部分要做什么;这些指令本身存在于数据库中。

每次登录时,我都会将这些指令存储在每个经过身份验证的用户的 Session[] 对象中。每个人的说明都是相同的。

我研究过一个解决方案,建议在数据库中存储一个通用会话标识符,然后查询它以重新使用该特定会话,但这似乎非常hacky。实现这一目标的最佳实践解决方案是什么?是否有一个可供所有用户使用的“通用”对象?

I am developing a website for a client (ASP.NET, T-SQL). It is a data-entry website allowing many of their users to login and manipulate records in the same database.

There are instructions (basically a list of string) throughout the form, telling the users what to do for each section; these instructions are themselves present in the database.

On each login, I store these instructions in the Session[] object per authenticated user. The instructions are identical for everyone.

I've looked at a solution which suggested storing a common session identifier in the database and then querying it to re-use that particular session but this seems very hacky. What is a best-practices solution to accomplish this? Is there a 'common' object available to all users?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离旧人 2024-09-23 00:44:05

首先,现在重要吗?是的,这是不好的做法并且效率低下,但是如果您在内存中存储 20Kb 的字符串并且最多有 100 个用户,那么这就是 2,000Kb 的数据。几乎没有多少内存被“浪费”。即使是 200Kb 的字符串,这也是 20,000Kb 的数据。再说一遍,不是很多。现在,这是否值得您花费时间,而客户正在等待您解决问题?

如果您决定这样做,那么您可以:

  1. 将字符串存储在 Application 对象或静态类中,以便检索一次并多次使用它们。
  2. 检索每个页面视图上的字符串。这可能不会像看起来那样损害性能。
  3. 使用类似 的内容System.Web.Caching 中的缓存类
  4. 利用 输出缓存
  5. 利用 Windows Server AppFabric“Velocity”内存缓存

Firstly, does it matter at this point? Yes, it's bad practice and inefficent, but if you're storing 20Kb of strings in memory and have a maximum of 100 users, that's 2,000Kb of data. Hardly a lot of memory "wasted". Even at 200Kb of strings, that's 20,000Kb of data. Again, not a lot. Is it worth your time, and the client waiting for you to solve it, right now?

If you decide it is then you could:

  1. Store the strings in the Application object or a static class so that they're retrieved once and used many times.
  2. Retrieve the strings on every page view. This may not be as performance damaging as it seems.
  3. Use something like the Cache class in System.Web.Caching.
  4. Make use of Output Caching.
  5. Make use of Windows Server AppFabric "Velocity" memory cache.
旧伤还要旧人安 2024-09-23 00:44:05

在我看来,您正在寻找应用程序缓存。与会话一样,它是内存中的数据缓存。与Session不同的是,它是在所有用户之间共享的;每个用户都不会获得自己的数据副本。此外,当您将数据元素添加到缓存时,您可以指定自动使该数据无效的条件,并导致其重新加载/刷新(当您很少更改的数据实际上确实发生更改时很有用:)。

下面的一些文章将为您提供有关使用应用程序缓存(以及 ASP.NET 中的一些其他缓存选项)所需了解的所有信息:

Sounds to me like you're looking for the Application Cache. Like the Session, it is an in-memory cache of data. Unlike the session, it is shared among all users; each user doesn't get their own individual copy of the data. Also, when you add data elements to the cache, you can specify criteria which will automatically invalidate that data, and cause it to be reloaded/refreshed (useful when your seldom-changing data actually does change :).

Here's some articles which should give you everything you need to know about using the Application cache (and some other caching options within ASP.NET as well):

像你 2024-09-23 00:44:05

我建议使用应用程序级缓存对象。它作为 HttpContext 的一部分随处可用。您可以在 App_Start 上填充它。

您可以将任何类型的对象放入缓存中,但显然越小越好。

以下是如何使用 C# 填充它的一些示例:

1) 将项目添加到缓存,就像通过指定项目的键和索引将项目添加到字典一样。价值。
示例:将文本框的当前 Value 属性添加到缓存中。

Cache["txt1"] = txtName.value;

Cache["result"] = dataset;

2) Insert 方法已重载,允许您定义正在使用的版本的参数值。
示例:仅添加一个项目键 &值:

Cache.Insert(“MyData1”,connectionString);

3) Add 方法与 Insert 方法具有相同的签名,但它返回一个表示您添加的项目的对象。

Cache.Add("MyData1", connectionString);

从缓存中检索:

stringName = Cache["MyData"];

如果缓存的数据不是字符串,您可能需要将其转换为正确的数据类型。

result = (DataSet)Cache["result"];

与 Application 对象相比,使用 Cache 对象的好处之一是,如果系统面临内存不足的危险,CLR 将转储 Cache 的内容。

I would suggest using the application-level Cache object. It is available everywhere as part of HttpContext. You can populate it on App_Start.

You can put any kind of object into Cache, though obviously, the smaller the better.

Here are some examples of how to populate it using C#:

1) Add items to the cache as you would add items to a dictionary by specifying the item's key & value.
Example: add the current Value property of a text box to the cache.

Cache["txt1"] = txtName.value;

or

Cache["result"] = dataset;

2) The Insert method is overloaded, allowing you to define values for the parameters of the version you're using.
Example: add only an item key & value:

Cache.Insert("MyData1", connectionString);

3) The Add method has the same signature as the Insert method, but it returns an object representing the item you added.

Cache.Add("MyData1", connectionString);

To retrieve the from cache:

stringName = Cache["MyData"];

If the cached data is not a string, you may need to cast it to the proper data type.

result = (DataSet)Cache["result"];

One of the benefits of using the Cache object as opposed to the Application object is that the CLR will dump contents of Cache if the system is in danger of running out of memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文