惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - 杨义金

Csla One or more properties are not registered for this type unomp 矿池运行问题随记 矿池负载运行监测记录 MySql 数据库移植记录 后台服务运行后无故停止运行,原因不明 缓存过期时间的设置 Bitcoin 使用及配置记录 关于 CSLA 服务器部署WCF访问出错的问题 AR 不同 继承映射的问题总结 当事人角色 变更映射策略引起的问题 Castle ActiveRecord 二级缓存使用 异常记录 VS2013 抛出 stackoverflow exception 的追踪 CastleActiveRecord在多线程 事务提交时数据库资源竞争导致更新失败的测试结果记录 WF4.0 工作流设计器 传入参数问题记录? Solving GitHub FetchHead (MergeConflict) in Visual Studio 2013 昆明光标科技有限公司简介 Castle.ActiveRecord 多对多关系 引发的错误处理 String PK StringBuilder,传说就是传说,只有动手实验,才能得出确定的答案 .net mvc Bundle 问题解决方案
.net 4.0 自定义本地缓存策略的不同实现
杨义金 · 2013-12-04 · via 博客园 - 杨义金

在分布式系统的开发中,为了提高系统运行性能,我们从服务器中获取的数据需要缓存在本地,以便下次使用,而不用从服务器中重复获取,有同学可能要问,为什么不使用 分布式缓存等,注意,服务器端肯定是考虑到扩展,可以使用各种缓存方法,但数据来到本地,在客户端(各种形式的客户端)是否了需要进行缓存以提升性能呢,回答是肯定的。

在.net 4 中,框架增加了System.Runtime.Caching 名称空间,用于支持缓存,虽然它可以用于服务器端,也可用于本地。

关于缓存在.net 中如何使用,它的概念,可以参考:

http://www.dotblogs.com.tw/larrynung/archive/2010/11/26/19746.aspx

关于如何自定义缓存策略的实现,可参考:

http://www.dotblogs.com.tw/larrynung/archive/2013/06/12/105458.aspx

缓存什么时候失效的检查是自定义的的关键,如以上示例所示,使用 Timer 组件来触发检查事件,其局限性为:必须引用System.Windows.Form.dll类库,并且性能也消耗也大,可参考:Timer 与 Thread 线程的性能比较。

这是我的实现1:

    internal class PartyChangeMonitor : ChangeMonitor

    {

        #region 私有变量
        private string _uniqueID = Guid.NewGuid().ToString();
        private DateTime _MonitorTime;
        private int _PartyID;
        private int _CheckInterval;

        #endregion

        #region Public Property
        public override string UniqueId
        {
            get { return _uniqueID; }
        }
        #endregion

        #region Constructor
        public PartyChangeMonitor(int partyID, int checkInterval)
        {
            _PartyID = partyID;
            _CheckInterval = checkInterval * 1000;
            _MonitorTime = DateTime.Now;
            isStoped = false;

            this.StartDaemon();
            InitializationComplete();
        }
        #endregion

        #region Protected Method
        protected override void Dispose(bool disposing)
        {

        }
        #endregion

        #region 心跳监控
        Thread thrDaemon;
        bool isStoped = true;
        //登录时启动,开始心跳检测
        void StartDaemon()
        {
            //System.Threading.ThreadPool.get
            this.thrDaemon = new Thread(new ThreadStart(this.Daemon));
            thrDaemon.Start();
        }
        /// <summary>
        
/// 监视任务,如果已过期,则停止检测
        
/// </summary>
        void Daemon()
        {
            while (!this.isStoped)
            {
                Thread.Sleep(_CheckInterval);

                DateTime _LastUpdatedTime = Party.GetLastUpdatedTime(_PartyID);
                if (_LastUpdatedTime > _MonitorTime)
                {
                    OnChanged(_LastUpdatedTime);
                    isStoped = true;
                }
            }
        }
        #endregion
    }

以上实现有个问题是,每个检查策略都会开启一个线程,这样如果有1000个策略呢,10000个呢,CPU在线程间切换的性能消耗就把机器拖跨,这个实现虽然比使用Timer组件要好很多,但离真正的可用还是差哪么一步,因此,才有了以下实现: 

这是我的实现2: 

    internal class PartyChangeMonitor2 : ChangeMonitor 

    {
        #region 私有变量
        private string _uniqueID ;
        private DateTime _MonitorTime;
        private int _PartyID;
        #endregion

        #region Public Property
        public override string UniqueId
        {
            get { return _uniqueID; }
        }
        #endregion

        #region Constructor
        public PartyChangeMonitor2(int partyID, int checkInterval)
        {            
            _uniqueID = Guid.NewGuid().ToString();

            _PartyID = partyID;
            _MonitorTime = DateTime.Now;

            LocalCache.RegisterCheckHandler(UniqueId,CheckExpired, checkInterval);
            InitializationComplete();
        }
        #endregion

        #region Protected Method
        protected override void Dispose(bool disposing)
        {
            LocalCache.RemoveCheckHandler(this.UniqueId);
            base.Dispose();
        }
        #endregion

        #region 过期检测
        /// <summary>
        
/// 如果已过期,则停止检测
        
/// </summary>
        
/// <returns>true 数据已过期,可以移除检测,false 数据有效,要继续检测</returns>
        public bool CheckExpired()
        {
            DateTime _LastUpdatedTime = Party.GetLastUpdatedTime(_PartyID);
            if (_LastUpdatedTime > _MonitorTime)
            {
                OnChanged(_LastUpdatedTime);
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }

 此实现无论有多少 缓存策略,都只有一个守护线程在工作,最大程度提高了系统性能与可用性。