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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:12:43+00:00 2026-06-08T18:12:43+00:00

I have read everywhere that the Add method fails if it already exists but

  • 0

I have read everywhere that the Add method fails if it already exists but does it throw an exception or does it fail silently?

I am writing a multithreaded web application where it should not exist already and it will cause problems if I overwrite the cache, so I can’t use the Insert method.

Would this be something I could do:

try
{
    HttpContext.Current.Cache.Add("notifications", notifications, null,
      System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(8),
      System.Web.Caching.CacheItemPriority.High, null);
}
catch
{
    //do whatever if notifications already exist
}

Thanks for any answers 🙂

  • 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-08T18:12:44+00:00Added an answer on June 8, 2026 at 6:12 pm

    System.Web.Caching.Cache is designed to be thread-safe in a multithreaded web application, and multiple threads may be in contention to add the same key to the cache. So it depends on how you want to handle such race conditions.

    • In many cases, you will be inserting immutable data into the cache and won’t care which thread ‘wins’ the race. So you can use Add or Insert.

    • If you want “first one wins”, use the Add method, if you want “last one wins (and overwrites)” use the Insert method.

    • There is no point in checking for existence before inserting/adding. Another thread may insert the item after your check and before you attempt to add/insert.

    • Neither Add nor Insert with throw an exception if the key already exists. It wouldn’t make sense to do so as the Cache is designed for thread-safe insertion without locking. Add will fail silently, and Insert wil overwrite.

    • Incidentally, when reading from the Cache, don’t check for existence then read:

      if (Cache["MyKey"] == null)
      {
          // ... handle missing value
      }
      else
      {
          // ... a race condition means the item may have been removed here
          // before you get a chance to read it
      
          MyType value = (MyType) Cache["MyKey"];
      }
      

      Instead, read the value from the cache and check for null:

      MyType value = Cache["MyKey"] as MyType; // for reference types
      if (value == null)
      {
          // ... handle missing value
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read everywhere that rewriting history of commits that have been published is bad
I have read so many times, here and everywhere on the net, that mutexes
I have read everywhere that a reference has to be initialized then and there
I have read everywhere that unsigned Java applets are not allowed to make network
I have searched everywhere but can't find a solution that works for me. I
I have read few articles about table partioning but still I am bit confused
I have read some of the questions and answers here, but it none match
I have read a bunch of forums on this but none have solved my
I am very new to Android programming, and I have read everywhere and I
Im getting really lost on how to use HttpContext.User. I read everywhere that its

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.