3

I have a list of product that I have stored in asp.net cache but I have a problem in refreshing the cache. As per our requirement I want to refresh cache every 15 minutes but I want to know that if in the mean time when the cache is being refreshed if some user ask for the list of product then should he get error or the old list or he have to wait until the cache is refreshed.

the sample code is below

public class Product
{
    public int Id{get;set;}
    public string Name{get;set;}
}

we have a function which gives us list of Product in BLL

public List<Product> Products()
{
       //////some code
}

Cache.Insert("Products", Products(), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);

I want to add one more situation here, Let say I use static object instead of cache object then what will happen and which approach is best if we are on a stand alone server and not on cluster

4
  • 2
    Either way it depends on code that will retrieve your data. Cache will simply return null after 15 minutes for the key "Products". Static object won't survive the app pool recycle, so in that case it will also depend on code that will retrieve the data from static object. Commented Jun 24, 2013 at 11:31
  • Also you need to use a thread safe collection in both cases. See the System.Collections.Concurrent Namespace Commented Jun 24, 2013 at 11:33
  • can you give me link to example where this thing is implemented Commented Jun 24, 2013 at 11:35
  • Maybe this can help ? Commented Jun 24, 2013 at 13:18

1 Answer 1

3

Sorry - this might be naive/obvious but just have a facade type class which does

if(Cache["Products"] == null)
{
        Cache.Insert("Products", Products(), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
 }
 return Cache["Products"];

There is also a CacheItemRemoveCallback delegate which you could use to repopulate an expired cache. As an alternative

ALSO

use the cache object rather than static objects. More efficient apparently (Asp.net - Caching vs Static Variable for storing a Dictionary) and you get all your cache management methods (sliding expiration and so on)

EDIT

If there is a concern about update times then consider two cache objects plus a controller e.g.

  1. Active Cache
  2. Backup Cache - this is the one that will be updated
  3. Cache controller (another cache object?) this will indicate which object is active

So the process to update will be

  1. Update backup cache
  2. Completes. Check is valid
  3. Backup becomes active and visa versa. The control now flags the Backup cache as being active

There needs to be a method which will fire when the products cache object is populated. I would probably use the CacheItemRemoveCallback delegate to initiate the cache repopulation. Or do an async call in the facade type class - you wouldn't want it blocking the current thread

I'm sure there are many other variants of this

EDIT 2

Actually thinking about this I would make the controller class something like this

public class CacheController
{
    public StateEnum Cache1State {get;set;}
    public StateEnum Cache1State {get;set;}
    public bool IsUpdating {get;set;}
}

The state would be active, backup, updating and perhaps inactive and error. You would set the IsUpdating flag when the update is occurring and then back to false once complete to stop multiple threads trying to update at once - i.e. a race condition. The class is just a general principle and could/should be amended as required

Sign up to request clarification or add additional context in comments.

3 Comments

what about the time in which the cache is being refreshed.
A race condition means that it is possible (though probably very unlikely) that your cached item might be evicted between "Cache.Insert(key,...)" and "return Cache[key]". Use a local variable to guarantee you don't return null.
thanks for that - would that be true using the callback delegate or using the backup cache method? Is that just a problem in the first (only?!?) code sample?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.