Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Safety and Scope Management for .NET 4.0 ObjectCache

I'm using the new .NET 4.0 Caching API, ObjectCache. I've asked a few questions on this area the last few days, and i've hinted to this issue - but thought it's worthwhile to break it out into it's own question.

Because the class is abstract and all the methods are virtual, this means we can create our own custom cache providers.

According to MSDN, ObjectCache does not have to be a singleton, and you can create multiple instances of it in your application.

But to me, this sounds like we need to manage the instantiation and lifetime of this object as well?

I have an ASP.NET MVC 3 Web Application, with StructureMap as my dependency injection container.

I want to have a single, shared cache for my entire web application.

So, i create a very simple class which wraps the ObjectCache class, and provides the unboxing in the methods implementation.

The class takes an instance of ObjectCache in the ctor, and sets this to a private static instance of the cache, which the methods (Add, Get, etc) work off.

E.g

public class CacheManager
{
   private static ObjectCache _cache;

   public CacheManager(ObjectCache cache)
   {
      _cache = cache;
   }

   // Add, Get, Remove methods work off _cache instance.
}

Now, here's my DI registry:

For<CacheManager>().Singleton().Use<CacheManager>().Ctor<ObjectCache>("cache").Is(MemoryCache.Default);

In english: When something requests a CacheManager instance, use a singleton instance, and set the ObjectCache parameter to be a MemoryCache instance.

So there's what i have, now the questions:

  1. If i have a class to wrap the ObjectCache, does this class need to be a singleton?
  2. MSDN says ObjectCache is thread-safe, but now that i'm using a singleton, do i need any type of locking to keep the thread safety?
  3. Does the private instance of ObjectCache in my wrapper class need to be static? Does the class itself need to be static?
  4. General thoughts on my overall implementation?

I have not been able to find a decent blog/article on .NET ObjectCache in ASP.NET Web Applications, hence my confusion.

I'm use to using HttpContext.Current.Cache (which is static) and not care about lifetime management for the cache.

like image 514
RPM1984 Avatar asked Feb 09 '11 22:02

RPM1984


1 Answers

  1. Since MemoryCache.Default is a singleton, your stateless class doesn't really need to be one. However, that's completely up to you.
  2. You should not need locking around the ObjectCache instance.
  3. No, and No. Making it static doesn't provide any value. Indicating it's a singleton in StructureMap makes GetInstance<>() always return the same object anyways.
  4. The real value of wrapping ObjectCache would to be abstract the cache implementation so you can change it or mock it. Without an interface this becomes less useful.

An example implementation below...

public interface ICacheManager
{
   // add, get, remove, etc
}

public class CacheManager : ICacheManager
{
   private static ObjectCache _cache;

   public CacheManager(ObjectCache cache)
   {
      _cache = cache;
   }

   // Add, Get, Remove methods work off _cache instance.
}

Then...

For<ICacheManager>()
    .Singleton()
    .Use<CacheManager>();

For<ObjectCache>()
    .Use(MemoryCache.Default);

If you want to change you cache provider that is still an ObjectCache in the future, then it's easy to adjust.

I hope this helps!

like image 151
Travis Avatar answered Sep 18 '22 23:09

Travis