André Casteliano
André Casteliano

Reputation: 644

Cache lookup performance

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.

Upvotes: 4

Views: 2744

Answers (4)

RocketsLee
RocketsLee

Reputation: 51

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)

Upvotes: 0

ggf31416
ggf31416

Reputation: 3647

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)

Upvotes: 0

Michael
Michael

Reputation: 55415

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.

Upvotes: 6

ChadT
ChadT

Reputation: 7693

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**)

Upvotes: 0

Related Questions