Ejemplo n.º 1
0
        /// <summary>
        ///     Removes all expired items from the dictionary.
        /// </summary>
        public void RemoveExpiredItems()
        {
            var removeList = innerDictionary.Where(kvp => kvp.Value.HasExpired).ToList();

            foreach (var kvp in removeList)
            {
                ItemExpired?.Invoke(this, new ExpirableItemRemovedEventArgs <TKey, TValue>(kvp.Key, kvp.Value.Value));
                innerDictionary.Remove(kvp.Key);
            }
        }
        public async void ThenThereIsANotificationAboutTheExpiredItem()
        {
            var createdItem = _scenarioContext["CreatedItem"] as ItemModel;

            using (RabbitMqNotificationBus bus = new RabbitMqNotificationBus())
            {
                ItemExpired notification = new ItemExpired(new CallContext());
                await bus.SubscribeAndWaitFirstMessage <ItemExpired>(a => { notification = a; });

                notification.Should().BeOfType(typeof(ItemExpired));
                notification.Label.Should().BeEquivalentTo(createdItem.Label);
            }
        }
Ejemplo n.º 3
0
        public void ExpireCacheItems()
        {
            var now = DateTimeOffset.UtcNow;

            Parallel.ForEach(_cacheItems, item =>
            {
                if (now - item.Value.LastAccessed >= _cacheExpirationDuration)
                {
                    CacheItem <K, V> itemToRemove;
                    _cacheItems.TryRemove(item.Key, out itemToRemove);
                    ItemExpired?.Invoke(this, itemToRemove);
                }
            });
        }
Ejemplo n.º 4
0
 public bool ContainsKey(K key)
 {
     lock (lockObject)
     {
         if (CacheItems.ContainsKey(key))
         {
             if (CacheItems[key].HasExpired)
             {
                 ItemExpired?.Invoke(this, new CacheItemRemovedEventArgs <K, T>
                 {
                     Key   = key,
                     Value = CacheItems[key].Value
                 });
                 CacheItems.Remove(key);
                 return(false);
             }
             return(true);
         }
         return(false);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        ///     Tries to the get item having the specified key. Returns <c>true</c> if the item exists and has not expired.
        /// </summary>
        public bool TryGetValue(TKey key, out TValue value)
        {
            ExpirableItem <TValue> item;

            if (innerDictionary.TryGetValue(key, out item))
            {
                if (item.HasExpired)
                {
                    ItemExpired?.Invoke(this, new ExpirableItemRemovedEventArgs <TKey, TValue>(key, item.Value));
                    innerDictionary.Remove(key);

                    value = default(TValue);
                    return(false);
                }

                value = item.Value;
                return(true);
            }

            value = default(TValue);
            return(false);
        }
Ejemplo n.º 6
0
 public override void Dispose()
 {
     base.Dispose();
     _memory.Clear();
     ItemExpired?.Dispose();
 }