zoukankan      html  css  js  c++  java
  • C#缓存操作

    1.缓存辅助方法类的接口代码:

      public interface IThrottleStore
        {
            /// <summary>
            /// 试图获取值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="entry"></param>
            /// <returns></returns>
            bool TryGetValue(string key, out ThrottleEntry entry);
    
            /// <summary>
            /// 增量请求
            /// </summary>
            /// <param name="key"></param>
            void IncrementRequests(string key);
    
            /// <summary>
            /// 反转
            /// </summary>
            /// <param name="key"></param>
            void Rollover(string key);
    
            /// <summary>
            /// 清除
            /// </summary>
            void Clear();
        }

    2.缓存辅助方法的实体类代码:

     /// <summary>
        /// 调节实体
        /// </summary>
        public class ThrottleEntry
        {
            /// <summary>
            /// 开始时间
            /// </summary>
            public DateTime PeriodStart { get; set; }
    
            /// <summary>
            /// 请求
            /// </summary>
            public long Requests { get; set; }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public ThrottleEntry()
            {
                PeriodStart = DateTime.UtcNow;
                Requests = 0;
            }
        }

    3.缓存辅助类的实现代码:

     public class InMemoryThrottleStore : IThrottleStore
        {
            /// <summary>
            /// 定义类型字段时,采用线程安全字典
            /// </summary>
            private readonly ConcurrentDictionary<string, ThrottleEntry> _throttleStore = new ConcurrentDictionary<string, ThrottleEntry>();
    
            public bool TryGetValue(string key, out ThrottleEntry entry)
            {
                return _throttleStore.TryGetValue(key, out entry);
            }
    
            public void IncrementRequests(string key)
            {
                _throttleStore.AddOrUpdate(key, k => { return new ThrottleEntry() { Requests = 1 }; },
                                           (k, e) => { e.Requests++; return e; });
            }
    
            public void Rollover(string key)
            {
                ThrottleEntry dummy;
                _throttleStore.TryRemove(key, out dummy);
            }
    
            public void Clear()
            {
                _throttleStore.Clear();
            }
        }
  • 相关阅读:
    协方差的意义
    ios7新特性实践
    微信支付大盗--黑色产业链
    UVA 297 Quadtrees(四叉树建树、合并与遍历)
    HDU 2876 Ellipse, again and again
    java中接口的定义与实现
    Oracle Linux 6.3下安装Oracle 11g R2(11.2.0.3)
    Fortran使用隐形DO循环和reshape给一维和多维数组赋初值
    Java实现 蓝桥杯VIP 算法训练 成绩的等级输出
    Java实现 蓝桥杯VIP 算法训练 成绩的等级输出
  • 原文地址:https://www.cnblogs.com/pengze0902/p/5942262.html
Copyright © 2011-2022 走看看