【发布时间】:2022-01-11 07:53:53
【问题描述】:
所以我有一些这样的代码:
var attribute = _genericAttributeService
.GetAttributesForEntity(_workContext.CurrentCustomer.Id, "Customer")
.FirstOrDefault(x => x.Key == "CompareProducts");
GetAttributesForEntity 看起来像这样:
public virtual IList<GenericAttribute> GetAttributesForEntity(int entityId, string keyGroup)
{
return _cacheManager.Get(string.Format(GENERICATTRIBUTE_KEY, entityId, keyGroup), () =>
{
var query = from ga in _genericAttributeRepository.Table
where ga.EntityId == entityId &&
ga.KeyGroup == keyGroup
select ga;
var attributes = query.ToList();
return attributes;
});
}
所以它使用缓存来减少数据库查询。
FirstOrDefault() 现在是在查询返回的列表,还是进行了另一个数据库查询?
很高兴知道这里发生了什么。
【问题讨论】:
-
如果我们不知道 _cacheManager 是什么,我们就无法知道。您可以随时profile the database 了解正在发生的事情。
-
@Crowcoder 说了什么。请添加 CacheManager 的代码或至少一个包的链接。
-
@Crowcoder cachemanager 按请求缓存
-
一个缓存在另一个缓存前面通常是没用的。有时甚至适得其反。
-
虽然您可能正在缓存实体的所有属性,但我会缓存一个字典并更改 API 以查询该字典。扫描整个列表以查找一个项目感觉有点贵。
标签: c# mysql performance linq nopcommerce