Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:20:08+00:00 2026-06-04T08:20:08+00:00

I am trying to implement a generic thread-safe Cache method, and I wonder how

  • 0

I am trying to implement a generic thread-safe Cache method, and I wonder how I should implement the lock in it.

It should look something like this:

//private static readonly lockObject = new Object();

public T GetCache<T>(string key, Func<T> valueFactory...)
{

  // try to pull from cache here

  lock (lockObject) // I don't want to use static object lock here because then every time a lock is performed, all cached objects in my site have to wait, regarding of the cache key.
  {
    // cache was empty before we got the lock, check again inside the lock

    // cache is still empty, so retreive the value here

    // store the value in the cache here
  }

  // return the cached value here

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:20:09+00:00Added an answer on June 4, 2026 at 8:20 am

    For non shared data among pools

    When you have many pools (web garden) each pool can have their static data. There I have measure this days that the ConcurrentDictionary<TKey, TItem> is the faster because they have implement some kind of technique that don’t use look inside, so they have make it extreme fast.

    So I suggest the ConcurrentDictionary<TKey, TItem> for non shared data among pools.

    In this case you must take care the synchronization of the data him self to avoid concurrent data change on the same the data. There you can use the SlimLock, or a Lock.

    common resources change among pools

    Now, when you have resource that are shared among pools, you need to use mutex. For example if you try to go to save a file from many threads, of open a file for change it from many threads – you need mutex to synchronize that common resource

    So for common resource you use the mutex
    Mutex can use a Key to lock to lock base on that key – but you can not change the same resource!.

    public T GetCache<T>(string key, Func<T> valueFactory...) 
    {
        // note here that I use the key as the name of the mutex
        // also here you need to check that the key have no invalid charater
        //   to used as mutex name.
        var mut = new Mutex(true, key);
    
        try
        {   
            // Wait until it is safe to enter.
            mut.WaitOne();
    
            // here you create your cache
        }
        finally
        {
            // Release the Mutex.
            mut.ReleaseMutex();
        }   
    }
    

    What kind of lock

    we have two case for lock.

    1. One case is when we use common resources in all pools, all threads. Common resource can be a file, or the database its self.

    In the common resources we need to use mutex.

    1. Second case is when we use variables that are visible only to the inside of a pool – different pools can not see that resources. For example a static List<>, a static Dictionary etc. This static variables, arrays can access only inside the pool and they are not the same across different pools.

    In this second case, the lock() is the most easy and common way to use.

    Faster than lock

    Now, when we have a static dictionary that we keep for long time and make too many reads/writes there, a faster approach to avoid the full program to wait, is the ReaderWriterLockSlim

    you can take a full example from here: ReaderWriterLockSlim

    Using the ReaderWriterLockSlim, we can avoid the locks when we do not need them – and we do not need to lock the static values when we read – only when we write on them. So I can suggest it for static values that we use them as cache.

    What is a pool in asp.net.

    Imaging as if different programs that run isolate each other but serves the incoming requests from users. Each pool have his own world and they are not communicate each other. Each pool have their initialize, their static values, and their life. To have some common resource between pools you need some other third program, like a database, like a file on disk, like a service.

    So if you have many pools (web garden) to synchronize them for common resource you need mutex. To synchronize them inside you use lock.

    IIS app pools, worker processes, app domains
    Lifetime of ASP.NET Static Variable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the repository pattern using a generic repository like the one
i am trying to implement a generic method which allows to output csv file
I'm trying to build an object that looks something like this: public class MyObject
I'm trying to implement a Generic Repository. This is what I've got so far
After coming up against this problem myself in trying to implement a generic Vector2<int/float/double>
I have a generic repository and would like to implement a generic GetById method.
I'm trying to implement the generic method which is intended for converting objects of
I'm trying to implement generic method to put in a class a calculated value
I'm trying to implement a linked collection using generics, something like the following. public
I'm trying to implement a generic way of adding remote validation to xVal /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.