简体   繁体   中英

Use wildcards when searching in HttpContext.Cache

Is there a way to use wildcards or a regex to search and remove items from HttpContext.Cache ?

I can have in my cache "item_1", "item_2",...,"item_n" and I want to remove from cache all values that are related to keys with the pattern "item_*". How to achieve that without checking if the item exists and then remove it?

Ex:

instead of:

HttpContext.Current.Cache.Remove("item_1")
HttpContext.Current.Cache.Remove("item_2")
HttpContext.Current.Cache.Remove("item_3")

I want something like:

HttpContext.Current.Cache.Remove("item_*")

You could loop the items like this:

foreach(var key in HttpContext.Current.Cache)
{
    if (key.StartsWith("item_"))
    {
        // remove the corresponding item here.
    }
}

The basic sample, and needs some tweaking to match your implementation.

AFAIK you can't remove items based on wildcards, as you need the specific key. (Please prove me wrong)

^HttpContext\.Current\.Cache\.Remove\("item_.*?"\)$

You can try this.Replace with empty string .See demo.

http://regex101.com/r/sU3fA2/65

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM