Avatar of ElPollo123
ElPollo123Flag for United States of America asked on

custom change monitors in net 4.0 using system.runtime.caching

hello experts.
I am currently using system.runtime.caching to cache some lists in my program.  I know that this also supports the ability to create my own custom policy or monitor.  I need to create my own custom monitor.  basically I want to invalidate the cache if an execution number has changed.  For instance the program starts and will have an execution number of 1, when I load the list and cache it i want to increment the execution number to 2, if that execution number changes to 3 i want to invalidate the cache and reload it from the database.  

Does anyone know how to accomplish this?  Perhaps  a start would be helpful, I don't even know how to create a custom monitor yet.

.NET ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon
Bob Learned

I haven't worked with the System.Runtime.Caching namespace much, since I don't have the luxury of running 4.0 assemblies yet.  I do know, though, that references are hard to come by for examples.  It is strange to have incomplete documentation, but I did find an example with the HostFileChangeMonitor class.  If you need a custom monitor, the ChangeMonitor class is an abstract base class that you would need to start with.

HostFileChangeMonitor Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Caching;
using System.IO;

public partial class _Default : System.Web.UI.Page

    protected void Button1_Click1(object sender, EventArgs e)
        ObjectCache cache = MemoryCache.Default;
        string fileContents = cache["filecontents"] as string;

        if (fileContents == null)
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration =

            List<string> filePaths = new List<string>();
            string cachedFilePath = Server.MapPath("~") +



            // Fetch the file contents.
            fileContents = File.ReadAllText(cachedFilePath) + "\n"
                + DateTime.Now.ToString();

            cache.Set("filecontents", fileContents, policy);


        Label1.Text = fileContents;

Open in new window


Thanks for the response,
I have had trouble finding references fro examples thus far; and I have found the example you posted, i Guess my question is how would I go about creating a custom monitor?  What would the syntax be?

Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

That has some good information, than you, I am getting closer.
In the above example they are using a timer control (which is already defined) to fire when the elapsed time has expired.  the timer control has a specific control for that called Elapsed.

how would i do a similar thing not based on time?  I want to do it on something original like an execution count (int)...

Any ideas?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Bob Learned

Do you want to monitor method execution count for a class instance?

Yes I do
Bob Learned

Then, I think that you need to look at Castle's Dynamic Proxy.

Using Castle’s Dynamic Proxy

What does a dynamic proxy do

A dynamic proxy is a way of generating a subclass from a class or interface of which is generally a model. That subclass overrides every method that it can (make your methods virtual to allow to do it). This provides the ability to intercept calls to all methods on your class/interface because the sub-classed methods route requests to an interface which dictates whether a call can proceed. You could implement that functionality yourself, however you would need to cater for all method calls. The dynamic proxy provides one interceptor handler for all methods, and you can have many interceptors on one class.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

That seems a bit complex for such a simple sounding task that i want to accomplish.
All I want to do is see an example like the ones your posted but with one change; instead of using the hosfilechangemonitor, or timer control, I want to have a global variable called execount that will increment every time the load is called for a particular list.  Load meaning that it gets it from the database vs cache.

i would try to get the cached version first, then move to the load if it was null.
I would imagine that there is a simple way to do this.
Does that make sense?

Thank for all your help!
Bob Learned

The problem, that I see, is that this function would need to come from the monitor, and how would it know what you were working with.  The interceptor pattern allows you to attach to the method, and detect when it is called, without having to make any changes to the class itself.

Thank you for the help,  I will close this question now.
Your help has saved me hundreds of hours of internet surfing.