Ejemplo n.º 1
0
        public void Remove(Transaction.ITransactionLogger transaction)
        {
            if (Count == 0)
            {
                return;
            }
            var keys = new List <object>();

            foreach (MruItem itm in CacheCollection.Values)
            {
                if (itm.Transaction == transaction)
                {
                    keys.Add(itm.Key);
                }
            }
            foreach (object o in keys)
            {
                var m = (MruItem)CacheCollection[o];
                if (m != null && m.IndexToMruList != null)
                {
                    mruManager.RemoveNode(m.IndexToMruList, true);
                }

                CacheCollection.Remove(o);
            }
        }
Ejemplo n.º 2
0
        public void Remove(Transaction.ITransactionLogger transaction)
        {
            if (Count == 0)
            {
                return;
            }
            var keys = new List <TKey>();

            foreach (MruItem <TKey, TValue> itm in CacheCollection.Values)
            {
                if (itm.Transaction == transaction)
                {
                    keys.Add(itm.Key);
                }
            }
            foreach (TKey o in keys)
            {
                MruItem <TKey, TValue> m = CacheCollection[o];
                if (m != null && m.IndexToMruList != null)
                {
                    mruManager.RemoveNode(m.IndexToMruList);
                }
                CacheCollection.Remove(o);
            }
        }
Ejemplo n.º 3
0
 public MruItem RemoveInTail(bool moveToRemoveList)
 {
     if (Count > 0 &&
         (SaveState & SaveTypes.CollectionSave) != SaveTypes.CollectionSave)
     {
         var itm = mruManager.RemoveInTail(moveToRemoveList);
         if (itm != null && itm.Key != null)
         {
             CacheCollection.Remove(itm.Key);
             return(itm);
         }
     }
     return(null);
 }
Ejemplo n.º 4
0
 internal MruItem <TKey, TValue> RemoveInTail(bool moveToRemoveList)
 {
     if (Count > 0 &&
         (SaveState & Sop.Mru.SaveTypes.CollectionSave) !=
         Sop.Mru.SaveTypes.CollectionSave)
     {
         MruItem <TKey, TValue> itm = mruManager.RemoveInTail(moveToRemoveList);
         if (itm != null && (typeof(TKey).IsValueType || itm.Key != null))
         {
             CacheCollection.Remove(itm.Key);
             return(itm);
         }
     }
     return(null);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Removes entry with key.
        /// </summary>
        /// <param name="key">key of entry to delete from collection</param>
        /// <param name="removeFromCache"> </param>
        public virtual object Remove(object key, bool removeFromCache)
        {
            var itm = (MruItem)CacheCollection[key];

            if (itm != null)
            {
                if ((SaveState & SaveTypes.CollectionSave) != SaveTypes.CollectionSave)
                {
                    if (itm.IndexToMruList != null)
                    {
                        mruManager.RemoveNode(itm.IndexToMruList, removeFromCache);
                    }
                    CacheCollection.Remove(key);
                }
            }
            return(itm);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Removes entry with key.
        /// </summary>
        /// <param name="key">key of entry to delete from collection</param>
        public virtual object Remove(TKey key)
        {
            MruItem <TKey, TValue> itm = CacheCollection[key];

            if (itm != null)
            {
                if ((SaveState & Sop.Mru.SaveTypes.CollectionSave) !=
                    Sop.Mru.SaveTypes.CollectionSave)
                {
                    if (itm.IndexToMruList != null)
                    {
                        mruManager.RemoveNode(itm.IndexToMruList);
                    }
                    CacheCollection.Remove(key);
                }
            }
            return(itm);
        }
Ejemplo n.º 7
0
        public void GetOrCreate()
        {
            var cnt = 0;
            var key = 10;
            var src = new CacheCollection <int, int>(n => Sigma(n));

            src.Created += (s, e) => ++ cnt;
            Parallel.For(0, 10, _ => src.GetOrCreate(key));
            Task.Delay(200).Wait();

            Assert.That(cnt, Is.EqualTo(1));
            Assert.That(src.Count, Is.EqualTo(1));
            Assert.That(src.TryGetValue(key, out var _), Is.True, nameof(src.TryGetValue));
            Assert.That(src.TryGetValue(999, out var _), Is.False, nameof(src.TryGetValue));
            Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
            Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

            src.Remove(key);
            src.Clear();
            Assert.That(src.Count, Is.EqualTo(0));
        }
Ejemplo n.º 8
0
        public void GetOrCreate()
        {
            using (var src = new CacheCollection <int, int>(i => Sigma(i)))
            {
                var n   = 0;
                var key = 10;

                src.Created += (s, e) => ++ n;
                Parallel.For(0, 10, e => src.GetOrCreate(key));

                Assert.That(Wait(() => n > 0), "Timeout");
                Assert.That(src.Count, Is.EqualTo(1), nameof(src.Count));
                Assert.That(src.TryGetValue(key, out _), Is.True, nameof(src.TryGetValue));
                Assert.That(src.TryGetValue(999, out _), Is.False, nameof(src.TryGetValue));
                Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
                Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

                src.Remove(key);
                src.Clear();
                Assert.That(src.Count, Is.EqualTo(0));
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Add Object to the cache
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public virtual void Add(object key, object value)
        {
            if (SaveState != SaveTypes.Default)
            {
                return;
            }
            MruItem itm = null;

            if (GeneratePruneEvent)
            {
                if (MaxCapacity > 0 && CacheCollection.Count >= this.MaxCapacity)
                {
                    if (mruManager.RemovedObjects.Count == 0)
                    {
                        bool ogp = GeneratePruneEvent;
                        GeneratePruneEvent = false;
                        itm = mruManager.PeekInTail();
                        // prevent pruning to occur if itm is Specialized Store and is locked by a thread...
                        if (!(itm.Value is SpecializedStoreBase &&
                              ((SpecializedStoreBase)itm.Value).Locker.IsLocked))
                        {
                            Log.Logger.Instance.Log(Log.LogLevels.Information, "MruManager.Add: pruning enter.");
                            for (int i = MaxCapacity; i > MinCapacity; i--)
                            {
                                itm = mruManager.RemoveInTail(mruManager.Collection != null);
                                CacheCollection.Remove(itm.Key);
                                if (!(itm.Value is IDisposable))
                                {
                                    continue;
                                }
                                var specializedStoreBase = itm.Value as SpecializedStoreBase;
                                if (specializedStoreBase != null)
                                {
                                    if (specializedStoreBase.Locker.IsLocked)
                                    {
                                        continue;
                                    }
                                    specializedStoreBase.InvokeFromMru = true;
                                }
                                if (mruManager.Collection == null)
                                {
                                    ((IDisposable)itm.Value).Dispose();
                                }
                                if (specializedStoreBase != null)
                                {
                                    specializedStoreBase.InvokeFromMru = false;
                                }
                            }
                            Log.Logger.Instance.Log(Log.LogLevels.Information, "MruManager.Add: pruning exit.");
                            SaveRemovedBlocks();
                        }
                        GeneratePruneEvent = ogp;
                    }
                }
            }
            itm = (MruItem)Remove(key, true);
            if (itm == null)
            {
                itm = new MruItem(key, value, Transaction);
                itm.IndexToMruList = mruManager.AddInHead(itm, false);
            }
            else
            {
                itm.Transaction = Transaction;
                itm.Key         = key;
                itm.Value       = value;
                mruManager.AddInHead(itm.IndexToMruList, true);
            }
            CacheCollection[key] = itm;
        }