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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:01:53+00:00 2026-05-18T02:01:53+00:00

Suppose I have two threads(Thread1, Thread2) where the threads are accessing the cache for

  • 0

Suppose I have two threads(Thread1, Thread2) where the threads are accessing the cache for a given object such as in the code below nearly at the same time:

    Dim expensiveToGetData = Cache("ExpensiveDataKey")

    If ExpensiveToGetData is nothing then
'because the cache has expired

ExpensiveToGetData = LoadExpensiveDataFromDataSource()
       Cache("ExpensiveDataKey") = ExpensiveToGetData
    end If

    ProcessExpensiveData(ExpensiveToGetData)

Isn’t it possible for both threads to load the cache because they both requested data from the cache that was nothing/expired? I’ve run some tests on a local machine and it seems that the cache is being loaded more than once. Is this a normal pattern?

  • 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-18T02:01:53+00:00Added an answer on May 18, 2026 at 2:01 am

    Yes, using that code it is certainly possible that two different request will get Nothing from Cache, and therefore both reload data. If you want to avoid this, you need to synchronize the whole operation of getting the data.

    One way to do synchronize access, would be to use code similar to:

    Dim expensiveToGetData = Cache("ExpensiveDataKey")
    
    If ExpensiveToGetData is nothing then
        SyncLock yourLockObject /* YourLockObject should be a Shared object. */
            expensiveToGetData = Cache("ExpensiveDataKey")
            If expensiveToGetData Is Nothing Then
                ExpensiveToGetData = LoadExpensiveDataFromDataSource()
                Cache("ExpensiveDataKey") = ExpensiveToGetData
            End If
        End SyncLock
    end If
    
    ProcessExpensiveData(ExpensiveToGetData)
    

    The idea of checking whether we got the data before acquiring the lock, is to avoid excessive locking in an environment under high load. If it is not there, we need to check again once inside the lock, because another thread might have gotten the data while we were acquiring the lock.

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

Sidebar

Related Questions

Suppose I have two collections as follows: Collection1: A1 A1 M1 M2 Collection2: M2
I have few queries related to read()/pread() system calls in a multithreaded environment I
I have a Thread -extending class that is supposed to run only one instance
I am writing a chess game which allows two programs compete, the player needs
I have an ACE reactor that accepts socket connections and listens for the incoming
I have a web application that loads some 50 tables from SQL Server into
I'm doing a path-finding project as part of my 4th year software engineering degree.
I am working on my first mutlithreaded program and got stuck about a couple
Okay this is about my fifth thread on the topic because I'm balls-out lost
We needed a location service for our app that was available from anyplace. We

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.