Skip to content

feat: custom filter by parameters #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions DevTrends.MvcDonutCaching/OutputCacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,54 @@ public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction]
/// <param name="actionName">The name of the controller action method.</param>
/// <param name="routeValues">A dictionary that contains the parameters for a route.</param>
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));
});
}

/// <summary>
/// Removes all output cache entries for the specified controller, action and parameters.
/// </summary>
/// <param name="controllerName">The name of the controller that contains the action method.</param>
/// <param name="actionName">The name of the controller action method.</param>
/// <param name="routeValues">A dictionary that contains the parameters for a route.</param>
/// <param name="parameterFilter">
/// A custom filter function that allows selective removal of cache entries based on route parameters.
/// <para>
/// This function receives:
/// </para>
/// <para>
/// - A collection of cache entry keys (`IEnumerable&lt;string&gt;`)
/// </para>
/// <para>
/// - A route key-value pair (`KeyValuePair&lt;string, object&gt;`)
/// </para>
/// <para>
/// It returns a filtered subset of keys to be removed.
/// </para>
/// <example>
/// <code>
/// keysToDelete.Where((string _) =>
/// !string.IsNullOrEmpty(_) &amp;&amp;
/// _.Contains(routeValue.Key) &amp;&amp;
/// _.Contains(routeValue.Value.ToString()))
/// </code>
/// </example>
/// <para>
/// 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.
/// </para>
/// </param>
public void RemoveItems([AspMvcController] string controllerName, [AspMvcAction] string actionName, RouteValueDictionary routeValues, Func<IEnumerable<string>, KeyValuePair<string, object>, IEnumerable<string>> parameterFilter)
{
var enumerableCache = _outputCacheProvider as IEnumerable<KeyValuePair<string, object>>;

if (enumerableCache == null)
{
throw new NotSupportedException("Ensure that your custom OutputCacheProvider implements IEnumerable<KeyValuePair<string, object>>.");
Expand Down Expand Up @@ -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);
}
}

Expand Down