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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:07:00+00:00 2026-05-25T18:07:00+00:00

In my DataCache I need to cache an object using two indexes. Say I

  • 0

In my DataCache I need to cache an object using two indexes.

Say I cache like this:

Campaign campaign = //get campaign from db

HttpContext.Current.Cache.Add(
"Campaigns.Id."+campaign.Id.ToString(), 
campaign,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);

HttpContext.Current.Cache.Insert("Campaigns.Code."+campaign.Code,
campaign,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);

I try to access the cache using either Id or Code “index”. If not found, the campaign is retrieved and indexed as shown above.

Can this method lead to any problems?

ASP can decide to remove just one of the indexes. If I access the cache through that index it will fetch the item and reindex both again and that’s ok.

UPDATE:

My main question is whether I have to pay for storing the object two times or if it a just a reference to the same object that is stored in 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-25T18:07:00+00:00Added an answer on May 25, 2026 at 6:07 pm

    You can make sure that both entries are removed together by using a CacheDependency object. Here is the updated insert statement. That makes the expiration times no longer necessary.

    HttpContext.Current.Cache.Insert(
      "Campaigns.Code." + campaign.Code, 
      campaign, 
      new CacheDependency(null, new [] {"Campaigns.Id."+campaign.Id.ToString()}));
    

    But in practice both variants are fine.

    Edit: You should probably make the insertion of the second entry dependent on the success of adding the first entry. Consider a scenario where multiple requests ask for an object not in the cache. A typical race. All of them create the data (fine), one of them can successful call Add(...) (fine), but all of them can successful call Insert(...)(possibly bad). You might end up with different objects returned for your two indices.

    I propose the following changes to your code:

    Campaign campaign = //get campaign from db
    string id = "Campaigns.Id." + campaign.Id.ToString();
    object old = HttpContext.Current.Cache.Add(
        id, campaign, null,
        System.Web.Caching.Cache.NoAbsoluteExpiration,
        System.Web.Caching.Cache.NoSlidingExpiration,
        System.Web.Caching.CacheItemPriority.Normal,
        null);
    if (old == null) {
        // the object was successfully added
        HttpContext.Current.Cache.Insert(
            "Campaigns.Code." + campaign.Code, 
            campaign, 
            new CacheDependency(null, new [] { id }));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a named cache and simply want to return (programmatically) its object count.
I am using the Yii framework for a site that will get a lot
I am using Local data Cache (.sdf) in my application. I want to store
Does loop unrolling effect data cache performance in any way? This is related to
I'm using Core Data + sqlite as a data cache. The app reads in
We have a data driven ASP.NET website which has been written using the standard
In SSRS, I noticed that the rdl.data cache files are being stored on my
I'm using MVVM Light to build a WP7 (Windows Phone 7) application. I wish
With the Zend Data Cache - PHP API , is there a method for
I have a web app and I need print the data into pdf. In

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.