zoukankan      html  css  js  c++  java
  • 一个缓存类CacheFactoryBase

    Common
    public class SafeDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
    object WriteValueEvent_Handle = new object();
    public new TValue this[TKey key]
    {
    get
    {
    return base[key];
    }
    set
    {
    lock (WriteValueEvent_Handle)
    {
    base[key] = value;
    }
    }
    }
    public new void Add(TKey key, TValue value)
    {
    lock (WriteValueEvent_Handle)
    {
    base.Add(key, value);
    }
    }
    public SafeDictionary(int Capacity)
    :
    base(Capacity)
    {

    }
    }
    Cacher
    public class CacheFactoryBase<Tkey, TValue> : IDisposable
    {
    //SecondFactor
    SafeDictionary<Tkey, TValue> dic = null;//asc
    System.Timers.Timer timer;
    SortedDictionary
    <DateTime, Tkey> sord = null;
    DateTime curNext;

    object _SecondFactor_WriteHandle = new object();
    object _Dic_WriteHandle = new object();
    object _SorDic_WriteHandle = new object();
    public int Count
    {
    get
    {
    return dic.Count;
    }
    }

    bool InsertSorDic(Tkey key, DateTime time)
    {
    if (sord == null)
    sord
    = new SortedDictionary<DateTime, Tkey>();
    if (sord.ContainsKey(time))
    {
    InsertSorDic(key, time.AddMilliseconds(
    1));
    }
    else
    {
    double interval;
    interval
    = time.Subtract(DateTime.Now).TotalMilliseconds;
    if (interval < 0)
    return false;

    if (timer == null)
    {
    timer
    = new System.Timers.Timer();
    timer.Elapsed
    += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    }
    if (!timer.Enabled)
    {
    timer.Interval
    = interval;
    timer.Enabled
    = true;
    timer.Start();
    curNext
    = time;
    }
    else
    {
    if (time < curNext)
    {
    timer.Interval
    = interval;
    curNext
    = time;
    }
    }
    lock (_SorDic_WriteHandle)
    {
    if (sord.ContainsKey(time))
    sord[time]
    = key;
    else
    sord.Add(time, key);
    }
    }
    return true;
    }
    void RemoveSorDic(DateTime time)
    {
    lock (_SorDic_WriteHandle)
    {
    DateTime trueTime;
    Tkey key;
    if (sord.ContainsKey(time))
    {
    key
    = sord[time];
    trueTime
    = time;
    }
    else
    {
    key
    = sord.First().Value;
    trueTime
    = sord.First().Key;
    }
    sord.Remove(time);
    Remove(key);
    }

    if (sord.Count != 0)
    {
    curNext
    = sord.First().Key;
    double interval = curNext.Subtract(DateTime.Now).TotalMilliseconds;
    if (interval > 0)
    {
    timer.Interval
    = interval;
    timer.Enabled
    = true;
    timer.Start();
    }
    else
    {
    RemoveSorDic(curNext);
    }
    }
    else
    {
    timer.Enabled
    = false;
    timer.Stop();
    //tiemr = null;// not set,reuse
    }
    }
    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    RemoveSorDic(curNext);
    }

    double _SecondFactor;
    public double SecondFactor
    {
    get
    {
    return _SecondFactor;
    }
    set
    {
    lock (_SecondFactor_WriteHandle)
    {
    _SecondFactor
    = value;
    }
    }
    }
    protected DateTime CalculateSecond(int second)
    {
    return DateTime.Now.AddSeconds(SecondFactor * second);
    }

    //ctor
    public CacheFactoryBase(int capacity, int secondFactor)
    {
    SecondFactor
    = secondFactor;
    dic
    = new SafeDictionary<Tkey, TValue>(capacity);
    }
    //public TValue this[Tkey key]
    //{
    // get
    // {
    // return dic[key];
    // }
    // set
    // {
    // Insert(key, value);
    // }
    //}
    public TValue Get(Tkey key)
    {
    return dic[key];
    }
    public bool Contain(Tkey key)
    {
    return dic.ContainsKey(key);
    }
    public void Insert(Tkey key, TValue src)
    {
    lock (_Dic_WriteHandle)
    {
    if (dic.ContainsKey(key))
    dic[key]
    = src;
    else
    dic.Add(key, src);
    }
    }
    public void Insert(Tkey key, TValue src, int second)
    {
    Insert(key, src, CalculateSecond(second));
    }
    public void Insert(Tkey key, TValue src, TimeSpan span)
    {
    Insert(key, src, DateTime.Now.Add(span));
    }
    public void Insert(Tkey key, TValue src, DateTime endTime)//可以加刷新时间
    {
    if (InsertSorDic(key, endTime))
    Insert(key, src);
    }

    public void Remove(Tkey key)
    {
    lock (_Dic_WriteHandle)
    {
    if (dic.ContainsKey(key))
    dic.Remove(key);
    }
    }
    public void RemoveByPattern(string pattern, bool ignoreCase)
    {
    Dictionary
    <Tkey, TValue>.Enumerator IEnum = dic.GetEnumerator();
    Regex regex;
    if(ignoreCase)
    regex
    = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
    else
    regex
    = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled);
    lock (_Dic_WriteHandle)
    {
    while (IEnum.MoveNext())
    {
    if (regex.IsMatch(IEnum.Current.Key.ToString()))
    dic.Remove(IEnum.Current.Key);
    }
    }
    }
    public void Clear()
    {
    dic.Clear();
    }
    #region IDisposable Members
    void IDisposable.Dispose()
    {
    Clear();
    }
    #endregion
    }
  • 相关阅读:
    小白学 Python 数据分析(21):pyecharts 好玩的图表(系列终篇)
    小白学 Python 数据分析(20):pyecharts 概述
    小白学 Python 数据分析(19):Matplotlib(四)常用图表(下)
    小白学 Python 数据分析(18):Matplotlib(三)常用图表(上)
    在 ASP.NET Core 程序启动前运行你的代码
    在 ASP.NET Core 项目中使用 MediatR 实现中介者模式
    在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射
    [Vue 牛刀小试]:第十七章
    或许是你应该了解的一些 ASP.NET Core Web API 使用小技巧
    [Vue 牛刀小试]:第十六章
  • 原文地址:https://www.cnblogs.com/Googler/p/1762126.html
Copyright © 2011-2022 走看看