Example #1
0
        /// <summary>
        /// 设置一个缓存项,若键已存在则将覆盖,add a new item
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="ttl">缓存时间,零值永不过期,单位:秒 <para>ttl, zero is no expired, unit: seconds.</para></param>
        public void Set(TKey key, TValue value, int ttl)
        {
            int endTick = 0;

            if (ttl > 0)
            {
                endTick = Environment.TickCount + ttl;
            }

            LinkedListNode <LRUCacheItem <TKey, TValue> > node;

            if (this.dictionary.TryGetValue(key, out node))
            {
                this.list.Remove(node);
                this.list.AddLast(node);

                node.Value.ItemValue = value;
                node.Value.EndTick   = endTick;
            }
            else
            {
                //capacity - 1 differ
                if (this.dictionary.Count > this.capacity)
                //if (cachedObjects.Count >= capacity)
                {
                    this.RemoveLastUsed();
                }

                LRUCacheItem <TKey, TValue> cacheItem = new LRUCacheItem <TKey, TValue>(key, value, endTick);
                node = new LinkedListNode <LRUCacheItem <TKey, TValue> >(cacheItem);
                this.list.AddLast(node);
                this.dictionary.Add(key, node);
            }
        }
Example #2
0
        /// <summary>
        /// 添加一个缓存项,若键已存在则添加失败,add a new item,  if exists to failure
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="ttl">缓存时间,零值永不过期,单位:秒 <para>ttl, zero is no expired, unit: seconds.</para></param>
        /// <returns>success: true, ,key exists: false </returns>
        public bool Add(TKey key, TValue value, int ttl)
        {
            int endTick = 0;

            //capacity - 1 differ

            if (this.dictionary.Count > this.capacity)
            //if (cachedObjects.Count >= capacity)
            {
                this.RemoveLastUsed();
            }

            LinkedListNode <LRUCacheItem <TKey, TValue> > node;

            if (this.dictionary.TryGetValue(key, out node))
            {
                if (node.Value.EndTick == 0)
                {
                    return(false);
                }
                else if (Environment.TickCount - node.Value.EndTick < 0)
                {
                    return(false);
                }
                else //update
                {
                    if (ttl > 0)
                    {
                        endTick = Environment.TickCount + ttl;
                    }

                    this.list.Remove(node);
                    this.list.AddLast(node);

                    node.Value.ItemValue = value;
                    node.Value.EndTick   = endTick;

                    return(true);
                }
            }

            if (ttl > 0)
            {
                endTick = Environment.TickCount + ttl;
            }

            LRUCacheItem <TKey, TValue> cacheItem = new LRUCacheItem <TKey, TValue>(key, value, endTick);

            node = new LinkedListNode <LRUCacheItem <TKey, TValue> >(cacheItem);
            this.list.AddLast(node);
            this.dictionary.Add(key, node);

            return(true);
        }