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

The Moth options model is based on IOutputCacheProvider. By default the options are found in AspNetCacheProvider, but you can roll your own.

Initializing a custom options model

Call in Global.asax in the Application_Start function the following code:

MothAction.Initialize(new CustomMothProvider());

Options

For simple options, override the AspNetCacheProvider, and create your own IOutputCacheRestrictions object:

public class CustomMothProvider : AspNetCacheProvider
{
    public override IOutputCacheRestrictions Enable
    {
        get
        {
            return new OutputCacheRestrictions()
            {
                // you can disabled Moth's engine with this setting
                PageOutput = true,
                // disable CssTidy
                CssTidy = false,
                // disable all minification
                ScriptMinification = false,
            };
        }
    }
}

Changing cache duration

By default Moth caches all items between 10 and 20 minutes, but you can vary this:

public class CustomDurationProvider : AspNetCacheProvider
{
    public override IOutputCacheDurations CacheDurations
    {
        get
        {
            return new OutputCacheDurations()
                {
                    // change the durations for these modules
                    DataUri = new TimeSpan(0, 5, 0),
                    ExternalScript = new TimeSpan(0, 5, 0),
                    InlineScript = new TimeSpan(0, 10, 0),
                    PageOutput = new TimeSpan(0, 30, 0)
                };
        }
    }
}

Rolling your own cache provider

By default Moth uses ASP.NET Cache to store all items that are cached, but you can use any other cache store. In this cache, you should inherit IOutputCacheProvider instead of AspNetCacheProvider. For example, a fictional Memcached provider:

public class CustomDurationProvider : IOutputCacheProvider
{
    public T Get<T>(string key) where T : class
    {
        return (T)Memcached.Get(key);
    }

    public void Store(string key, object o, TimeSpan duration)
    {
        return Memcached.Set(key, o, duration);
    }

    public IOutputCacheDurations CacheDurations
    {
        get { /* todo */ }
    }

    public IOutputCacheRestrictions Enable
    {
        get { /* todo */ }
    }
}

Clone this wiki locally