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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:09:30+00:00 2026-05-16T06:09:30+00:00

We have a data driven ASP.NET website which has been written using the standard

  • 0

We have a data driven ASP.NET website which has been written using the standard pattern for data caching (adapted here from MSDN):

public DataTable GetData()
{
   string key = "DataTable";
   object item = Cache[key] as DataTable;
   if((item == null)
   {
      item = GetDataFromSQL();
      Cache.Insert(key, item, null, DateTime.Now.AddSeconds(300), TimeSpan.Zero;
   }
   return (DataTable)item;
}

The trouble with this is that the call to GetDataFromSQL() is expensive and the use of the site is fairly high. So every five minutes, when the cache drops, the site becomes very ‘sticky’ while a lot of requests are waiting for the new data to be retrieved.

What we really want to happen is for the old data to remain current while new data is periodically reloaded in the background. (The fact that someone might therefore see data that is six minutes old isn’t a big issue – the data isn’t that time sensitive). This is something that I can write myself, but it would be useful to know if any alternative caching engines (I know names like Velocity, memcache) support this kind of scenario. Or am I missing some obvious trick with the standard ASP.NET data 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-16T06:09:30+00:00Added an answer on May 16, 2026 at 6:09 am

    You should be able to use the CacheItemUpdateCallback delegate which is the 6th parameter which is the 4th overload for Insert using ASP.NET Cache:

    Cache.Insert(key, value, dependancy, absoluteExpiration,
        slidingExpiration, onUpdateCallback);
    

    The following should work:

    Cache.Insert(key, item, null, DateTime.Now.AddSeconds(300),
        Cache.NoSlidingExpiration, itemUpdateCallback);
    
    private void itemUpdateCallback(string key, CacheItemUpdateReason reason,
        out object value, out CacheDependency dependency, out DateTime expiriation,
        out TimeSpan slidingExpiration)
    {
        // do your SQL call here and store it in 'value'
        expiriation = DateTime.Now.AddSeconds(300);
        value = FunctionToGetYourData();
    }
    

    From MSDN:

    When an object expires in the cache,
    ASP.NET calls the
    CacheItemUpdateCallback method with
    the key for the cache item and the
    reason you might want to update the
    item. The remaining parameters of this
    method are out parameters. You supply
    the new cached item and optional
    expiration and dependency values to
    use when refreshing the cached item.

    The update callback is not called if
    the cached item is explicitly removed
    by using a call to Remove().

    If you want the cached item to be
    removed from the cache, you must
    return null in the expensiveObject
    parameter. Otherwise, you return a
    reference to the new cached data by
    using the expensiveObject parameter.
    If you do not specify expiration or
    dependency values, the item will be
    removed from the cache only when
    memory is needed.

    If the callback method throws an
    exception, ASP.NET suppresses the
    exception and removes the cached
    value.

    I haven’t tested this so you might have to tinker with it a bit but it should give you the basic idea of what your trying to accomplish.

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

Sidebar

Related Questions

I have an ASP.NET MVC 3 / .NET Web Application, which is heavily data-driven,
I have data from travel diaries which has been read in from a csv
I am developing a data-driven web application using ASP.Net MVC 3. I will be
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
I'm just beinning basic data driven ASP.NET webforms design. I have the first part
This is an ASP.NET MVC website. Following domain driven design, we have a service
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
I'm working on developing my first Data Driven Domain using Dependency Injection in ASP.net.
I have an ASP.NET v2.0 website (not web application) where the root directory is
I've recently inherited a database driven e-commerce site written in C# ASP.Net, with an

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.