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

时间:2013-07-09 19:54:10

标签: 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;
}

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

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

然后循环结果并清除它们(我甚至可以直接清除一个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;
}

但我得到了:

对象引用未设置为对象的实例

我现在要试试p.s.w.g除了它可能起作用之外我的眼睛更优雅。

第二次编辑:

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

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

1 个答案:

答案 0 :(得分:4)

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