Michael Eilers Smith
Michael Eilers Smith

Reputation: 8618

How to use Data Caching in ASP .NET MVC 2.0

I want to share a Queue data structure between all the users of my web application, without using any database. I want this data to be always available and thread safe between all the users. Is caching a good idea? I tried using System.Web.Caching by calling:

Queue<int> users= new Queue<int>();

Context.Cache.Insert("users", users, null,  Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);

in my Global.axax file, but when I call the cache in my .cs files:

( (Queue<int>) Cache["users"] ).Enqueue( newUser);

I get the following error:

'System.Web.Caching.Cache' is a 'type' but is used like a 'variable'    

Am I using caching correctly?

Thanks!

Upvotes: 1

Views: 586

Answers (1)

tvanfosson
tvanfosson

Reputation: 532765

The Controller class doesn't have a Cache property like the ASP.NET web forms Page class. You need to reference it off the HttpContext property.

((Queue<int>)HttpContext.Cache["users"]).Enqueue( newUser );

Upvotes: 4

Related Questions