Link to home
Start Free TrialLog in
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.

Thanks
EP
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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
http://msdn.microsoft.com/en-us/library/system.runtime.caching.hostfilechangemonitor.aspx


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 =
                DateTimeOffset.Now.AddSeconds(10.0);

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

            filePaths.Add(cachedFilePath);

            policy.ChangeMonitors.Add(new
                HostFileChangeMonitor(filePaths));

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

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

        }

        Label1.Text = fileContents;
    }
}

Open in new window

Avatar of ElPollo123

ASKER

TheLearnedOne,
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?

 
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That has some good information, than you, I am getting closer.
However,
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?
Do you want to monitor method execution count for a class instance?
Yes I do
Then, I think that you need to look at Castle's Dynamic Proxy.

Using Castle’s Dynamic Proxy
http://richardwilburn.wordpress.com/2009/12/17/using-castles-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.
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!
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.