Exemple #1
0
        /// <summary>
        /// Add a new key-value pair to the cache, with given estimated byte size. If total size in cache exceeds
        /// LRU limit then old items get tossed out.
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <param name="byteSize">Estimated byte size</param>
        public void Add(K key, V value, int byteSize)
        {
            if (mBytesUsed + byteSize > mMaxBytes)
            {
                // Full, throw something out
                if (mCacheList.First != null)
                {
                    var first = mCacheList.First;
                    mBytesUsed -= first.Value.size;
                    mCacheDict.Remove(first.Value.key);
                    mCacheList.RemoveFirst();
                }
            }

            var item = new LRUItem <K, V>();

            item.key   = key;
            item.value = value;
            item.size  = byteSize;

            var node = mCacheList.AddLast(item);

            mCacheDict[key] = node;

            mBytesUsed += byteSize;
        }
Exemple #2
0
        /// <summary>
        /// Adds an item to the cache, replacing any existing item.
        /// </summary>
        /// <param name="key">The item key.</param>
        /// <param name="value">The item value.</param>
        /// <param name="ttl">The maximum lifespan of the item or <see cref="TimeSpan.MaxValue" />.</param>
        public void Add(TKey key, TValue value, TimeSpan ttl)
        {
            LRUItem item;

            Assertion.Test(items.Count == lru.Count);

            // If the cache is already maxed out then remove
            // the least recently used item to make room for the
            // new one.

            if (items.Count >= maxItems)
            {
                item = (LRUItem)lru.RemoveLRU();
                items.Remove(item.Key);
                DisposeItem(item);
            }

            // Add the new item, replacing any existing item.

            LRUItem existing;

            if (items.TryGetValue(key, out existing))
            {
                items.Remove(key);
                lru.Remove(existing);
            }

            item = new LRUItem(key, value, ttl);

            items.Add(key, item);
            lru.Add(item);
        }
 /* Min Priority Queue for 'hackerrank/contests/justcode_lru.cs'
  * Since provided priority is greatest (as we are now incrementing priority
  * number in this algorithm to solve this LRU problem) right now, heapify
  * down to bring it down to the last level of the tree/Heap.
  *
  * What should be general case for any priority value?
  * Try to bubble up, if that has no effect heapify?
  */
 public void IncreaseKey(int index, LRUItem item)
 {
     if (A[index].Value == item.Value)
     {
         A[index].Priority = item.Priority;
     }
     // BubbleUp(index);
     Heapify(index);
 }
Exemple #4
0
        /// <summary>
        /// Performs the necessary work to touch an item.
        /// </summary>
        /// <param name="item">The item being touched.</param>
        private void Touch(LRUItem item)
        {
            lru.Touch(item);

            if (renewTTD)
            {
                item.RenewTTD();
            }
        }
 private void RemoveItem(LRUItem item)
 {
     //LOG.Debug("LRU RemoveItem " + item.key);
     this.item_list.Remove(item);
     this.item_table.Remove(item.key);
     if (item.value != null)
     {
         item.value.Dispose();
     }
 }
 /// <summary>
 /// O(N)
 /// </summary>
 /// <param name="item"></param>
 /// <returns> returns index; -1 if not found </returns>
 protected int heapSearch(LRUItem item)
 {
     for (int i = 0; i < heapSize; i++)
     {
         LRUItem aItem = A[i];
         if (item.Value == aItem.Value)
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #7
0
        /// <summary>
        /// Disposes the item's value if required.
        /// </summary>
        /// <param name="item">The item being removed from the cache.</param>
        private void DisposeItem(LRUItem item)
        {
            IDisposable o;

            if (!autoDispose)
            {
                return;
            }

            o = item.Value as IDisposable;
            if (o != null)
            {
                o.Dispose();
            }
        }
    /// <summary>
    /// 添加一个节点
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void AddItem(string key, T value)
    {
        //LOG.Debug("LRU AddItem " + key);

        LRUItem item;

        if (this.item_table.TryGetValue(key, out item))
        {
            RemoveItem(item);
        }
        item       = new LRUItem();
        item.key   = key;
        item.value = value;
        item.time  = GameTimer.time;
        this.item_table.Add(key, item);
        this.item_list.Add(item as PListNode);
    }
Exemple #9
0
        public virtual bool Unbind(ActorSystem actorSystem)
        {
            ThrowIfDisposed();

            if (actorSystem != null &&
                TryGetBindedSystem(actorSystem.Name, out ActorSystem bindedSystem) && bindedSystem == actorSystem)
            {
                if (_lastBinding.Key == actorSystem.Name)
                {
                    _lastBinding = new LRUItem <ActorSystem, string> {
                    }
                }
                ;

                return(_actorSystemBindings?.TryRemove(actorSystem.Name, out bindedSystem) ?? false);
            }
            return(false);
        }
Exemple #10
0
        protected bool TryGetBindedSystem(string actorSystem, out ActorSystem bindedSystem)
        {
            bindedSystem = null;
            if (actorSystem == _lastBinding.Key)
            {
                bindedSystem = _lastBinding.Value;
                return(true);
            }

            var result = (_actorSystemBindings?.TryGetValue(actorSystem, out bindedSystem) ?? false);

            if (result)
            {
                _lastBinding = new LRUItem <ActorSystem, string> {
                    Key   = actorSystem,
                    Value = bindedSystem
                }
            }
            ;
            return(result);
        }
    /// <summary>
    /// 遍历节点,提供visitor如果返回true时,停止遍历
    /// </summary>
    /// <param name="visitor"></param>
    public void ForEach(Predicate <T> visitor)
    {
        if (visitor == null)
        {
            return;
        }
        if (Count == 0)
        {
            return;
        }
        PListNode node = this.item_list.next;

        while (node != this.item_list)
        {
            LRUItem item = node as LRUItem;
            if (visitor(item.value))
            {
                break;
            }
            node = node.next;
        }
    }
Exemple #12
0
        /// <summary>
        /// Adds an item to the cache.
        /// </summary>
        /// <param name="key">The item key.</param>
        /// <param name="value">The item value.</param>
        public void Add(TKey key, TValue value)
        {
            LRUItem item;

            Assertion.Test(items.Count == lru.Count);

            // If the cache is already maxed out then remove
            // the least recently used item to make room for the
            // new one.

            if (items.Count >= maxItems)
            {
                item = (LRUItem)lru.RemoveLRU();
                items.Remove(item.Key);
                DisposeItem(item);
            }

            // Add the new item

            item = new LRUItem(key, value);
            items.Add(key, item);
            lru.Add(item);
        }
    /// <summary>
    /// 更新节点
    /// </summary>
    /// <param name="needkeeper">可选,外部决定是否需要删除过期节点</param>
    public void Update(Predicate <T> needkeeper)
    {
        //从尾部开始遍历
        PListNode node = this.item_list.prev;

        while (node != this.item_list)
        {
            LRUItem   item = node as LRUItem;
            PListNode prev = node.prev;
            if (item.time + this.life_time < GameTimer.time)
            {
                if (needkeeper == null || !needkeeper(item.value))
                {
                    RemoveItem(item);
                }
            }
            else
            {
                break;
            }
            node = prev;
        }
    }
Exemple #14
0
        public virtual bool Bind(ActorSystem actorSystem)
        {
            ThrowIfDisposed();

            if (actorSystem != null)
            {
                var actorSystemName = actorSystem.Name;
                if (TryGetBindedSystem(actorSystemName, out ActorSystem bindedSystem))
                {
                    return(bindedSystem == actorSystem);
                }

                _actorSystemBindings[actorSystemName] = actorSystem;

                _lastBinding = new LRUItem <ActorSystem, string> {
                    Key   = actorSystemName,
                    Value = actorSystem
                };

                return(true);
            }
            return(false);
        }
 public int find(LRUItem item)
 {
     return(heapSearch(item));
 }
 /* Usually Bubble Up
  * However, in this solution, priority coming up later are always greater.
  * Therefore, Bubble-Up is not required, making insert constant time.
  */
 protected void Insert(LRUItem item)
 {
     // BubbleUp(heapSize++);
 }