views:

328

answers:

4

We have a big winforms C# application, that's basically a frontend for some databases (CRUD stuff) and I'm trying to implement some in memory cache for business objects.

Something like:

List<Customer> customerCache; // Loaded during app. startup

I've already created some code to keep the cache up-to-date with the database. This code run on a separate thread all the time, and is working really well. My problem is that depending on the size of the cache, it's faster to do a 'select * from customers where id = x' in the database than looping through the cache with a foreach (foreach Customer cmr in customerCache) to find that specific object...

Is there a way to search for specific objects in my cache really fast ? I was going to try some algorithm or changing the type of my collection, but I would appreciate listening to your suggestions.

Please note that we have several 'List xxxCache' and everything is fast (for small N, off course). But when the number of cached itens grow (> 3000 normally) its faster to read from the database.

What's the best way to loop through my cached items to find a specific one ? All business items inherit from a common ancestor and have an 'ID' property(integer, unique).

Sorry for my bad english, it's not my primary language. Best regards, Greetings from Brazil.

+6  A: 

Use Dictionary<int, Customer> instead. It supports O(1) lookup based on a key. In this case, key would be Customer.Id.

You might also want to look into other pre-built database caching solutions for .Net.

Michael
A: 

Insteaf of using a List<T> object, why not use a :

KeyValuePair

Dictionary is the correct object to use (KeyValuePair is what a dictionary holds a collection of **facepalm**)

DaRKoN_
A: 

use as many dictionaries as the number of indexes you need.

dictionary<int,Customer> CustomerIds //(Ids)  
dictionary<string,Customer> CustomerNames //(Names)  
//or  
dictionary<string,List<Customer>> //(if name is not unique)
ggf31416
A: 

We have a similar case for the web form application. We use MS Enterprise Lib Cache block. It is easy to implement and use. The only thing you need to focus in Cache Key (string type) cache.add(key, object) cache.getdata(key)

RocketsLee