我已经思考了一段时间如何最好地缓存 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
}
现在我最好如何缓存这个对象?我想保留可查询的延迟执行,因为实际上除了类别之外还有很多东西可以链接到该项目,并且我最好不要获取它们来存储在对象上,因为对象可能变得太大,或者它们可能需要单独缓存。
我正在考虑 编译查询 可以以某种方式使这成为可能,我只是不太明白如何实现。有没有人找到任何好的解决方案?这似乎是一种非常常见的使用模式。
I have been pondering for a while how to best cache IQueryables. I am using a repository pattern to fetch data into a class such as this one:
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
}
Now how would I best go about to cache this object? I would like to keep the deferred execution on the queryable as in reality there are many more things besides Category that can be linked to the item, and I would preferably not fetch them to store on the object as the objects can get too big, or they could need to be cached separately.
I am thinking a Compiled Query could make this possible somehow, I'm just not quite seeing how. Has anyone found any good solution to this? It seems to be a pretty common pattern to use.
发布评论
评论(1)
使用已编译的查询,您可以缓存
Func> 类型的结果委托。每当您需要新的可查询时,只需创建一个新的数据上下文以传递到委托中,并像平常一样对生成的
IQueryable
进行查询。Using a compiled query, you would cache the resulting delegate, of type
Func<MyDataContext, IQueryable<Category>>
. Whenever you want a new queryable, you just create a new data context to pass into the delegate and query against the resultingIQueryable<Category>
like normal.