Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in C#/.Net

Tags:

c#

.net

caching

I wanted to ask you what is the best approach to implement a cache in C#? Is there a possibility by using given .NET classes or something like that? Perhaps something like a dictionary that will remove some entries, if it gets too large, but where whose entries won't be removed by the garbage collector?

like image 422
Sebastian Müller Avatar asked Aug 14 '09 07:08

Sebastian Müller


People also ask

What is cache in C?

Cache memory is an extremely fast memory type that acts as a buffer between RAM and the CPU. It holds frequently requested data and instructions so that they are immediately available to the CPU when needed. Cache memory is used to reduce the average time to access data from the Main memory.

What caching means?

Caching (pronounced “cashing”) is the process of storing data in a cache. A cache is a temporary storage area. For example, the files you automatically request by looking at a Web page are stored on your hard disk in a cache subdirectory under the directory for your browser.

What is caching and how it works?

Caching is the process of storing copies of files in a cache, or temporary storage location, so that they can be accessed more quickly. Technically, a cache is any temporary storage location for copies of files or data, but the term is often used in reference to Internet technologies.


2 Answers

If you are using .NET 4 or superior, you can use MemoryCache class.

like image 185
Muaddib Avatar answered Sep 22 '22 06:09

Muaddib


If you're using ASP.NET, you could use the Cache class (System.Web.Caching).

Here is a good helper class: c-cache-helper-class

If you mean caching in a windows form app, it depends on what you're trying to do, and where you're trying to cache the data.

We've implemented a cache behind a Webservice for certain methods
(using the System.Web.Caching object.).

However, you might also want to look at the Caching Application Block. (See here) that is part of the Enterprise Library for .NET Framework 2.0.

like image 26
Bravax Avatar answered Sep 18 '22 06:09

Bravax