views:

37

answers:

2

I'm working in an environment where a single web application (running in a web-farm) dynamically connects to different SQL Server databases (possibly on different servers) based on user selection (this part is non-negotiable, unless you can gift me $1,000,000 and/or some kind of time altering device).

I'd like to cache some of the commonly used data from the database(s) in the application, such as "settings", and am looking for a best practice to use with the SqlCacheDependency class or a related database cache dependent solution (not opposed to a custom implementation of SQL notification or polling; notification is preferred over polling).

So far, I'm thinking of using a collection of SqlCacheDependency objects (one for each database) that can be referenced by connection string name or related key. So when the connection is changed to a different database, the cache can be requested/populated by a key of <databasekey>+<datakey> and use the appropriate SqlCacheDependency object (probably handled by a cache-managing wrapper).

Any ideas, suggestions, or experience in this area?

Or is it just better to take the hit and always go to the database?

A: 

you can use nhibernate or hibernate with 2nd level cache in your data access code. for caching part you can use memcached or velocity (appfabric).

sirmak
Can you provide some more context?
ulty4life
if you're using .net you can use nhibernate, for java you can use hibernate. these orm tools allow level2 caching that can auto cache recent fetched records for you. their pluggable architecture allows to use your favorite caching tool like memcached.
sirmak
Not really sure how this helps with the multi-database problem. As far as I can tell, I'd still have to come up with a solution to manually manage the content for each database.
ulty4life
A: 

In my opinion, you should not store all settings from different databases in your web application. The better way is to store only settings of the database that's currently in use.

You told that, at some point, user selects the database and you creating connection, opening it, reading the data and so on. This is a good place to read your settings only for this database and use them in the application. At the same moment, you can adjust SqlCacheDependency to be notified when settings in your current database has changed.

When user selects another database, you just drop current settings and load another ones from newly selected database.

This is much easier to implement, in my opinion.

Andrey Tagaew
If there are N number of users simultaneously using the site, each connecting to a potentially different database, whenever a different database is accessed, it would overwrite the cached settings for all the other users. Yes?
ulty4life
You did not told that all users are using different databases.In your case, your solution is correct, but complex.You may need a singletone class that will be used by other classes to get current site settings.That class should contain collection of current settings.The settings should be loaded when this singletone class is first initiated.Also, that class should launch separate thread that will updates settings and then go to sleep for some amount of time.This solution is much flexible then using SqlCacheDependency object, in my oppinion
Andrey Tagaew
So you're suggesting a polling solution? The benefit of the SqlCacheDepenency class is that whenever data in the database changes SqlServer notifies the application, so it only has to fetch data when the data actually changes and it knows immediately when the cache becomes out of sync and needs to be refreshed. Also, how would you keep a separate thread permanently active in a web application pool?
ulty4life
Actually its really up to you.My way and the way that SqlCachDependency does are the same. They all should poll the database from time to time to check if settings were changed. The only difference is that SqlCacheDependency creates additional datatable and few procedures. This means, that you should always has that table in all your databases.As for me, i prefer to write such things by myself to be sure that its working in the way i want.But, seems its just a bad habit. I've just read more about this component and may tell that its looking nice to use for you.
Andrey Tagaew