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

  • 相关阅读:
    AD域撤销域用户管理员权限方案
    域普通用户执行金蝶K/3权限不够解决方法
    redis安装windows+64位
    解决pycharm中no python interpreter configured问题
    python3报错---Error in sitecustomize; set PYTHONVERBOSE for traceback: NameError: name 'reload' is not defined
    python+robot+oracle:执行脚本时中文sql报错:UnicodeEncodeError: 'ascii' codec can't encode
    git-最简单的操作流程
    pycharm配置后执行RF脚本
    性能测试-jmeter基础5-设计数据自动递增
    性能测试-jmeter基础4-设置日期的递增参数demo
  • 原文地址:https://www.cnblogs.com/movemoon/p/4755232.html
Copyright © 2011-2022 走看看