asp.net custom cache dependency, refresh all in at one moment

后端 未结 1 468
[愿得一人]
[愿得一人] 2021-01-24 20:03

I\'ve got a custom cache dependency

class MyCacheDependency : CacheDependency
{
    private const int PoolInterval = 5000;
    private readonly Timer _timer;
             


        
相关标签:
1条回答
  • 2021-01-24 20:32

    Maybe you could add a static event which would be fired in your CheckDependencyCallback()-method. In your constructor for the MyCacheDependency you would then attach an eventhandler. When the event is fired you could call NotifyDependencyChanged or DependencyDispose from there. In this way all MyCacheDependency-objects would react to a change.

    class MyCacheDependency : CacheDependency
    {
        private const int PoolInterval = 5000;
        private readonly Timer _timer;
        private readonly string _readedContent;
    
        public static event EventHandler MyEvent;
    
        public MyCacheDependency()
        {
            _timer = new Timer(CheckDependencyCallback, this, PoolInterval, PoolInterval);
            _readedContent = ReadContentFromFile();
            MyEvent += new EventHandler(MyEventHandler);
        }
    
       protected void MyEventHandler(object sender, EventArgs e) {
        NotifyDependencyChanged(sender, e);
       }
    
        private void CheckDependencyCallback(object sender)
        {
            lock (_timer)
            {
                if (_readedContent != ReadContentFromFile())
                {
                    if(MyEvent!=null)
                        MyEvent(sender, EventArgs.Empty);
                }
            }
        }
    
        private static string ReadContentFromFile()
        {
            return File.ReadAllText(@"C:\file.txt");
        }
    
        protected override void DependencyDispose()
        {
            if (_timer != null) _timer.Dispose();
    
            base.DependencyDispose();
        }
    }
    
    0 讨论(0)
提交回复
热议问题