public async Task <IReadOnlyList <Document> > GetDocumentsAsync(IEnumerable <string> documentIds) { var documents = new List <KeyValuePair <string, Document> >(); var missingIds = new List <string>(); foreach (string docId in documentIds) { string key = LocalCacheKeys.Document(docId); bool cached = _cache.TryGetValue(key, out Document cachedDoc); documents.Add(new KeyValuePair <string, Document>(docId, cachedDoc)); if (!cached) { missingIds.Add(docId); } } List <Document> missing = await _context.GetDocumentsAsync(missingIds); List <Document> results = documents.GroupJoin(missing, d => d.Key, m => m.Id, (d, m) => d.Value ?? m.FirstOrDefault()) .Where(d => d != null) .ToList(); foreach (var missingDoc in missing) { string key = LocalCacheKeys.Document(missingDoc.Id); _cache.Set(key, missingDoc, TimeSpan.FromMinutes(5)); } return(results); }
public async Task <IActionResult> Test([FromServices] DocumentContext documentContext) { var docs = await documentContext.GetDocumentsAsync(new[] { "1", "2" }); return(Ok(docs)); }