From 21b005a2a24b895a9ac9f86d5f5f7bc14dd9f789 Mon Sep 17 00:00:00 2001 From: Higor Goulart Massiroli Date: Tue, 24 Jun 2025 17:46:50 -0300 Subject: [PATCH] feat: custom filter by parameters --- .../OutputCacheManager.cs | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/DevTrends.MvcDonutCaching/OutputCacheManager.cs b/DevTrends.MvcDonutCaching/OutputCacheManager.cs index 6cd0387..b804cd5 100644 --- a/DevTrends.MvcDonutCaching/OutputCacheManager.cs +++ b/DevTrends.MvcDonutCaching/OutputCacheManager.cs @@ -136,9 +136,54 @@ public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction] /// The name of the controller action method. /// A dictionary that contains the parameters for a route. public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction] string actionName, RouteValueDictionary routeValues) + { + RemoveItems(controllerName, actionName, routeValues, (keysToDelete, routeValue) => + { + var keyFrag = _keyBuilder.BuildKeyFragment(routeValue); + if (string.IsNullOrEmpty(keyFrag)) + { + return keysToDelete; + } + + return keysToDelete.Where(_ => !string.IsNullOrEmpty(_) && _.Contains(keyFrag)); + }); + } + + /// + /// Removes all output cache entries for the specified controller, action and parameters. + /// + /// The name of the controller that contains the action method. + /// The name of the controller action method. + /// A dictionary that contains the parameters for a route. + /// + /// A custom filter function that allows selective removal of cache entries based on route parameters. + /// + /// This function receives: + /// + /// + /// - A collection of cache entry keys (`IEnumerable<string>`) + /// + /// + /// - A route key-value pair (`KeyValuePair<string, object>`) + /// + /// + /// It returns a filtered subset of keys to be removed. + /// + /// + /// + /// keysToDelete.Where((string _) => + /// !string.IsNullOrEmpty(_) && + /// _.Contains(routeValue.Key) && + /// _.Contains(routeValue.Value.ToString())) + /// + /// + /// + /// This example filters only those cache keys that are not null/empty and contain both the route key and value. It's useful when the action has multiple parameters and you don't want to match all of them. + /// + /// + public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction] string actionName, RouteValueDictionary routeValues, Func, KeyValuePair, IEnumerable> parameterFilter) { var enumerableCache = _outputCacheProvider as IEnumerable>; - if (enumerableCache == null) { throw new NotSupportedException("Ensure that your custom OutputCacheProvider implements IEnumerable>."); @@ -170,14 +215,7 @@ public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction] } } - var keyFrag = _keyBuilder.BuildKeyFragment(routeValue); - - if (string.IsNullOrEmpty(keyFrag)) - { - continue; - } - - keysToDelete = keysToDelete.Where(_ => !string.IsNullOrEmpty(_) && _.Contains(keyFrag)); + keysToDelete = parameterFilter(keysToDelete, routeValue); } }