예제 #1
0
        async Task IAsyncCache.InitAsync(InitInput input)
        {
            var entered = false;

            try
            {
                entered = _syncRoot.WaitOne(Timeout.Infinite);
                if (entered)
                {
                    input.Validate();
                    _logger      = input.Logger;
                    _combinedCts = CancellationTokenSource.CreateLinkedTokenSource(_cts.Token, input.Token);
                    _cancelToken = _combinedCts.Token;

                    _gotoDbForMissingKey = input.CacheConfig.GoToDbForMissingKey;
                    ((IReloadable)this).ReloadOnceImmediate = input.CacheConfig.ReloadFromFileFirst;

                    _cacheData = CreateCacheDataInstance(input.CacheConfig,
                                                         CreateCacheSerializer(input.TopLevelLocalDirectory, false));
                    _additionalData = CreateAdditionalCacheDataInstance(input.CacheConfig,
                                                                        CreateCacheSerializer(input.TopLevelLocalDirectory.CreateSubdirectory(AdSubFolder), true));

                    var cacheWorkerInitializer = CacheWorkerInitializer(input);
                    var instances = await cacheWorkerInitializer.InitInstances().ConfigureAwait(false);

                    //item 1 is actual cache worker
                    _cacheWorker = instances.Item1;
                    //item 2 is additional data worker.
                    _additionalDataWorker = instances.Item2;

                    await InternalReload(_cancelToken).ConfigureAwait(false);

                    //Thread.MemoryBarrier();

                    //Post init actions.
                    instances = cacheWorkerInitializer.PostInitInstances();
                    //item 1 is actual cache worker
                    _cacheWorker = instances.Item1;
                    //item 2 is additional data worker.
                    _additionalDataWorker = instances.Item2;

                    //Thread.MemoryBarrier();

                    _cacheScheduler     = new CacheScheduler(this, input.ReloadConfig, _cancelToken, _logger, _name);
                    _runningInInitPhase = false;
                }
            }
            finally
            {
                if (entered)
                {
                    _syncRoot.Set();
                }
            }
        }
예제 #2
0
        public async Task FreshLoadToAsync(AsyncCacheData <TKey, TValue> data, CancellationToken token)
        {
            try
            {
                data.Clear();
                await LoadAsync(data, token).ConfigureAwait(false);

                token.ThrowIfCancellationRequested();
                await SerializeAsync(data).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (Fallback == null)
                {
                    throw new AsyncCacheException(AsyncCacheErrorCode.InvalidCache, "Unknown Error", e);
                }
                await Fallback.FreshLoadToAsync(data, token).ConfigureAwait(false);
            }
        }
예제 #3
0
 protected abstract Task LoadAsync(AsyncCacheData <TKey, TValue> emptyData, CancellationToken token);