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 <IReadOnlyList <string> > GetSearchResultsAsync(int projectId, SearchData searchData) { string cacheKey = LocalCacheKeys.ProjectSearchResults(projectId); if (searchData.ForceNoCache) { _memoryCache.Remove(cacheKey); } return(await _memoryCache.GetOrCreateAsync(cacheKey, entry => { entry.SetSlidingExpiration(TimeSpan.FromHours(6)); return _searchResultsProcessor.SearchAsync(searchData); })); }
private string GetCacheKey(LocalCacheKeys cacheKey, object value) { return(_key + "_" + cacheKey + "_" + value); }