/// <summary> /// Rebuild the cache and return the new <see cref="IPhoenixState"/> /// </summary> /// <returns></returns> protected virtual async Task <IPhoenixState> FireAsync() { MethodInfo.CheckMethodForCacheSupported(out _isAsync); try { var target = GetTargetInstance(); var invokedResult = await InvokeAndGetBareResult(target).ConfigureAwait(false); var cacheItem = GetCacheItem(invokedResult); if (cacheItem == null) { var disposing = new DisposingPhoenix(DieAsync()); return(disposing.Reborn(null)); } var cacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(_info.StoreId); //NOTE: Because the cacheItem was created before, the cacheStore cannot be null await cacheStore.SetAsync(_info.Key, cacheItem, DateTime.UtcNow.AddSeconds(_info.MaxAge + _info.StaleWhileRevalidate)).ConfigureAwait(false); Global.Logger.Info($"Updated key \"{_info.Key}\", store \"{_info.StoreId}\""); Retry(_info.GetRefreshTime()); _phoenixState = new InActivePhoenix(); return(_phoenixState); } catch (Exception ex) { Global.Logger.Error($"Error while refreshing key {_info.Key}, store \"{_info.StoreId}\". Will retry after 1 second.", ex); Retry(TimeSpan.FromSeconds(1)); throw; } }
public void Should_run_the_disposing_task_once() { var wait = new AutoResetEvent(false); var count = 0; Task action = Task.Run(() => { wait.Set(); count++; }); var state = new DisposingPhoenix(action); for (var i = 0; i < 1000; i++) { Assert.AreSame(state, state.Reborn(null)); } Assert.IsTrue(wait.WaitOne(1000)); Assert.AreEqual(1, count); }
/// <summary> /// Rebuild the cache and return the new <see cref="IPhoenixState"/> /// </summary> /// <returns></returns> protected virtual async Task<IPhoenixState> FireAsync() { MethodInfo.CheckMethodForCacheSupported(out _isAsync); try { using (var dependencyScope = Activator.BeginScope()) { var target = GetTargetInstance(dependencyScope); var invokedResult = await InvokeAndGetBareResult(target).ConfigureAwait(false); var cacheItem = GetCacheItem(invokedResult); if (cacheItem == null) { var disposing = new DisposingPhoenix(DieAsync()); return disposing.Reborn(null); } var cacheStore = Global.CacheStoreProvider.GetAsyncCacheStore(_info.StoreId); //NOTE: Because the cacheItem was created before, the cacheStore cannot be null await cacheStore.SetAsync(_info.Key, cacheItem, DateTime.UtcNow.AddSeconds(_info.MaxAge + _info.StaleWhileRevalidate)).ConfigureAwait(false); Global.Logger.Info($"Updated key \"{_info.Key}\", store \"{_info.StoreId}\""); Retry(_info.GetRefreshTime()); _phoenixState = new InActivePhoenix(); return _phoenixState; } } catch (Exception ex) { Global.Logger.Error($"Error while refreshing key {_info.Key}, store \"{_info.StoreId}\". Will retry after 1 second.", ex); Retry(TimeSpan.FromSeconds(1)); throw; } }
/// <summary> /// Rebuild the cache and return the new <see cref="IPhoenixState"/> /// </summary> /// <returns></returns> private IPhoenixState Fire() { try { var target = GetTargetInstance(); var cacheItem = GetCacheItem(InvokeAndGetBareResult(target)); if (cacheItem == null) { var disposing = new DisposingPhoenix(Die); return disposing.Reborn(null); } var cacheStore = Global.CacheStoreProvider.GetCacheStore(_info.CacheStoreId); cacheStore.Set(_info.CacheKey, cacheItem, DateTime.UtcNow.AddSeconds(_info.CacheDuration + _info.StaleWhileRevalidate)); WriteCacheUpdatedLog(); _timer.Change(_info.GetRefreshTime(), TimeSpan.Zero); return new AlivePhoenix(); } catch (Exception ex) { Global.Logger.Error($"Error while refreshing key {_info.CacheKey}, store \"{_info.CacheStoreId}\". Will retry after 1 second.", ex); _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.Zero); throw; } }