Scott Hanselman says yes.
Adding System.Web to your non-web project is a good way to get folks to panic. Another is adding a reference to Microsoft.VisualBasic in a C# application. Both are reasonable and darned useful things to do, though.
MSDN says no.
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.
So what should I think?
Cache is stored in web server memory.
The first is application caching, which allows you to cache data you generate, such as a DataSet or a custom report business object. The second is page output caching, which saves the output of page processing and reuses the output instead of re-processing the page when a user requests the page again.
Caching enables you to store data in memory for rapid access. When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. This can improve performance and scalability.
I realize this question is old, but in the interest of helping anyone who finds this via search, its worth noting that .net v4 includes a new general purpose cache for this type of scenario. It's in the System.Runtime.Caching namespace:
https://msdn.microsoft.com/en-us/library/dd997357(v=vs.110).aspx
The static reference to the default cache instance is: MemoryCache.Default
There shouldn't be any problem with using HttpRuntime.Cache. It's a sophisticated in-memory hash-table that can be very useful outside of the web context. Still, it might be a bit of a code-smell to reference HttpRuntime.Cache in a non-Http related application, so it can be a good idea to wrap it behind some ICache interface and use that wherever possible.
One thing to keep in mind is that Microsoft have a released the .NET Framework Client Profile Setup Package. This is a version of the 3.5 framework that is targeted at client applications and has a reduced footprint. The Client Profile does not include the ASP.NET pieces of the framework.
If your application depends on System.Web it will stop your application being able to take advantage of the Client Profile.
See Scott Gu's Blog for more details.
There doesn't seem to be anything in current versions of System.Web.Caching.Cache that depend on the HTTP runtime except for the Insert()
method that accepts a CacheItemUpdateCallback
, so Scott is right for the most part.
This doesn't prevent Microsoft from modifying the class in the future to be more integrated with the HTTP infrastructure.
I wrote a WeakReference-based lightweight cache in another answer.
Don't use it, even if it does work it can stop working in the next service pack/version.
The moment you do something based on internal implementation details and not the contract (in this case, MSDN) you can expect to get into trouble in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With