views:

1046

answers:

5

At the moment I am working on a project admin application in C# 3.5 on ASP.net. In order to reduce hits to the database, I'm caching a lot of information using static variables. For example, a list of users is kept in memory in a static class. The class reads in all the information from the database on startup, and will update the database whenever changes are made, but it never needs to read from the datebase.

The class pings other webservers (if they exist) with updated information at the same time as a write to the database. The pinging mechanism is a Windows service to which the cache object registers using a random available port. It is used for other things as well.

The amount of data isn't all that great. At the moment I'm using it just to cache the users (password hashes, permissions, name, email etc.) It just saves a pile of calls being made to the database.

I was wondering if there are any pitfalls to this method and/or if there are better ways to cache the data?

A: 

Hmmm... The "classic" method would be the application cache, but provided you never update the static variables, or understand the locking issues if you do, and you understand that they can disappear at anytime with an appdomain restart then I don't really see the harm in using a static.

WaldenL
+8  A: 

A pitfall: A static field is scoped per app domain, and increased load will make the server generate more app domains in the pool. This is not necessarily a problem if you only read from the statics, but you will get duplicate data in memory, and you will get a hit every time an app domain is created or recycled.

Better to use the Cache object - it's intended for things like this.

Edit: Turns out I was wrong about AppDomains (as pointed out in comments) - more instances of the Application will be generated under load, but they will all run in the same AppDomain. (But you should still use the Cache object!)

JacquesB
Ah... thanks for making me feel foolish ;-) Didn't realise it even existed. Still, doesn't change my code too much. thanks.
Vincent McNabb
Do you know of any articles describing the "server generating more app domains in the pool"?
sledgebox
I'll second that question, whats this about more app domains being created? As far as I'm aware you will only get one domain per process for an application. You may have more processes upfront if you has specified a web garden. Each will have its own cache as well though.
AnthonyWJones
+1  A: 

As long as you can expect that the cache will never grow to a size greater than the amount of available memory, it's fine. Also, be sure that there will only be one instance of this application per database, or the caches in the different instances of the app could "fall out of sync."

Where I work, we have a homegrown O/RM, and we do something similar to what you're doing with certain tables which are not expected to grow or change much. So, what you're doing is not unprecedented, and in fact in our system, is tried and true.

sledgebox
+1  A: 

Another Pitfall you must consider is thread safety. All of your application requests are running in the same AppDomain but may come on different threads. Accessing a static variable must account for it being accessed from multiple threads. Probably a bit more overhead than you are looking for. Cache object is better for this purpose.

JaredPar
A: 

I suggest you look into ways of having a distributed cache for your app. You can take a look at NCache or indeXus.Net

The reason I suggested that is because you rolled your own ad-hoc way of updating information that you're caching. Static variables/references are fine but they don't update/refresh (so you'll have to handle aging on your own) and you seem to have a distributed setup.

cruizer
It costs money, this is currently just a hobby project. Not looking to make money from it.
Vincent McNabb
How do those rank against Microsofts Velocity?
WebDude
No idea, I haven't tried Velocity yet.
cruizer