Exemplo n.º 1
0
        public void CallbackInvokedMultipleTimes()
        {
            int callbackCount   = 0;
            var callbackInvoked = new ManualResetEvent(false);

            GcNotification.Register(state =>
            {
                callbackCount++;
                callbackInvoked.Set();
                if (callbackCount < 2)
                {
                    return(true);
                }
                return(false);
            }, null);

            GC.Collect(2, GCCollectionMode.Forced, blocking: true);
            Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)));
            Assert.Equal(1, callbackCount);

            callbackInvoked.Reset();

            GC.Collect(2, GCCollectionMode.Forced, blocking: true);
            Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)));
            Assert.Equal(2, callbackCount);

            callbackInvoked.Reset();

            // No callback expected the 3rd time
            GC.Collect(2, GCCollectionMode.Forced, blocking: true);
            Assert.False(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)));
            Assert.Equal(2, callbackCount);
        }
Exemplo n.º 2
0
        public void CallbackInvokedMultipleTimes()
        {
            var reRegisterForFinalize = true;
            var callbackInvoked       = new ManualResetEvent(false);

            GcNotification.Register(state =>
            {
                callbackInvoked.Set();
                return(reRegisterForFinalize);
            }, null);

            GcCollectAndWait();
            Assert.True(callbackInvoked.WaitOne(0));

            callbackInvoked.Reset();
            reRegisterForFinalize = false;

            GcCollectAndWait();
            Assert.True(callbackInvoked.WaitOne(0));

            callbackInvoked.Reset();

            // No callback expected the 3rd time
            GcCollectAndWait();
            Assert.False(callbackInvoked.WaitOne(0));
        }
Exemplo n.º 3
0
        public void CallbackRegisteredAndInvoked()
        {
            var callbackInvoked = new ManualResetEvent(false);

            GcNotification.Register(state =>
            {
                callbackInvoked.Set();
                return(false);
            }, null);

            GcCollectAndWait();
            Assert.True(callbackInvoked.WaitOne(0));
        }
Exemplo n.º 4
0
        public void CallbackRegisteredAndInvoked()
        {
            var callbackInvoked = new ManualResetEvent(false);

            GcNotification.Register(state =>
            {
                callbackInvoked.Set();
                return(false);
            }, null);

            GC.Collect(2, GCCollectionMode.Forced, blocking: true);
            Assert.True(callbackInvoked.WaitOne(TimeSpan.FromSeconds(30)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new MemoryCache instance. This overload is intended for testing purposes.
        /// </summary>
        /// <param name="clock"></param>
        /// <param name="listenForMemoryPressure"></param>
        public MemoryCache([NotNull] IOptions <MemoryCacheOptions> accessor)
        {
            var options = accessor.Options;

            _entries   = new Dictionary <string, CacheEntry>(StringComparer.Ordinal);
            _entryLock = new ReaderWriterLockSlim();
            _entryExpirationNotification = EntryExpired;
            _clock = options.Clock ?? new SystemClock();
            if (options.ListenForMemoryPressure)
            {
                GcNotification.Register(DoMemoryPreassureCollection, state: null);
            }
            _expirationScanFrequency = options.ExpirationScanFrequency;
            _lastExpirationScan      = _clock.UtcNow;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new MemoryCache instance.
        /// </summary>
        /// <param name="clock"></param>
        /// <param name="listenForMemoryPressure"></param>
        public MemoryCache(IOptions <MemoryCacheOptions> optionsAccessor)
        {
            if (optionsAccessor == null)
            {
                throw new ArgumentNullException(nameof(optionsAccessor));
            }

            var options = optionsAccessor.Value;

            _entries   = new Dictionary <object, CacheEntry>();
            _entryLock = new ReaderWriterLockSlim();
            _entryExpirationNotification = EntryExpired;
            _clock = options.Clock ?? new SystemClock();
            if (options.CompactOnMemoryPressure)
            {
                GcNotification.Register(DoMemoryPreassureCollection, state: null);
            }
            _expirationScanFrequency = options.ExpirationScanFrequency;
            _lastExpirationScan      = _clock.UtcNow;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates a new <see cref="PersistentMemoryCache"/> instance.
        /// </summary>
        /// <param name="options">The options of the cache.</param>
        public PersistentMemoryCache(PersistentMemoryCacheOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _Options = options;

            _InMemoryEntries             = new Dictionary <object, PersistentCacheEntry>();
            _EntryLock                   = new ReaderWriterLockSlim();
            _SetEntry                    = SetEntry;
            _EntryExpirationNotification = EntryExpired;

            _LastExpirationScan = _Options.Clock.UtcNow;
            if (_Options.CompactOnMemoryPressure)
            {
                GcNotification.Register(DoMemoryPreassureCollection, state: null);
            }
            if (_Options.IsPersistent)
            {
                ReloadDataFromStore();
            }
        }