4

我有一个请求缓存,使用 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;
}

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

public IEnumerable<string> GetKey (Func<string, bool> condition)

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

有什么帮助吗?

谢谢

编辑:

服务,我正在尝试(一直在盲目地尝试一些事情,但或多或​​少地遵循这条路)

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;
}

但我得到:

你调用的对象是空的

我现在要试试 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);
}

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

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

    foreach (var ownedItemSummaryKey in ownedItemSummaryKeys.ToList())
    {
        CacheCurrentCall.Instance.Clear(ownedItemSummaryKey);
    }
}
4

1 回答 1

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);
}

或在查询语法中:

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;
}

更新我错过了阅读问题。我以为您想根据键的某些标准选择值。如果您只想选择键,实际上会更容易一些:

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

或者在查询语法中:

public IEnumerable<string> GetKey (Func<string, bool> condition)
{
    return
        from k in HttpContext.Current.Items.Keys.OfType<string>()
        where condition(k)
        select k;
}
于 2013-07-09T19:59:41.377 回答