Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store a simple table in memory in ASP.NET?

Is there a simple way to store a simple table in memory on the server in ASP.NET that all users would share? Let's say I have a simple chat program for instance and I only ever want to hold the last 100 records in memory (this isn't a real application, just an example). Say I didn't want to have an entire table in SQL dedicated to being this chat buffer with only 100 records. Is there some way I could create an in-memory data table and share it among connected users?

like image 993
Chev Avatar asked Oct 24 '12 08:10

Chev


People also ask

What is Memorycache C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.

What is cache in asp net with example?

ASP.NET provides caching of web pages through Page Output Caching. A page is enabled for caching using the @OutputCache directive. This directive is written at the top of the page which is to be cached. The code below shows the code in the hold to cache a web page for 60 seconds.

What is cache in ASP.NET Core?

ASP.NET Core supports several different caches. The simplest cache is based on the IMemoryCache. IMemoryCache represents a cache stored in the memory of the web server. Apps running on a server farm (multiple servers) should ensure sessions are sticky when using the in-memory cache.


1 Answers

Use Cache (MSDN)

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

DataTable yourDataTable = new DataTable();
Cache["yourTable"] = yourDataTable;

//to access it
DataTable dt = Cache["yourTable"] as DataTable;
like image 123
Habib Avatar answered Oct 24 '22 17:10

Habib