0

This is an asp.net-mvc application and I am trying to add an object, specifically a list of user objects to the cache. I feel like I'm missing a key concept or component when it comes to caching data, so any help would be greatly appreciated.

 var cache = new Cache();

 cache["Users"] = users;

The second line throw's "Object reference not set to an instance of an object. The users variable is not null, I'm certain I'm not creating or using the cache correctly, but cannot find any information in MSDN or SO regarding the right way to set up a cache. What's my mistake?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
LaRae White
  • 1,222
  • 3
  • 17
  • 35
  • 1
    Use `HttpRuntime.Cache` Check http://stackoverflow.com/questions/863654/difference-b-w-httpruntime-cache-vs-httpcontext-current-cache – Murali Murugesan Mar 26 '14 at 15:42
  • In case of an exception, always post the stack trace. – Steven Mar 26 '14 at 15:53
  • Thanks, I was missing a reference. – LaRae White Mar 26 '14 at 15:57
  • It's unfortunate that the .NET framework throws a `NullReferenceException` in this case, but the `Cache` class is not intended to be instantiated by the user. The reason it throws is because the internal `CacheInternal` hasn't been initialized, but the way to do this is by calling `SetCacheInternal` which is an internal method. This method is called by ASP.NET when creating a cache instance for the current request. – Steven Mar 26 '14 at 15:57

1 Answers1

2

try using Cache Like this it may works if you are using static methods

System.Web.HttpContext.Current.Cache
Dgan
  • 10,077
  • 1
  • 29
  • 51