Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use HttpRuntime.Cache outside ASP.NET applications?

Tags:

.net

caching

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?

like image 525
Jakub Šturc Avatar asked Oct 01 '08 17:10

Jakub Šturc


People also ask

Where is Httpruntime cache stored?

Cache is stored in web server memory.

What is Application cache in asp net?

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.

What is .NET cache?

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.


5 Answers

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

like image 165
Joel Fillmore Avatar answered Oct 26 '22 17:10

Joel Fillmore


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.

like image 5
Doron Yaacoby Avatar answered Oct 26 '22 18:10

Doron Yaacoby


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.

like image 4
Brownie Avatar answered Oct 26 '22 19:10

Brownie


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.

like image 2
Mark Cidade Avatar answered Oct 26 '22 19:10

Mark Cidade


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.

like image 1
Nir Avatar answered Oct 26 '22 18:10

Nir