Added default predicate to CacheTable.Where() method

This commit is contained in:
KharchenkoVV 2021-08-16 14:16:44 +05:00
parent 1db3a81d56
commit e1310be988
3 changed files with 21 additions and 12 deletions

View File

@ -141,29 +141,38 @@ namespace AsbCloudInfrastructure.Services.Cache
return result;
}
public Task<IEnumerable<TEntity>> SelectAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
=> SelectAsync(predicate, RefreshMode.IfResultEmpty, token);
public Task<IEnumerable<TEntity>> WhereAsync(Func<TEntity, bool> predicate, CancellationToken token = default)
=> WhereAsync(predicate, RefreshMode.IfResultEmpty, token);
public IEnumerable<TEntity> Select(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty)
public IEnumerable<TEntity> Where(Func<TEntity, bool> predicate = default, RefreshMode refreshMode = RefreshMode.IfResultEmpty)
{
bool isUpdated = CheckRefresh(refreshMode);
var result = cached.Where(predicate);
var result = (predicate != default)
? cached.Where(predicate)
: cached;
if (!result.Any() && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
Refresh();
return cached.Where(predicate);
result = (predicate != default)
? cached.Where(predicate)
: cached;
}
return result;
}
public async Task<IEnumerable<TEntity>> SelectAsync(Func<TEntity, bool> predicate, RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
public async Task<IEnumerable<TEntity>> WhereAsync(Func<TEntity, bool> predicate = default,
RefreshMode refreshMode = RefreshMode.IfResultEmpty, CancellationToken token = default)
{
bool isUpdated = await CheckRefreshAsync(refreshMode, token);
var result = cached.Where(predicate);
bool isUpdated = await CheckRefreshAsync(refreshMode, token);
var result = (predicate != default)
? cached.Where(predicate)
: cached;
if (!result.Any() && refreshMode == RefreshMode.IfResultEmpty && !isUpdated)
{
await RefreshAsync(token);
return cached.Where(predicate);
result = (predicate != default)
? cached.Where(predicate)
: cached;
}
return result;
}

View File

@ -41,7 +41,7 @@ namespace AsbCloudInfrastructure.Services
if (telemetryId is null)
return null;
var events = cacheEvents.Select(e => e.IdTelemetry == telemetryId);
var events = cacheEvents.Where(e => e.IdTelemetry == telemetryId);
if (!events.Any())
return null;
@ -89,7 +89,7 @@ namespace AsbCloudInfrastructure.Services
if (messagesList.Count == 0)
return result;
var users = cacheTUsers.Select(u => u.IdTelemetry == telemetryId);
var users = cacheTUsers.Where(u => u.IdTelemetry == telemetryId);
foreach (var message in messagesList)
{

View File

@ -28,7 +28,7 @@ namespace AsbCloudInfrastructure.Services
this.telemetryService = telemetryService;
this.saubDataCache = saubDataCache;
cacheOperations = cacheDb.GetCachedTable<OperationCategory>((AsbCloudDbContext)db);
operations = cacheOperations.Select(c => true);
operations = cacheOperations.Where(c => true);
operationDetectorService = new TelemetryOperationDetectorService(operations);
}