Click here to Skip to main content
13,861,257 members
Click here to Skip to main content
Add your own
alternative version

Stats

9.7K views
7 bookmarked
Posted 22 Sep 2017
Licenced CPOL

C#. Static Cache and Multithreading

, 22 Sep 2017
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 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:

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:

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:

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.

License

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

Share

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

 
SuggestionRe: .NET 1.1 Pin
Alexander Batishchev1-Oct-17 6:44
memberAlexander Batishchev1-Oct-17 6:44 
GeneralRe: .NET 1.1 Pin
lemravec1-Oct-17 9:06
memberlemravec1-Oct-17 9:06 
SuggestionHave you looked into System.Runtime.Caching or System.Web.Caching? Pin
mldisibio26-Sep-17 6:16
membermldisibio26-Sep-17 6:16 
GeneralInnovative Pin
John Brett24-Sep-17 22:48
memberJohn Brett24-Sep-17 22: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 22 Sep 2017
Article Copyright 2017 by Anton Burtsev
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid