示例#1
0
        async Task IAsyncCache.Dispose()
        {
            _cts.Cancel();
            using (_cts)
            {
                if (_syncRoot.WaitOne(Timeout.Infinite))
                {
                    if (_cacheScheduler != null)
                    {
                        await _cacheScheduler.Dispose().ConfigureAwait(false);

                        _cacheScheduler = null;
                    }
                    if (_cacheWorker != null)
                    {
                        await _cacheWorker.Shutdown().ConfigureAwait(false);
                    }
                    if (_additionalDataWorker != null)
                    {
                        await _additionalDataWorker.Shutdown().ConfigureAwait(false);
                    }
                    _syncRoot.Set();
                }
                if (_combinedCts != null)
                {
                    using (_combinedCts)
                    {
                        //to dispose.
                    }
                }
            }
        }
示例#2
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();
                }
            }
        }
示例#3
0
        public void ScheduledExpiry_EnsureItemRemoved()
        {
            var scheduler = new CacheScheduler(TimeSpan.FromSeconds(5));
            var c         = new CacheLite(scheduler);

            c.Set("id1", "Scooby Doo", TimeSpan.FromSeconds(1));

            // force an update
            scheduler.Update();

            Assert.IsFalse(c.Exists("id1"));
        }