ASP.NET Session - 大对象与许多小对象

时间:2022-02-04 16:55:00

I have a scenario to optimise how my web app is storing data in the session and retrieving it. I should point out that I'm using SQL Server as my session store.

我有一个方案来优化我的Web应用程序如何在会话中存储数据并检索它。我应该指出我使用SQL Server作为我的会话存储。

My scenario is I need to store a list of unique IDs mapped to string values in the user's session for later use. The current code I've inherited is using a List<T> with a custom object but I can already see some kind of dictionary is far better for performance.

我的方案是我需要存储一个映射到用户会话中字符串值的唯一ID列表供以后使用。我继承的当前代码是使用带有自定义对象的List ,但我已经可以看到某种字典对于性能来说要好得多。

I've tested two ideas for alternatives:

我已经测试了两个替代方案的想法:

  1. Storing a Dictionary<int, string> in the session. When I need to get the strings back, I get the dictionary from the session once and can test each ID on the dictionary object.

    在会话中存储Dictionary 。当我需要返回字符串时,我从会话中获取字典一次,并可以测试字典对象上的每个ID。 ,string>

  2. Since the session is basically like a dictionary itself, store the string directly in the session using a unique session key e.g. Session["MyString_<id>"] = stringValue". Getting the value out of the session would basically be the inverse operation.

    由于会话基本上类似于字典本身,因此使用唯一的会话密钥将字符串直接存储在会话中。 Session [“MyString_ ”] = stringValue“。从会话中获取值基本上是反向操作。

My test results show the following based on the operation I need to do and using 100 strings:

我的测试结果显示以下基于我需要做的操作并使用100个字符串:

  • Dictionary - 4552 bytes, 0.1071 seconds to do operation
  • 字典 - 4552字节,0.1071秒做操作

  • Session Direct - 4441 bytes, 0.0845 seconds to do operation
  • 会话直接 - 4441字节,0.0845秒做操作

From these results I see that I save some space in the session (probably because I've not got the overhead of serialising a dictionary object) and it seems to be faster when getting the values back from the session, maybe because strings are faster to deserialise than objects.

从这些结果中我看到我在会话中节省了一些空间(可能是因为我没有得到序列化字典对象的开销)并且从会话中获取值时似乎更快,可能因为字符串更快到反序列化比对象。

So my question is, is it better for performance to store lots of smaller objects in session rather than one big one? Is there some disadvantage for storing lots of smaller objects vs. one bigger object that I haven't seen?

所以我的问题是,在会话中存储大量较小的对象而不是一个大的对象是否更好?存储大量较小的物体与一个我没见过的较大物体有什么不利之处吗?

2 个解决方案

#1


1  

There are penalties for serializing and searching large objects (they take up more space and processor time due to the need to represent a more complex structure).

序列化和搜索大型对象会受到惩罚(由于需要表示更复杂的结构,它们会占用更多空间和处理器时间)。

And why do 2 searches when you can do only one.

当你只能做一次时,为什么要进行2次搜索?

Also, all documentation that deal with caching/storing solutions mention that it is much more efficient to serialize a single value from a list based on a computed key, rather than store all the dictionary and retrieve that and search in it.

此外,所有处理缓存/存储解决方案的文档都提到,基于计算密钥从列表中序列化单个值更有效,而不是存储所有字典并检索并在其中搜索。

#2


0  

I think you have almost answered your own question in showing that that yes, there is an overhead with deserializing objects but I think the real reason should be one of manageability and maintainability.

我认为你几乎已经回答了你自己的问题,表明是的,反序列化对象有一个开销,但我认为真正的原因应该是可管理性和可维护性。

The size of storage difference is going to be minimal when you are talking about 100 objects but as you scale this up to 1000's of objects the differences will increase too, especially if you are using complex custom objects. If you have an application that has many users all using 1000's of sessions then you can imagine how this is just not scalable.

当你谈论100个对象时,存储差异的大小将是最小的,但是当你将它扩展到1000个对象时,差异也会增加,特别是如果你使用复杂的自定义对象。如果您的应用程序中有许多用户都使用1000个会话,那么您可以想象这是如何不可扩展的。

Also, by having many session objects you are undoubtedly going to have to write more code to handle each varying object. This may not be a vast amount more, but certainly more. This would also potentially make it more difficult for a developer picking up your code to understand you reasoning etc and therefore extend your code.

此外,通过拥有许多会话对象,您无疑将需要编写更多代码来处理每个不同的对象。这可能不是更多,但肯定更多。这也可能使开发人员更难以理解您的推理等因此扩展您的代码。

If you can handle the session in a single barebones format like a IEnumerable or IDictionary then this in my opinion is preferable even if there is a slight overhead involved.

如果你可以用IEnumerable或IDictionary等单一的准系统格式来处理会话,那么我认为这是优选的,即使涉及到一点点开销。

#1


1  

There are penalties for serializing and searching large objects (they take up more space and processor time due to the need to represent a more complex structure).

序列化和搜索大型对象会受到惩罚(由于需要表示更复杂的结构,它们会占用更多空间和处理器时间)。

And why do 2 searches when you can do only one.

当你只能做一次时,为什么要进行2次搜索?

Also, all documentation that deal with caching/storing solutions mention that it is much more efficient to serialize a single value from a list based on a computed key, rather than store all the dictionary and retrieve that and search in it.

此外,所有处理缓存/存储解决方案的文档都提到,基于计算密钥从列表中序列化单个值更有效,而不是存储所有字典并检索并在其中搜索。

#2


0  

I think you have almost answered your own question in showing that that yes, there is an overhead with deserializing objects but I think the real reason should be one of manageability and maintainability.

我认为你几乎已经回答了你自己的问题,表明是的,反序列化对象有一个开销,但我认为真正的原因应该是可管理性和可维护性。

The size of storage difference is going to be minimal when you are talking about 100 objects but as you scale this up to 1000's of objects the differences will increase too, especially if you are using complex custom objects. If you have an application that has many users all using 1000's of sessions then you can imagine how this is just not scalable.

当你谈论100个对象时,存储差异的大小将是最小的,但是当你将它扩展到1000个对象时,差异也会增加,特别是如果你使用复杂的自定义对象。如果您的应用程序中有许多用户都使用1000个会话,那么您可以想象这是如何不可扩展的。

Also, by having many session objects you are undoubtedly going to have to write more code to handle each varying object. This may not be a vast amount more, but certainly more. This would also potentially make it more difficult for a developer picking up your code to understand you reasoning etc and therefore extend your code.

此外,通过拥有许多会话对象,您无疑将需要编写更多代码来处理每个不同的对象。这可能不是更多,但肯定更多。这也可能使开发人员更难以理解您的推理等因此扩展您的代码。

If you can handle the session in a single barebones format like a IEnumerable or IDictionary then this in my opinion is preferable even if there is a slight overhead involved.

如果你可以用IEnumerable或IDictionary等单一的准系统格式来处理会话,那么我认为这是优选的,即使涉及到一点点开销。