1.函数内使用lambda
public virtual async Task<TEntity> GetByIdAsync(int? id, Func<ICacheKeyService, CacheKey> getCacheKey = null, bool includeDeleted = true, bool useShortTermCache = false)
{
if (!id.HasValue || id == 0)
return null;
async Task<TEntity> getEntityAsync()
{
return await AddDeletedFilter(Table, includeDeleted).FirstOrDefaultAsync(entity => entity.Id == Convert.ToInt32(id));
}
if (getCacheKey == null)
return await getEntityAsync();
ICacheKeyService cacheKeyService = useShortTermCache ? _shortTermCacheManager : _staticCacheManager;
var cacheKey = getCacheKey(cacheKeyService)
?? cacheKeyService.PrepareKeyForDefaultCache(NopEntityCacheDefaults<TEntity>.ByIdCacheKey, id);
if (useShortTermCache)
return await _shortTermCacheManager.GetAsync(getEntityAsync, cacheKey);
return await _staticCacheManager.GetAsync(cacheKey, getEntityAsync);
}