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

  • 相关阅读:
    C#写的操作系统
    FPS游戏:实现狙击子弹加速
    内表的一些操作例子(工作区赋值使用了新语法)
    SAP MM模块相关透明表收集
    简单的ALV显示例子
    拼接和前导零用法
    LOOP AT GROUP语法练习
    RFC函数的初步使用同步
    excel保存时出现“请注意,您的文档的部分内容可能包含了文档检查器无法删除的个人信息”
    Abap内表
  • 原文地址:https://www.cnblogs.com/movemoon/p/4755232.html
Copyright © 2020-2023  润新知