コード例 #1
0
        /// <summary>
        /// Removes an entry from the cache.
        /// </summary>
        /// <param name="keyData">The key data.</param>
        /// <returns>Whether an entry was removed.</returns>
        public bool Remove(IData keyData)
        {
            if (!_entries.TryRemove(keyData))
            {
                return(false);
            }

            Statistics.NotifyEntryRemoved();
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Deals with an object being disposed.
        /// </summary>
        /// <param name="o">The object.</param>
        private void OnObjectDisposed(DistributedObjectBase o)
        {
            // simply disposing the distributed object removes it from the list
            var info = new DistributedObjectInfo(o.ServiceName, o.Name);

            _objects.TryRemove(info);
        }
コード例 #3
0
        /// <inheritdoc />
        public async ValueTask DisposeAsync()
        {
            if (Interlocked.CompareExchange(ref _running, 0, 1) == 0)
            {
                return;
            }

            if (_repairingCancellation != null)
            {
                _repairingCancellation.Cancel();
                try
                {
                    await _repairing.CfAwait();
                }
                catch (OperationCanceledException) { /* expected */ }

                _repairingCancellation.Dispose();
            }

            await foreach (var(name, cache) in _caches)
            {
                _caches.TryRemove(name); // ok with concurrent dictionary
                await cache.DisposeAsync().CfAwait();
            }
        }
        public async Task Test()
        {
            var d = new ConcurrentAsyncDictionary <string, ValueItem <int> >();
            var i = 0;

            ValueTask <ValueItem <int> > Factory(string key, CancellationToken _)
            {
                i++;
                return(new ValueTask <ValueItem <int> >(new ValueItem <int>(2)));
            }

            var v = await d.GetOrAddAsync("a", Factory);

            Assert.AreEqual(2, v.Value);
            Assert.AreEqual(1, i);

            v = await d.GetOrAddAsync("a", Factory);

            Assert.AreEqual(2, v.Value);
            Assert.AreEqual(1, i);

            var entries = new List <string>();

            await foreach (var(key, value) in d)
            {
                entries.Add($"{key}:{value}");
            }

            Assert.AreEqual(1, entries.Count);
            Assert.AreEqual("a:2", entries[0]);

            var(hasValue, gotValue) = await d.TryGetAsync("a");

            Assert.IsTrue(hasValue);
            Assert.AreEqual(2, gotValue.Value);

            Assert.IsTrue(d.TryRemove("a"));
            Assert.IsFalse(d.TryRemove("a"));

            (hasValue, _) = await d.TryGetAsync("a");

            Assert.IsFalse(hasValue);