Im using MVC3. I have a large application that uses Dictionary<int, List<T>> to store data in memory for each user. The problem is that these data consume a lot of memory in my IIS. Is there any other alternative way of achieving caching?
get rid of the information that you don't need in the first place. Also if the information's the same for every user then make use of the Cache object instead of Session state.
If that can't be done then you can make use of memcached for example or simply keep the information in the database and retrieve it when necessary (and then you might opt to microcache it for like one minute or even a certain amount of seconds in the Cache
object and make use of a dedicated key per user).
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
ConcurrentDictionary<int, List<T>> is part of the Session? I thought that it was in application's memory.
The database solution unfortunately cannot be done because very heavy queries exist and that's why I keep them in ConcurrentDictionary. Can you give me a link with more information to Cache and memcached?
No, you need to put it in Session state yourself, or in Cache, if you want it so. A ConcurrentDictionary however is thread safe. I don't know if you need that but if you simply want to put information in memory for a specific user you can also put it in
a Dictionary and put that in Session state. However note that Session state is limited when you make use of the default InProc setting so.
A general rule of thumb in web development: only put the least necessary information in Session state, not more as it's a valuable resource which needs to be handled with care.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Marked as answer by pantonis on May 08, 2012 09:37 AM
I'm sticking with the ConcurrentDictionary as I need to store data from database for each user and I prefer it because its thread safe.
So my question. If I use a static ConcurrentDictionary<int, List<T>> this will be stored in memory? I have also read some articles about AppFabric which can be used for distributed caching but unfortunately support for AppFabric is minimal yet. I cannot
even find a book to buy for programming with appfabric.
However what you didn't make clear yet is that the information which you want to put into it is unique for every visitor or is the same for every visitor? If the latter's the case then rather put the Dictionary in Cache instead.
pantonis
I have also read some articles about AppFabric which can be used for distributed caching
Yes it is distributed caching but at the moment I believe memcached is still the ruling distributed caching mechanism and is used on several platforms, not only on Windows.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
pantonis
Member
328 Points
260 Posts
Caching problem
May 07, 2012 03:15 PM|LINK
Hi,
Im using MVC3. I have a large application that uses Dictionary<int, List<T>> to store data in memory for each user. The problem is that these data consume a lot of memory in my IIS. Is there any other alternative way of achieving caching?
Thanks
Please mark this post as answer if it helped you solve your problem
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: Caching problem
May 07, 2012 03:22 PM|LINK
Hi,
get rid of the information that you don't need in the first place. Also if the information's the same for every user then make use of the Cache object instead of Session state.
If that can't be done then you can make use of memcached for example or simply keep the information in the database and retrieve it when necessary (and then you might opt to microcache it for like one minute or even a certain amount of seconds in the Cache object and make use of a dedicated key per user).
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
pantonis
Member
328 Points
260 Posts
Re: Caching problem
May 08, 2012 06:15 AM|LINK
Kris thanks for your reply.
ConcurrentDictionary<int, List<T>> is part of the Session? I thought that it was in application's memory.
The database solution unfortunately cannot be done because very heavy queries exist and that's why I keep them in ConcurrentDictionary. Can you give me a link with more information to Cache and memcached?
Thanks
Please mark this post as answer if it helped you solve your problem
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: Caching problem
May 08, 2012 06:43 AM|LINK
Hi,
In your original post you mentioned Dictionary instead so I assumed you put it in session state.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
pantonis
Member
328 Points
260 Posts
Re: Caching problem
May 08, 2012 07:02 AM|LINK
yes sorry.my fault. So.
Dictionary is stored in session whereas ConcurrentDictionary is stored in memory. Is that right?
Thanks again
Please mark this post as answer if it helped you solve your problem
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: Caching problem
May 08, 2012 07:24 AM|LINK
Hi,
No, you need to put it in Session state yourself, or in Cache, if you want it so. A ConcurrentDictionary however is thread safe. I don't know if you need that but if you simply want to put information in memory for a specific user you can also put it in a Dictionary and put that in Session state. However note that Session state is limited when you make use of the default InProc setting so.
A general rule of thumb in web development: only put the least necessary information in Session state, not more as it's a valuable resource which needs to be handled with care.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
pantonis
Member
328 Points
260 Posts
Re: Caching problem
May 08, 2012 07:32 AM|LINK
I'm sticking with the ConcurrentDictionary as I need to store data from database for each user and I prefer it because its thread safe.
So my question. If I use a static ConcurrentDictionary<int, List<T>> this will be stored in memory? I have also read some articles about AppFabric which can be used for distributed caching but unfortunately support for AppFabric is minimal yet. I cannot even find a book to buy for programming with appfabric.
Please mark this post as answer if it helped you solve your problem
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: Caching problem
May 08, 2012 08:40 AM|LINK
Hi,
Yes. I haven't tested it myself yet but from what I noticed on this thread http://stackoverflow.com/questions/3383059/concurrentdictionary-as-static-cache you might want to put it in a Singleton wrapper.
However what you didn't make clear yet is that the information which you want to put into it is unique for every visitor or is the same for every visitor? If the latter's the case then rather put the Dictionary in Cache instead.
Yes it is distributed caching but at the moment I believe memcached is still the ruling distributed caching mechanism and is used on several platforms, not only on Windows.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
pantonis
Member
328 Points
260 Posts
Re: Caching problem
May 08, 2012 09:10 AM|LINK
Data are different for every visitor. So I suppose from your answer using ConcurrentDictionary<int, List<T>> would be better.
But by using ConcurrentDictionary which contains List<T> I wont have any problem with serialization when storing it into memcached?
Thanks
Please mark this post as answer if it helped you solve your problem
XIII
All-Star
182690 Points
23458 Posts
ASPInsiders
Moderator
MVP
Re: Caching problem
May 08, 2012 09:15 AM|LINK
Hi,
Didn't try that one out yet so I won't be able to answer that question now. You can try and see if it works or not.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!