Click here to Skip to main content
13,050,242 members (99,365 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 17 Jun 2009

C#. Static cache and multithreading.

, 17 Jun 2009
Rate this:
Please Sign up or sign in to vote.
A few months ago I had to optimize web portal (classified ad). The portal was built on 1.1.

A few months ago I had to optimize web portal (classified ad). The portal was built on 1.1. It seemed that portal had 2 main problems: It failed when it was more than 1 user (Collection was modified, etc.) It returned different data for the same page when I setup more than 1 IIS worker process

The reason is: static cache and other static objects. As far as static objects are the same for all threads and different for every process, they are the reason for both troubles. They conflicted in multithread operations and prevented querying SQL Server “thinking” that data has already been read.

There was not time and resource to redesign and recode the portal. So I needed something clear and powerful.

First, I tried classical methods. I Try to synchronize critical code blocks. But if you have static collection of collections this is not really possible. All nested collections are candidates to fail in any part of code.

Next I tried not to enumerate original lists. I always create copy before enumeration to prevent its modification before enumeration finishes. However it leaded to a set of new troubles and I had to get everything back.

And... a fresh idea entered my head. Now it seems quite obvious, but it took a long period to appear...

Look, If we have static cache in the following form

static Hashtable Objects = new Hashatble();

We do not need to synchronize it. It is enough to make for each thread it’s own cache. And we easily can do it with CallContext. First, Create a thred-safe “static” object store:

public sealed class CallObjects 
   public enum Names 
   public static object Get(Names name) 
      return CallContext.GetData(name.ToString()); 

   public static void Set(Names name, object obj) 
      if ( obj == null ) 
         CallContext.SetData(name.ToString(), obj); 

Now we can use static property that returns thread-dependent Hashtable

Hashtable Objects 
   get { 
      Hashtable objects = 
         (Hashtable) CallObjects.Get(CallObjects.Names.STAT_OBJ_1); 
      If ( objects == null ) { 
         objects = new Hashtable(); 
         CallObjects.Set(CallObjects.Names.STAT_OBJ_1, objects); 
      return objects;

This simple modification saved life to the project. And that was great save of money and time. Bye.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anton Burtsev
Web Developer
Russian Federation Russian Federation
I have started as a C++ developer in far 2000. I'd been developing banking and treasury software for 3 years using C++. In 2003 I switched to .NET at DELL. After that I worked as a project manager on different projects (internal audit, treasury automation, publishing house automation, etc.). Since 2009 I own a small software company specialized in SaaS services and develop a DotNetNuke modules.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Jun 2009
Article Copyright 2009 by Anton Burtsev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid