Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does c# have a collection that works like a cache?

Tags:

c#

caching

Looking for a dictionary or similar that automatically removes entries after a specific amount of time, either if they're not used for X amount of time, or simply removed after X amount of time regardless of use.

Before I roll my own I was wondering if C# already has this capability in one of its collections. I haven't seen anything on google but I wanted to check.

like image 390
NibblyPig Avatar asked Jan 08 '23 14:01

NibblyPig


2 Answers

You can find MemoryCache from System.Runtime.Caching. Although it isn't generic (if you're using value types, this may be something to consider), it has the TTL mechanism you're looking for.

like image 161
Yuval Itzchakov Avatar answered Jan 10 '23 03:01

Yuval Itzchakov


System.Runtime.Caching.MemoryCache - for .NET Framework applications.

System.Web.Caching.Cache - for ASP.NET applications.

like image 24
ivamax9 Avatar answered Jan 10 '23 04:01

ivamax9