在 HttpContext.Current.Items 缓存中使用 lambda 表达式查找键

mit*_*med 4 c# lambda httprequest

我有一个请求缓存,使用 HttpContext.Current.Items 实现,如下所示:

private static readonly Lazy<CacheCurrentCall> lazy =
    new Lazy<CacheCurrentCall>(() => new CacheCurrentCall());

public static CacheCurrentCall Instance
{
    get
    {
        IDictionary items = HttpContext.Current.Items;
        if (!items.Contains("CacheCurrentCall"))
        {
            items["CacheCurrentCall"] = new CacheCurrentCall();
        }
        return items["CacheCurrentCall"] as CacheCurrentCall;
    }
}

private CacheCurrentCall()
{

}

public void Add<T>(T o, string key, int cacheDurationSeconds = 0)
{
    HttpContext.Current.Items.Add(key, o);
}

public void Clear(string key)
{
    HttpContext.Current.Items.Remove(key);
}

public bool Exists(string key)
{
    return HttpContext.Current.Items[key] != null;
}

public bool Get<T>(string key, out T value)
{
    try
    {
        if (!Exists(key))
        {
            value = default(T);
            return false;
        }
        value = (T)HttpContext.Current.Items[key];
    }
    catch
    {
        value = default(T);
        return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要删除以特定字符串开头的所有键,因此我正在考虑这样的方法

public IEnumerable<string> GetKey (Func<string, bool> condition)
Run Code Online (Sandbox Code Playgroud)

然后遍历结果并清除它们(我想我什至可以直接在 Clear 中清除接受 lambda 表达式)。但是,如果可能的话,我在尝试实现这样的方法时迷失了方向。

有什么帮助吗?

谢谢

编辑:

Servy,我正在尝试(一直在盲目尝试一些东西,但或多或​​少遵循这条路)

public IEnumerable<string> GetKeys(Func<string, bool> condition)
{
    List<string> list = new List<string>();

    foreach (var key in HttpContext.Current.Items.Keys)
    {
        if (condition(key as string))
        {
            list.Add(key as string);
        }
    }

    return list;
}
Run Code Online (Sandbox Code Playgroud)

但我得到:

你调用的对象是空的

我现在要试试pswg,除了它可能有效之外,它对我的​​眼睛来说更优雅。

第二次编辑:

我需要稍微改变pswg解决方案。我不在缓存中存储字符串而是其他类型的对象,所以我现在使用它

public IEnumerable<string> GetKeys (Func<string, bool> condition)
{
    return HttpContext.Current.Items
        .Cast<DictionaryEntry>()
        .Where(e => e.Key is string && condition(e.Key as string))
        .Select(e => e.Key as string);
}
Run Code Online (Sandbox Code Playgroud)

例如,清除缓存的调用就是这个

public void ClearCache()
{
    var ownedItemSummaryKeys = CacheCurrentCall.Instance.GetKeys(k => k.Contains("OwnedItemSummaryCurrent"));

    foreach (var ownedItemSummaryKey in ownedItemSummaryKeys.ToList())
    {
        CacheCurrentCall.Instance.Clear(ownedItemSummaryKey);
    }
}
Run Code Online (Sandbox Code Playgroud)

p.s*_*w.g 5

Items属性是一个IDictionary,所以你必须要做到这一点:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return HttpContext.Current.Items
        .Cast<DictionaryEntry>()
        .Where(e => e.Key is string && 
                    e.Value is string && 
                    condition(e.Key as string))
        .Select(e => e.Value as string);
}
Run Code Online (Sandbox Code Playgroud)

或在查询语法中:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from e in HttpContext.Current.Items.Cast<DictionaryEntry>()
        where e.Key is string && 
              e.Value is string && 
              condition(e.Key as string)
        select e.Value as string;
}
Run Code Online (Sandbox Code Playgroud)

更新我错过了这个问题。我以为您想根据键的某些标准来选择。如果你只想选择键,它实际上更容易一些:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return HttpContext.Current.Items.Keys
        .OfType<string>()
        .Where(condition);
}
Run Code Online (Sandbox Code Playgroud)

或者在查询语法中:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from k in HttpContext.Current.Items.Keys.OfType<string>()
        where condition(k)
        select k;
}
Run Code Online (Sandbox Code Playgroud)