【问题标题】:asp.net custom cache dependency, refresh all in at one momentasp.net自定义缓存依赖,一瞬间全部刷新
【发布时间】:2012-02-26 17:28:52
【问题描述】:

我有一个自定义缓存依赖项

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

    public MyCacheDependency()
    {
        _timer = new Timer(CheckDependencyCallback, this, PoolInterval, PoolInterval);
        _readedContent = ReadContentFromFile();
    }

    private void CheckDependencyCallback(object sender)
    {
        lock (_timer)
        {
            if (_readedContent != ReadContentFromFile())
            {
                NotifyDependencyChanged(sender, EventArgs.Empty);
            }
        }
    }

    private static string ReadContentFromFile()
    {
        return File.ReadAllText(@"C:\file.txt");
    }

    protected override void DependencyDispose()
    {
        if (_timer != null) _timer.Dispose();

        base.DependencyDispose();
    }
}

它工作得很好,但我想知道如何一次刷新所有对象。这里我放入缓存 2 个对象

Cache.Insert("c1", "var1", new MyCacheDependency());

Cache.Insert("c2", "vae2", new MyCacheDependency());

很好,但是当 c1 将检测到更改时如何强制 c2 不等待 5 秒进行检查但我想在 c1 执行此操作时调用自己 DependencyDispose。

也就是说,如果 c1 检测到变化,c2 也应该调用 DependencyDispose

【问题讨论】:

    标签: c# asp.net caching


    【解决方案1】:

    也许您可以添加一个静态事件,该事件将在您的 CheckDependencyCallback() 方法中触发。然后在 MyCacheDependency 的构造函数中附加一个事件处理程序。当事件被触发时,您可以从那里调用 NotifyDependencyChanged 或 DependencyDispose。这样,所有 MyCacheDependency 对象都会对更改做出反应。

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多