1

Since I have no errors I don't know if this is the right place to ask this question, if it's not, please point me to the right forum. Any way...

I am working on a social network project and now it runs really slow, some pages take like 5 to 15 seconds to load. There is a "cache" structure but whoever did this, didn't use to already build in Cache from the c# .net, instead, they created a static List<object> as the cache of the system. Even tought is not right, i can deal with that...

The problem is, on every page request, I have to load the user data from this cache wich is quite big, and the user structure is quite big as well so I was wondering, stead of loading the user data from cache everytime a page is requested I will create Some sessions with the most used data that every page requires, like, photo, name, nickname, id, and than when I need to load anyother type of data from user that is not common, I request it from the cache...

I don't know if this is the right aproach not if this is the right place to ask it, but I really need to solve this problem pretty bad. so I would like some advices from the experts out here.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rpmlins
  • 135
  • 2
  • 9
  • 3
    Is there a database storing all this data? If so, and it's a decent database engine, and it's on the local network, I would trust it to cache the data in memory for you. There's no reason for you to cache it yourself when the DB engine is already doing it itself, and probably more efficiently. – Steven Doggart Jun 12 '12 at 13:56
  • 1
    Please post the code where you select the current user object(s) from "static List. – paparazzo Jun 12 '12 at 14:16
  • yes there is a database MsSql Server I think is way faster to get it from server cache than from the databse isn't it? Here is a screen of it. since the realations didn't show up and I don't now why, I hope you can understand [link]http://imageshack.us/photo/my-images/689/databasee.jpg/ – rpmlins Jun 12 '12 at 14:17
  • hey @Blam I will try to post the code overhere but since its not only one class, I think I won't be able. The load method loads 'every type of item' lets put this way... an user is a type of Item and has its own unique ID so I pass the ID to the method and I brings me an object with every data of the Item Id i just passed... what the load does is, Get from cache, if it isn't in the cache, get from database, insert into the cache, return the data... – rpmlins Jun 12 '12 at 14:23
  • OK I will post an answer shortly – paparazzo Jun 12 '12 at 14:33

2 Answers2

3

Part of the performance problem may be needing to scan the cache for items. A Dictionary might be better, since you could retrieve items by key. This is a closer implementation to the native cache structure.

Sessions are bad for scalability of a website. If you plan on this being as big as facebook or something, using sessions for caching will kill you

See this other related question :

What is a good way to store large temporary "session" data in a web application

and another decent question

Why is it a bad idea to use Session to store state in high traffic websites?

Community
  • 1
  • 1
Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
  • Well jason, i don't plan to store large data in the sessions, just the ones the is required on every page userid, name, nickname and photo, I don't plan to kill the cache for sure, just trying to minimize the fact that everytime I load I page, I have to read a list with thousands of records to get only 4 or 5 from it... but thankx for poiting the link, it will give me some help – rpmlins Jun 12 '12 at 14:04
  • 1
    @rpmlins Wait, reading a list with thousands of records to get only 4 or 5? You really need a Dictionary here. Of course the better solution will be simply loading it directly from DB. *If* the table are indexed according to your query parameters, it will be instantaneous – Martheen Jun 12 '12 at 14:21
  • Even if the data base is huge? It can pass a million of record very easy and fast – rpmlins Jun 12 '12 at 14:25
  • 1
    assuming a decent database with good indexing, a million records is fairly insignificant – Jason Coyne Jun 12 '12 at 14:26
  • 1
    However, just switching from a list to a dictionary will likely gain you significant performance gains as you wont have to search the entire list each time. However, that will not solve the scalability issue of keeping the cache in memory – Jason Coyne Jun 12 '12 at 14:28
  • So, should I use LINQ to SQL when getting the data from the database? – rpmlins Jun 12 '12 at 14:31
  • by the way @Jason I will definitely not use Session, since I plan the have a multi server structure with load balance. – rpmlins Jun 12 '12 at 14:57
  • linq to sql or some other way of accessing the database doesnt really matter for the purpose of this discussion. any way of getting to the data is fine. I fyou are going to have a multi server structure, how are you going to share the cache across the servers? This is a major downfall of the dictionary method – Jason Coyne Jun 12 '12 at 22:35
2

Agree with Jason +1

From you comment user objects have a key (ID).

Dictionary is way way faster if you have a key. Furthermore override gethash and equals if your objects have a natural key that can be expressed as Int32. A dictionary lookup against 10,000 should be milliseconds.

A keyed collection sound like what you needs. With a keyed collection one of the properties (ID) is the key.
keyedcollection

If this is a static list I would go dictionary over a database. Since you have a single key dictionary will beat database. If it was composite key then you would have to go with a database for an indexed lookup.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • the Key is not Int32 it is `uniqueidentifier` is there a problem with it? – rpmlins Jun 12 '12 at 14:51
  • 1
    Not a problem at all. Then you just don't override gethash. Gethash was just a tweak and not essential. – paparazzo Jun 12 '12 at 15:03
  • 2
    @JasonCoyne What KeyedCollection does is use the Guid directly from the object and not repeat it as the key. But Dictionary will work in that scenario with the same speed. – paparazzo Jun 12 '12 at 15:04
  • can't thank you guys enought. I will replace the `List` to a `Dictionary` and do All the refactorying and as soon as i finish it, I get back here to tell the results. Sadly I can't vote up for asnwers and comments because of my reputation is too low, but as soon as it raises I will get back, its all I can do, and thank u guys once again. A lot! – rpmlins Jun 12 '12 at 15:17
  • @JasonCoyne I agree one of us should get credit for the answer. I like my answer but he used yours. Hey I gave you a +1 – paparazzo Jun 12 '12 at 16:05
  • answer accepted @Jason since you were first one to point to `Dictionary` you deserve it, by the way, just voted up for you guys too – rpmlins Jun 12 '12 at 18:03
  • gave you an upvote too blam to even out the karma somewhat :) – Jason Coyne Jun 12 '12 at 18:09