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

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

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”, new MyCacheDependency());

Cache.Insert(“c2”, “vae2”, new MyCacheDependency());

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.

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x