Click here to Skip to main content
15,879,474 members
Articles / Web Development / ASP.NET

C#. Static Cache and Multithreading

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
22 Sep 2017CPOL1 min read 14.3K   7   4
A few months ago, I had to optimize web portal (classified ad). The portal was built on ASP.NET 1.1. It seemed that portal had two main problems. Here is how I solved them.

A few months ago, I had to optimize web portal (classified ad). The portal was built on ASP.NET 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 no time and resource to redesign and recode the portal. So I needed something clear and powerful.

First, I tried classical methods. I tried to synchronize critical code blocks. But if you have a 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 led 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:

C#
static Hashtable Objects = new Hashatble();

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

C#
public sealed class CallObjects
{
   public enum Names
   {
      STAT_OBJ_1,
      STAT_OBJ_2,
      ......... 
   }

   public static object Get(Names name)
   { 
      return CallContext.GetData(name.ToString());
   }

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

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

C#
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 saving of money and time.

This article was originally posted at https://anton-burtsev.livejournal.com/2452.html

License

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


Written By
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.

Comments and Discussions

 
SuggestionRe: .NET 1.1 Pin
Alexander Batishchev1-Oct-17 5:44
Alexander Batishchev1-Oct-17 5:44 
GeneralRe: .NET 1.1 Pin
lemravec1-Oct-17 8:06
lemravec1-Oct-17 8:06 
SuggestionHave you looked into System.Runtime.Caching or System.Web.Caching? Pin
mldisibio26-Sep-17 5:16
mldisibio26-Sep-17 5:16 
GeneralInnovative Pin
John Brett24-Sep-17 21:48
John Brett24-Sep-17 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.