Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all caches using contains keyword

This is how I am creating a cache key

string cachekey_base = "IR_";
string symbol = "AUD";
static string id = "12345";

string cacheKey_Quote = $"{cachekey_base}{symbol}{id}Quote";

The id is generated randomly so if I know the id then simply remove the cache like this

    MemoryCache.Default.Remove(key);

But the problem is Id can be any random number.

So is there e a way to remove using contains keyword.

For example in my case remove all cache if key contains `"IR_"

Any help or suggestion would be appreciated .

Thanks in advance`

like image 993
Owais Ahmed Avatar asked Sep 13 '25 01:09

Owais Ahmed


1 Answers

you can filter keys you want to remove them then iterate over these keys and remove them as following:

var removedKeys = MemoryCache.Default.Where( x=> x.Key.Contains("IR_")).Select(x=> x.Key).ToList();
foreach(var key in removedKeys)
    MemoryCache.Default.Remove(key);
like image 59
Ahmed Yousif Avatar answered Sep 14 '25 15:09

Ahmed Yousif