7

Is the ObjectCache class included in .Net Framework 3.5?

If not, is there an alternative for storing objects in cache, ( in windows applications not asp.net)?

And does anyone have any online resources or code snippets that demonstrate how to use the ObjectCache class?

sschrass
  • 7,014
  • 6
  • 43
  • 62
Hassan Mokdad
  • 5,832
  • 18
  • 55
  • 90

2 Answers2

3

Is the ObjectCache class included in .net framework 3.5

No.

You could still use the ASP.NET cache in a WinForms application if you reference the System.Web assembly:

HttpRuntime.Cache["key"] = "value";

and if you wanted to define more granular properties:

HttpRuntime.Cache.Add(
    "key",
    "value", 
    null,
    DateTime.MaxValue,
    TimeSpan.FromSeconds(3),
    CacheItemPriority.High,
    new CacheItemRemovedCallback(OnItemRemove)
);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Won't using the ASP.net cache in WinForms application affect performance in any way? thanks – Hassan Mokdad Jul 20 '11 at 06:58
  • @Hassan Mokdad, I don't know what is your scenario nor what objects you are trying to cache. The more objects you cache the more memory will be consumed. But this cache is pretty optimized so there shouldn't be any problems. – Darin Dimitrov Jul 20 '11 at 07:01
  • 2
    The Cache class is not intended for use outside of ASP.NET applications. see http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx – nakhli Jul 20 '11 at 08:14
2

ObjectCache is introduced in .NET framework 4. If you are using a prior framework, you can use Microsoft Entreprise Library caching.

I do not recommend you using HttpRuntime.Cache as it is not intended to be used outside of asp.net applications.

nakhli
  • 4,009
  • 5
  • 38
  • 61
  • 1
    Just to add, you can also use AppFabric (enterprise level caching) or you can create your own cache system too, but why re-invent the wheel, right? ;-) – Ahmed ilyas Oct 01 '15 at 12:58