Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a key exists in a MemoryCache

Tags:

c#

.net

caching

I need to check if a key is present in a MemoryCache. However there is no Keys collection or something similar available.

I do not need the object associated with the key only a true or false if a certain key is present.

I know I can try a .Get(key) but I do not need the object. Is this the only way?

like image 603
that guy over there Avatar asked Jul 25 '13 11:07

that guy over there


People also ask

Where is MemoryCache stored?

Cache memory increases a computer's performance. The cache memory is located very close to the CPU, either on the CPU chip itself or on the motherboard in the immediate vicinity of the CPU and connected by a dedicated data bus.

Should I dispose MemoryCache?

MemoryCache implements IDisposable so you should call Dispose before replacing the old instance.


1 Answers

Use bool Contains(string key, string regionName) method.

MSDN:

Determines whether a cache entry exists in the cache. Return Value Type: System.Boolean true if the cache contains a cache entry whose key matches key; otherwise, false.
MemoryCache.Contains Method (String, String), MSDN

like image 105
oakio Avatar answered Sep 19 '22 02:09

oakio