缓存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;
    //    }
    //}
}

原文地址:https://www.cnblogs.com/movemoon/p/4755232.html