Skip to content
janjongboom edited this page Sep 1, 2011 · 1 revision

Moths options model is pluggable, and therefore you can also create a Moth provider that doesn't cache, minify, etc. This provider can be plugged in during runtime.

The DebugProvider

This is an example provider you can add to your project:

public class DebugProvider : IOutputCacheProvider
{
    public T Get<T>(string key) where T : class
    {
        return default(T);
    }

    public void Store(string key, object o, TimeSpan duration)
    {
        /* tah dah */
    }

    public IOutputCacheDurations CacheDurations
    {
        get
        {
            return new OutputCacheDurations()
                {
                    DataUri = new TimeSpan(0, 5, 0),
                    ExternalScript = new TimeSpan(0, 5, 0),
                    InlineScript = new TimeSpan(0, 5, 0),
                    PageOutput = new TimeSpan(0, 5, 0)
                };
        }
    }

    public IOutputCacheRestrictions Enable
    {
        get 
		{ 
            return new OutputCacheRestrictions()
            {
                PageOutput = true,
                CssTidy = false,
                ScriptMinification = false,
            };
        }
    }
}

Plug it in!

Example usage of the debug flag is via the querystring parameter (f.e. ?disablemoth=1), therefore you can swap providers during the Application_BeginRequest in your global.asax file.

public class Global : HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var app = sender as HttpApplication;
        if (app != null)
        {
            if(app.Request.QueryString["disablemoth"] == "1")
            {
                MothAction.Initialize(new DebugProvider());
            }
            else { MothAction.Initialize(new AspNetCacheProvider()); }
        }
    }
}
Clone this wiki locally