Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 08, 2012 09:38 AM by pantonis
May 07, 2012 03:15 PM|LINK
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?
May 07, 2012 03:22 PM|LINK
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).
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?
May 08, 2012 06:43 AM|LINK
In your original post you mentioned Dictionary instead so I assumed you put it in session state.
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?
May 08, 2012 07:24 AM|LINK
Dictionary is stored in session
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.
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.
May 08, 2012 08:40 AM|LINK
If I use a static ConcurrentDictionary<int, List<T>> this will be stored in memory?
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.
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.
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?
May 08, 2012 09:15 AM|LINK
I wont have any problem with serialization when storing it into memcached?
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.