// stores macro content into the cache private async Task AddMacroContentToCacheAsync(MacroModel model, MacroContent macroContent) { IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); // only if cache is enabled if (umbracoContext.InPreviewMode || model.CacheDuration <= 0) { return; } // do not cache if it should cache by member and there's not member if (model.CacheByMember) { IMemberManager?memberManager = _httpContextAccessor.HttpContext?.RequestServices.GetRequiredService <IMemberManager>(); MemberIdentityUser?member = await memberManager?.GetCurrentMemberAsync() !; if (member is null) { return; } } // remember when we cache the content macroContent.Date = DateTime.Now; IAppPolicyCache cache = _appCaches.RuntimeCache; cache.Insert( CacheKeys.MacroContentCacheKey + model.CacheIdentifier, () => macroContent, new TimeSpan(0, 0, model.CacheDuration)); _logger.LogDebug("Macro content saved to cache '{MacroCacheId}'", model.CacheIdentifier); }
public static void InsertCacheItem <T>( this IAppPolicyCache provider, string cacheKey, Func <T> getCacheItem, TimeSpan?timeout = null, bool isSliding = false, string[]?dependentFiles = null) => provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, dependentFiles);
public static void InsertCacheItem <T>(this IAppPolicyCache provider, string cacheKey, Func <T> getCacheItem, TimeSpan?timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding, priority, removedCallback, dependentFiles); }
/// <summary> /// Rebuilds the index /// </summary> /// <param name="indexName"></param> /// <returns></returns> public IActionResult PostRebuildIndex(string indexName) { ActionResult validate = ValidateIndex(indexName, out IIndex? index); if (!validate.IsSuccessStatusCode()) { return(validate); } validate = ValidatePopulator(index !); if (!validate.IsSuccessStatusCode()) { return(validate); } _logger.LogInformation("Rebuilding index '{IndexName}'", indexName); //remove it in case there's a handler there already index !.IndexOperationComplete -= Indexer_IndexOperationComplete; //now add a single handler index.IndexOperationComplete += Indexer_IndexOperationComplete; try { var cacheKey = "temp_indexing_op_" + index.Name; //put temp val in cache which is used as a rudimentary way to know when the indexing is done _runtimeCache.Insert(cacheKey, () => "tempValue", TimeSpan.FromMinutes(5)); _indexRebuilder.RebuildIndex(indexName); return(new OkResult()); } catch (Exception ex) { //ensure it's not listening index.IndexOperationComplete -= Indexer_IndexOperationComplete; _logger.LogError(ex, "An error occurred rebuilding index"); var response = new ConflictObjectResult( "The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {ex}"); HttpContext.SetReasonPhrase("Could Not Rebuild"); return(response); } }