所以我有一些這樣的代碼:
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()現在查詢回傳的串列,或者是其他資料庫的查詢作出?很高興確切地知道這里發生了什么。
uj5u.com熱心網友回復:
FirstOrDefault() 現在是在查詢回傳的串列,還是進行了另一個資料庫查詢?
簡答:
FirstOrDefault 將查詢快取串列。
長答案:
_cacheManager 將使用提供的引數(entityId, keyGroup)從快取中檢索串列。
如果元素不在快取中,它將執行 lambda 從資料庫中檢索資料,并將結果存盤在 key 中(entityId, keyGroup)。
它將存盤完整的List<>. 注意宣告:
var attributes = query.ToList();
因此,如果您GetAttributesForEntity使用相同的引數重復呼叫,您將獲得存盤的結果。
注意:NopCommerce 實作了不同的快取級別和快取失效程式,我現在不會深入研究。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/374506.html
下一篇:扁平化陣列物件
