How can I store data in memory of asp.net mvc application which can be accessed by all users logged in.
Asked
Active
Viewed 5,852 times
0
-
that's part of the question - essentially want to store it in RAM, not DB storage. Essentially HttpContext.Current.Application variable that should be common for all users, but not sure if that's the best approach.... – Lyubomir Velchev Jun 05 '15 at 09:49
-
Data will anyways be stored in RAM. – Rahul Jun 05 '15 at 09:50
3 Answers
1
You can use a static variable. If you have some static Constants class, just create a static list or a static variable.
public static List<string> commonList = new List<string>

shwetaOnStack
- 594
- 4
- 13
-
-
according to [this](http://stackoverflow.com/questions/10960695/using-static-variables-instead-of-application-state-in-asp-net) answer, Yes. It also gives reference to Microsoft support link which explains it. – shwetaOnStack Jun 06 '15 at 07:58
-
0
You can store the value in your application using something like this:
protected void Application_Start()
{
HttpContext.Current.Application["somevar"] = "0";
}

cramopy
- 3,459
- 6
- 28
- 42

Rahul Tripathi
- 168,305
- 31
- 280
- 331
-
I guess I should do locking as this is common resource for all users? – Lyubomir Velchev Jun 05 '15 at 09:51
-
@LyubomirVelchev:- Indeed you can by using the Application variable your all users can use the variable value once the application starts. – Rahul Tripathi Jun 05 '15 at 09:53
0
Something like that will work for me:
Application.Lock();
Application["PageRequestCount"] =
((int)Application["PageRequestCount"])+1;
Application.UnLock();
Here I can save data from user and store it in a data object which will be in application - stored in memory. My idea is to make a chat, but not use a db, relying solely on RAM. So every user will have their messages in their own session, when the user want to submit a message, it will lock and save in messages collection common for all users and the unlock. When the user read - there will be no need for unlocking. Just elaborating about possible use of that, but the answer of the question is the 3 lines source code.

Lyubomir Velchev
- 962
- 2
- 11
- 30