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

  • Home
  • SEARCH
  • 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 6758283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:45:55+00:00 2026-05-26T13:45:55+00:00

I want create a cache so that when an item doesn’t exist, only one

  • 0

I want create a cache so that when an item doesn’t exist, only one person who requested that item spends the time generating it, and others that request it at the same time will simply block until the result is cached by the first person. Here is a description of that scenario:

  • Thread 1 comes in and requests the cached data for DateA
  • Thread 1 sees that it isn’t in the cache and starts the relatively long
    process of generating the information
  • Thread 2 comes in a sees that the information is currently being generated, waits on some kind of lock
  • Thread 3 also comes in and sees that the information is currently being generated, waits on some kind of lock
  • Thread 4 comes in and requests data for a different key that is already in the cache and doesn’t wait at all
  • Thread 1 finishes the generation and updates the cache with the value
  • Both thread 2 and 3 wake up and get the result that is now in the cache

I was thinking I would have the cache in a ConcurrentDictionary with a DateTime storing just a date as the key like ConcurrentDictionary<DateTime, CustomImmutableObject> _cache;

But I can’t see how Thread 2 and 3 would be able to wait on something. Even if there was another ConcurrentDictionary that stored some kind of status flag, how would they know when Thread 1 finishes?

Does anyone have any suggestions on how to approach developing such a cache?

  • 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-05-26T13:45:56+00:00Added an answer on May 26, 2026 at 1:45 pm

    You could use ConcurrentDictionary<DateTime, Lazy<CustomImmutableObject>> with getting the object as follows:

    myDictionary
        .GetOrAdd(
            someDateTime,
            dt => new Lazy<CustomImmutableObject>(
                () => CreateMyCustomImmutableObject()
            )
        ).Value
    

    The guarantee made by Lazy<T> in (the default) thread-safe mode will ensure that initialization occurs only once and subsequent accesses prior to the value actually being instantiated will block.

    In multithreaded scenarios, the first thread to access the Value property of a thread-safe Lazy(Of T) object initializes it for all subsequent accesses on all threads, and all threads share the same data. Therefore, it does not matter which thread initializes the object, and race conditions are benign.

    See here for the details.

    Below is a test I wrote to ensure that I’m not spouting. This should persuade you that all is good:

    void Main()
    {
        var now=DateTime.UtcNow;
        var d=new ConcurrentDictionary<DateTime, Lazy<CustomImmutableObject>>();
        Action f=()=>{
            var val=d
                .GetOrAdd(
                    now,
                    dt => new Lazy<CustomImmutableObject>(
                        () => new CustomImmutableObject()
                    )
                ).Value;
            Console.WriteLine(val);
        };
        for(int i=0;i<10;++i)
        {
            (new Thread(()=>f())).Start();
        }
        Thread.Sleep(15000);
        Console.WriteLine("Finished");
    }
    
    class CustomImmutableObject
    {
        public CustomImmutableObject()
        {
            Console.WriteLine("CREATING");
            Thread.Sleep(10000);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to cache an dto, that I get consuming an api, in the
In my application, I want to cache the displayed data in tableview so that
I want to create a temporary cache lookup to speed up a file lookup
I want to create lightweight interfaces with methods that I can plug into classes.
I want to create an iPad application that is pretty much just a catalog
I want to create a singleton that remains alive for the life of the
I want to create a web application with a model that persists between HTTP
I want to create a view that shows vast amounts of data. Using UITableView

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.