R – Should we not use the System.Web.Caching.Cache class in a WinForms app

cachingnetwinforms

I've seen several articles/blog posts that advocate the use of the System.Web.Caching.Cache in applications that are not for the Web, however, the MSDN documentation quite clearly states that

The Cache class is not intended for use outside of ASP.NET applications. It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.

Is the documentation wrong, or should we really not be using this class? If the latter, what could go wrong? Our platform is Winforms/.NET 3.5.

Best Answer

Expired or unused Cache objects cannot be automatically removed if the application is not an ASP.NET application. Therefore, if Cache objects are used outside ASP.NET, the system memory that is reserved for the Cache objects is not released. You cannot manually invoke the internal ASP.NET methods to clean up the cache because the methods are marked as internal and intended for internal use. So its your decision to use it in an outside application at your own risk. System.Web.Caching.Cache object

Scott Hanselman has a article post on it for more details