0
我有這種方法用於從我的上下文中檢索結果並使用MemoryCache
對其進行緩存。從EF中緩存結果
public IEnumerable<CustomerRole> GetCustomerRoles()
{
string key = String.Format(CC_CACHE_CUSTOMER_ROLE_ALL, "all");
return _cacheManager.Get(key,() =>
{
return from r in _customerRoleRepository.Table select r;
}
);
}
我然後在我看來,用這個像
@foreach (CustomerRole role in Model)
{
}
我的問題是,由於沒有執行的實際結果,直到數據被訪問(在我看來),它實際上不是緩存結果。
如何強制通過我的緩存功能運行此查詢,而不是等到數據被使用?
我沒有包括什麼_cacheManager.Get()
所做的,因爲我知道它正在緩存我發送給它的任何內容,但如果您認爲這是問題,請告訴我,我將發佈相關代碼。
注:我曾嘗試做這種方式希望它會強制查詢運行,但仍然沒有運氣
public IEnumerable<CustomerRole> GetCustomerRoles()
{
string key = String.Format(CC_CACHE_CUSTOMER_ROLE_ALL, "all");
return _cacheManager.Get(key,() =>
{
var roles = from r in _customerRoleRepository.Table select r;
return roles.Take(roles.Count());
}
);
}
這就是我正在尋找,謝謝。 – webnoob
p.s怎麼''.AsEnumerable()'不會這樣做?我認爲它應該(因爲這是我試過的)。 – webnoob
看到這個:http://stackoverflow.com/questions/17968469/whats-the-differences-between-tolist-asenumerable-asqueryable – Oscar