zoukankan      html  css  js  c++  java
  • 缓存2

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Collections.Concurrent;
    using System.Threading;

    namespace WebApiDemo.Common
    {

        //public class LRUCache<TValue> : IEnumerable<KeyValuePair<string, TValue>>
        //{
        //    private TimeSpan maxTime; //最大过期时间
            
        //    private long ageToDiscard = 0;  //淘汰的年龄起点
        //    private long currentAge = 0;        //当前缓存最新年龄
        //    private int maxSize = 0;          //缓存最大容量
        //    private readonly ConcurrentDictionary<string, TrackValue> cache;
        //    public LRUCache(int maxKeySize, TimeSpan maxExpireTime)
        //    {
        //        cache = new ConcurrentDictionary<string, TrackValue>();
        //        maxSize = maxKeySize;
        //        maxTime = maxExpireTime;
        //    }

        //    public void Add(string key, TValue value)
        //    {
        //        Add(key, value, maxTime);
        //    }
        //    public void Add(string key, TValue value, TimeSpan expireTime)
        //    {
        //        Adjust(key);
        //        var result = new TrackValue(this, value, expireTime);
        //        cache.AddOrUpdate(key, result, (k, o) => result);
        //    }
        //    public TValue Get(string key)
        //    {
        //        var data = CheckExpire(key);
        //        if (data.Item1 != null)
        //        {
        //            return data.Item1.Value;
        //        }
        //        return default(TValue);
        //    }

        //    private class TrackValue
        //    {
        //        //TrackValue增加创建时间和过期时间
        //        public readonly DateTime CreateTime;
        //        public readonly TimeSpan ExpireTime;

        //        public readonly TValue Value;
        //        public long Age;
        //        public TrackValue(LRUCache<TValue> lv, TValue tv, TimeSpan expireTime)
        //        {
        //            Age = Interlocked.Increment(ref lv.currentAge);
        //            Value = tv;
        //            CreateTime = DateTime.Now;
        //            ExpireTime = expireTime;
        //        }
        //    }

        //    private void Adjust(string key)
        //    {
        //        while (cache.Count >= maxSize)
        //        {
        //            long ageToDelete = Interlocked.Increment(ref ageToDiscard);
        //            var toDiscard =
        //                  cache.FirstOrDefault(p => p.Value.Age == ageToDelete);
        //            if (toDiscard.Key == null)
        //                continue;
        //            TrackValue old;
        //            cache.TryRemove(toDiscard.Key, out old);
        //        }
        //    }
        //    /// <summary>
        //    /// 检查过期
        //    /// </summary>
        //    /// <param name="key"></param>
        //    /// <returns></returns>
        //    private Tuple<TrackValue, bool> CheckExpire(string key)
        //    {
        //        TrackValue result;
        //        if (cache.TryGetValue(key, out result))
        //        {
        //            var age = DateTime.Now.Subtract(result.CreateTime);
        //            if (age >= maxTime || age >= result.ExpireTime)
        //            {
        //                TrackValue old;
        //                cache.TryRemove(key, out old);
        //                return Tuple.Create(default(TrackValue), false);
        //            }
        //        }

        //        return Tuple.Create(result, true);
        //    }
        //    /// <summary>
        //    /// 定时检查过期
        //    /// </summary>
        //    public void Inspection()
        //    {
        //        foreach (var item in this)
        //        {
        //            CheckExpire(item.Key);
        //        }
        //    }

        //    public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
        //    {
        //        //foreach (var item in cache.AsQueryable())
        //        //{
        //        //    yield return KeyValuePair(;
        //        //}
        //        return null;
        //    }
        //}
    }

  • 相关阅读:
    做汉堡
    作业2结对子
    软件开发流程阅读《构建之法》 第5.5 第6 第7章
    自动生成四则运算
    修改过的四则运算
    【暴力DP】[Dota1004]受折磨的灵魂(TormentedSoul)
    【Blog】Start My Journey In Cnblogs!
    【DP+ShortPath】[Dota1000]德鲁伊(Dyrad)
    【Watery DP】[Dota1002]光之守卫(Gandolf)
    【DP】[Dota1003]育母蜘蛛(BroodMother)
  • 原文地址:https://www.cnblogs.com/movemoon/p/4755232.html
Copyright © 2011-2022 走看看