0

我正在将一个项目从 MVC 迁移到 MVC Core。除了失效之外,我能够用 [ResponseCache] 重现 [OutputCache] 的所有功能。这是我拥有并使用 OutputCache 的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Caching;


namespace Whatever.Web.Util
{
public static class OutputCacheInvalidator
{
    public static void Remove(string[] urls)
    {
        foreach (var url in urls)
        {
            HttpResponse.RemoveOutputCacheItem(url);
        }
    }

    public static void SearchAndRemove(string[] keywords)
    {
        var runtimeType = typeof(Cache);
        var internalCache = runtimeType.GetProperty(
            "InternalCache",
            BindingFlags.Instance | BindingFlags.NonPublic);

        if (internalCache == null || !(internalCache.GetValue(HttpRuntime.Cache) is CacheStoreProvider cache))
        {
            return;
        }

        var enumerator = cache.GetEnumerator();
        var keysToRemove = new List<string>();

        while (enumerator.MoveNext())
        {
            if (enumerator.Key == null)
            {
                continue;
            }

            var key = enumerator.Key.ToString();

            if (keywords.Any(keyword => key.IndexOf(keyword, StringComparison.OrdinalIgnoreCase) >= 0))
            {
                keysToRemove.Add(key);
            }
        }

        foreach (var key in keysToRemove)
        {
            cache.Remove(key);
        }
    }
}
}

.NET Core 等价物是什么?

4

0 回答 0