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();
            }
        }
  • 相关阅读:
    es6中promise的实现及原理
    移动端
    javascript知识点复习
    html和css基本常识总结
    kafka,查看指定group下topic的堆积数量
    解决问题:Android设备运行自动化脚本报错 ioerror RPC server not started
    linux下安装python3
    使用vsftpd搭建FTP服务
    前端性能监控平台showslow+Yslow搭建
    学习笔记-- Python网络编程
  • 原文地址:https://www.cnblogs.com/pengze0902/p/5942262.html
Copyright © 2011-2022 走看看