asp.net自定义缓存依赖,刷新所有在某一时刻 [英] asp.net custom cache dependency, refresh all in at one moment

查看:94
本文介绍了asp.net自定义缓存依赖,刷新所有在某一时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义缓存依赖

I've got a custom cache dependency

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

It works perfectly, but Im wondering how to make a refresh of all the object in one time. Here I put into cache 2 objects

Cache.Insert(C1,VAR1,新MyCacheDependency());

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

Cache.Insert(C2,vae2,新MyCacheDependency());

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

它很好,但是当C1将检测的变化如何强制C2以不等待5秒钟来检查,但我想自称DependencyDispose当C1做到这一点。

Its fine, but when c1 will detect change how to force c2 to don't wait 5 seconds to check but I want to call itself DependencyDispose when c1 do it.

在换句话说,如果c1检测变化,C2也应该调用DependencyDispose

In other words, if c1 detects change, c2 also should call DependencyDispose

推荐答案

也许你可以添加这将在您的CheckDependencyCallback()被触发静态事件 - 方法。在您的构造函数MyCacheDependency你会再附上一个事件处理程序。当事件被触发,你可以从那里打电话NotifyDependencyChanged或DependencyDispose。这样,所有的MyCacheDependency对象将作出反应的变化。

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

这篇关于asp.net自定义缓存依赖,刷新所有在某一时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆