开发者

Static variable across multiple requests

开发者 https://www.devze.com 2023-01-22 12:25 出处:网络
In order to improve speed of chat application, I am remembering last message id in static variable (actually, Dictionary).

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 c开发者_运维知识库ase 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消