重新创建对象或将其存储在会话变量中更快吗?

发布于 2024-10-01 08:55:18 字数 220 浏览 3 评论 0原文

抱歉,如果这对于非菜鸟来说似乎是显而易见的。是否更快:

  • 每次有人在会话期间访问页面时重新创建一个对象实例

,或者

  • 在第一次创建对象实例时将其存储在会话变量中,然后在再次访问页面时始终从那里获取它

我不是确定这是否会变成“一根绳子有多长?”类似的问题,但如果确实如此,那么也许您可以让我知道做出决定涉及哪些因素?

Sorry if this seems obvious to the non-noob. Is it faster to:

  • Recreate an object instance each time someone goes to a page during a session

or

  • Store the object instance in a session variable when it first gets created, then always grab it from there when the page is accessed again

I'm not sure if this will turn out to be a "How long is a piece of string?" sort of question, but if it does, then perhaps you could let me know what factors are involved in making the decision?

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

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

发布评论

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

评论(5

殊姿 2024-10-08 08:55:18

会话数据存储为文本,而不是二进制数据,因此当您将其扔到会话中时,无论如何都会在幕后的某个地方重新创建对象。它可能比自己初始化要慢一点,因为它必须进行一些字符串解析,但我怀疑这值得担心。简而言之,这两种方式可能都没有什么区别。

Session data is stored as text, not binary data so somewhere behind the scenes when you toss it into the session the object is recreated anyway. It's probably a little bit slower than initializing it yourself since it has to do some string parsing but I doubt it's much to worry about. In short, it probably doesn't make a difference either way.

空宴 2024-10-08 08:55:18

这当然取决于创建对象时完成了多少逻辑。您应该对这两种变体进行一些基准测试。

It certainly depends on how much logic is done, when the object is created. You should do some benchmarks with both variations.

暮光沉寂 2024-10-08 08:55:18

如果不进行测量,我会说存储和检索大多数时候应该更快。
为了重新创建对象,您必须调用可能的多个构造函数等,而检索不应调用任何函数调用。

Without measuring it, i'd say storing and retrieving should most times be faster.
For recreating objects you have to call possibly multiple constructors, etc, whereas retrieving shouldn't invoke any function calls.

茶色山野 2024-10-08 08:55:18

这显然取决于对象中的字段、字段数量以及填充方式。无论如何,每次加载页面时都会发生对象的实例化,因此这是字段及其来源的问题。

It obviously depends on what fields you have in object, how many of them and how they are populated. Instantion of an object occurs every time you load a page anyway, so it's the matter of fields and their sources.

洛阳烟雨空心柳 2024-10-08 08:55:18

只是要小心不要过于努力地优化它。请记住,在会话中存储项目可能会很繁重,特别是当您的网站流量很高时。

另外,我见过很多人创建一个对象,该对象访问数据库并加载其属性。然后将其存储在会话中,在回发时更新,然后保存回数据库。

这很好,但它会导致并发检查变得困难 - 假设您的对象有上次保存的时间戳 - 如果您每次在保存之前重新加载它,您可以轻松检查自上次加载以来时间戳是否已更改,其中如果您可能需要停止继续保存。

无论哪种方式,差异都不会很大。

Just be wary of trying too hard to optimise this. Bear in mind that storing items in session can be heavy, particularly if your site is high traffic.

Also, I've seen a lot of people create an object, which accesses a database and loads it's attributes. This then gets stored in session, updated on postback and then saved back to teh database.

This is fine, but it makes for difficult concurrency checking - say your object has a timestamp of the last time saved - if you reload it each time prior to saving, you can easily check if the timestamp has changed since the last load, in which case you may need to stop the save from going ahead.

Either way, the difference is not going to be huge.

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