使用IQueryables缓存策略

时间:2009-08-20 16:37:31

标签: linq caching repository-pattern iqueryable

我一直在思考如何最好地缓存IQueryables。我正在使用存储库模式将数据提取到类中,例如:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IQueryable<Category> { get; set; } // For simplicity. It is actually a LazyList<Category> containing an IQueryable
}

现在我怎样才能最好地缓存这个对象?我想在可查询上保留延迟执行,因为实际上除了Category之外还有更多东西可以链接到项目,并且我最好不要将它们存储在对象上,因为对象可能会变得太大,或者它们可能需要单独缓存。

我在想Compiled Query能以某种方式使这成为可能,我只是不太明白。有没有人找到任何好的解决方案?这似乎是一种非常常见的模式。

1 个答案:

答案 0 :(得分:1)

使用已编译的查询,您将缓存类型为Func<MyDataContext, IQueryable<Category>>的结果委托。每当您想要一个新的可查询对象时,您只需创建一个新的数据上下文以传递给委托,并对结果IQueryable<Category>进行查询,就像正常一样。

相关问题