在Web应用程序中的静态变量 [英] Static variables in web applications

查看:119
本文介绍了在Web应用程序中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Web应用程序中使用静态变量?有什么办法,以静态的?当我在页面中使用静态变量和一个以上的用户使用的应用程序,它使数据冲突(不正确的数据)。

Can I use static variables in my web application ? what are the alternatives to static ? When I use static variables in pages and more than one user use the application, it makes conflict data (incorrect data).

什么是使用静态成员的限制呢?

What are the limits of using static members?

是静态成员在内存中共享?

Are static members shared in memory?

推荐答案

考虑HttpApplication对象或缓存对象存储的共享变量。

Consider storing your shared variables in the HttpApplication object or in the Cache object.

不过,如果你想单独存储值为每个用户,你应该存储在一个会话变量的值。

However, if you are trying to store values for each user separately, you should store those values in a Session variable.

里面Asp.Net静态变量的w3svc.exe进程的内存空间是共享的,不是线程安全的。它们可以被访问和应用程序的任何用户修改。这可能会导致不必要的修改,除非你写围绕这些值的存储你自己的锁机制。

Static variables inside Asp.Net are shared in the memory space of the w3svc.exe process and are NOT thread-safe. They can be accessed and modified by any user of the application. This could lead to unwanted modifications, unless you write your own lock mechanism around the storage of those values.

您应该尝试像语法:

Application["KEY"] = value;

存储共享应用程序范围内的数据和

to store shared application-wide data and

Session["KEY"] = value;

将数据存储在每个用户的基础

to store data on a per-user basis

您可以使用证明WebCache对象将数据存储在Web服务器的内存,终止条件。语法看起来类似于:

You can use the WebCache object to store data in the web server's memory with expiration conditions. Syntax for that looks similar to:

Page.Cache.Insert("KEY", "VALUE", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

更多关于管理证明WebCache对象的语法,可以发现:

More on the syntax of managing the WebCache object can be found at:

<一个href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx">http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

这篇关于在Web应用程序中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆