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 3501514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:52:07+00:00 2026-05-18T12:52:07+00:00

Ok I was a little unsure on how best name this problem :) But

  • 0

Ok I was a little unsure on how best name this problem 🙂 But assume this scenarion, you’re
going out and fetching some webpage (with various urls) and caching it locally. The cache part is pretty easy to solve even with multiple threads.

However, imagine that one thread starts fetching an url, and a couple of milliseconds later another want to get the same url. Is there any good pattern for making the seconds thread’s method wait on the first one to fetch the page , insert it into the cache and return it so you don’t have to do multiple requests. With little enough overhead that it’s worth doing even for requests that take about 300-700 ms? And without locking requests for other urls

Basically when requests for identical urls comes in tightly after each other I want the second request to “piggyback” the first request

I had some loose idea of having a dictionary where you insert an object with the key as url when you start fetching a page and lock on it. If there’s any matching the key already it get’s the object, locks on it and then tries to fetch the url for the actual cache.

I’m a little unsure of the particulars however to make it really thread-safe, using ConcurrentDictionary might be one part of it…

Is there any common pattern and solutions for scenarios like this?

Breakdown wrong behavior:

Thread 1: Checks the cache, it doesnt exists so starts fetching the url

Thread 2: Starts fetching the same url since it still doesn’t exist in Cache

Thread 1: finished and inserts into the cache, returns the page

Thread 2: Finishes and also inserts into cache (or discards it), returns the page

Breakdown correct behavior:

Thread 1: Checks the cache, it doesnt exists so starts fetching the url

Thread 2: Wants the same url, but sees it’s currently being fetched so waits on thread 1

Thread 1: finished and inserts into the cache, returns the page

Thread 2: Notices that thread 1 is finished and returns the page thread 1 it fetched

EDIT

Most solutions sofar seem to misunderstand the problem and only addressing the caching, as I said that isnt the problem, the problem is when doing an external web fetch to make the second fetch that is done before the first one has cached it to use the result from the first rather then doing a second

  • 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-18T12:52:08+00:00Added an answer on May 18, 2026 at 12:52 pm

    You could use a ConcurrentDictionary<K,V> and a variant of double-checked locking:

    public static string GetUrlContent(string url)
    {
        object value1 = _cache.GetOrAdd(url, new object());
    
        if (value1 == null)    // null check only required if content
            return null;       // could legitimately be a null string
    
        var urlContent = value1 as string;
        if (urlContent != null)
            return urlContent;    // got the content
    
        // value1 isn't a string which means that it's an object to lock against
        lock (value1)
        {
            object value2 = _cache[url];
    
            // at this point value2 will *either* be the url content
            // *or* the object that we already hold a lock against
            if (value2 != value1)
                return (string)value2;    // got the content
    
            urlContent = FetchContentFromTheWeb(url);    // todo
            _cache[url] = urlContent;
            return urlContent;
        }
    }
    
    private static readonly ConcurrentDictionary<string, object> _cache =
                                      new ConcurrentDictionary<string, object>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done some digging out here and am still a little unsure how
I'm a little unsure how to word the title to this question but I'm
I'm a little unsure about terminology here but is there some simple way to
I'm new to SQL and I'm a little unsure about the best way to
I'm a little unsure of how to ask this question, so I'll start as
A little help here to figure out what error to run this piece of
It's been a little while since I have done some hard out coding (since
I have been browsing this site for the answer but I'm still a little
Since this is just my second game, I'm still a little unsure of how
A little unsure of how to do this. I need to display a list

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.