跨多个请求的静态变量

发布于 2024-09-29 15:15:06 字数 567 浏览 2 评论 0原文

为了提高聊天应用程序的速度,我记住了静态变量(实际上是字典)中的最后一条消息ID。

然而,似乎每个线程都有自己的副本,因为用户不会在生产(单服务器环境)上获得更新。

private static Dictionary<long, MemoryChatRoom> _chatRooms = new Dictionary<long, MemoryChatRoom>();

没有使用treadstaticattribute...

在所有应用程序进程中共享几个整数的快速方法是什么?

更新

我知道网络必须是无状态的。然而,每条规则都有一个例外。目前,所有数据都在 ms sql 中存储,在这种特殊情况下,某些共享内存将显着提高性能,并允许避免无用的 sql 请求。

我已经很多年没有使用静态了,所以我什至错过了它开始在同一个应用程序中成为多个实例的时刻。

那么,问题是在进程之间共享内存对象的最简单方法是什么?目前,我的解决方法是远程处理,但有很多额外的代码,而且我不能 100% 确定这种方法的稳定性。

In order to improve speed of chat application, I am remembering last message id in static variable (actually, Dictionary).

Howeever, it seems that every thread has own copy, because users do not get updated on production (single server environment).

private static Dictionary<long, MemoryChatRoom> _chatRooms = new Dictionary<long, MemoryChatRoom>();

No treadstaticattribute used...

What is fast way to share few ints across all application processes?

update

I know that web must be stateless. However, for every rule there is an exception. Currently all data stroed in ms sql, and in this particular case some piece of shared memory wil increase performance dramatically and allow to avoid sql requests for nothing.

I did not used static for years, so I even missed moment when it started to be multiple instances in same application.

So, question is what is simplest way to share memory objects between processes? For now, my workaround is remoting, but there is a lot of extra code and I am not 100% sure in stability of this approach.

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

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

发布评论

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

评论(2

心意如水 2024-10-06 15:15:06

我假设您是网络编程新手。 Web 应用程序与常规控制台或 Windows 窗体应用程序的主要区别之一是它是无状态的。这意味着每个页面请求基本上都是从头开始初始化的。您正在使用数据库来维护状态,但您发现这相当慢。幸运的是,您还有其他选择。

如果您想记住每个用户经常访问的内容(例如,他们的用户名),那么您可以使用 session.我建议在此处阅读会话状态。但要小心,不要滥用会话对象——因为每个用户都有自己的会话副本,因此它很容易使用大量 RAM,并导致比数据库更多的性能问题。

如果要缓存与应用程序的所有用户相关的信息,ASP.NET 提供了 数据缓存框架。使用它的最简单方法就像字典一样,例如:

Cache["item"] = "Some cached data";

我建议详细阅读 ASP.NET 中缓存的各种选项 此处

不过总的来说,我建议您在熟悉网络编程之前不要考虑缓存。与任何类型的全球共享数据一样,它可能会导致不可预测的问题,如果滥用,这些问题很难诊断。

I'm assuming you're new to web programming. One of the key differences in a web application to a regular console or Windows forms application is that it is stateless. This means that every page request is basically initialised from scratch. You're using the database to maintain state, but as you're discovering this is fairly slow. Fortunately you have other options.

If you want to remember something frequently accessed on a per-user basis (say, their username) then you could use session. I recommend reading up on session state here. Be careful, however, not to abuse the session object -- since each user has his or her own copy of session, it can easily use a lot of RAM and cause you more performance problems than your database ever was.

If you want to cache information that's relevant across all users of your apps, ASP.NET provides a framework for data caching. The simplest way to use this is like a dictionary, eg:

Cache["item"] = "Some cached data";

I recommend reading in detail about the various options for caching in ASP.NET here.

Overall, though, I recommend you do NOT bother with caching until you are more comfortable with web programming. As with any type of globally shared data, it can cause unpredictable issues which are difficult to diagnosed if misused.

黑凤梨 2024-10-06 15:15:06

到目前为止,还没有一种简单的方法可以在进程之间进行通信。 (也许这基于隔离、缩放是很好​​的)。例如,这里明确提到: ASP.Net 静态对象

当您确实需要 Web 应用程序/服务来记住内存中的某些状态,而不是在数据库中,您有以下选项:

  1. 您可以最大进程数 = 1。需要将这段代码移动到单独的 Web 应用程序。如果您将其设置为单独的子域,则从 JS 访问它时将会遇到跨站点脚本问题。

  2. 远程/WCF - 您可以在远程应用程序中托管关键数据,并从 Web 应用程序访问它。

  3. 在每个进程中存储数据并通过 memcached 同步更改。 Memcached 没有实际数据,因为传输它需要很长时间。每个集合仅包含最后更改日期。

使用#3,我可以从单个服务器实现每秒超过 100 个页面。

So far, there is no easy way to comminucate between processes. (And maybe this is good based on isolation, scaling). For example, this is mentioned explicitely here: ASP.Net static objects

When you really need web application/service to remember some state in memory, and NOT IN DATABASE you have following options:

  1. You can Max Processes count = 1. Require to move this piece of code to seperate web application. In case you make it separate subdomain you will have Cross Site Scripting issues when accesing this from JS.

  2. Remoting/WCF - You can host critical data in remoting applcation, and access it from web application.

  3. Store data in every process and syncronize changes via memcached. Memcached doesn't have actual data, because it took long tim eto transfer it. Only last changed date per each collection.

With #3 I am able to achieve more than 100 pages per second from single server.

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