Caching the VoiceModel in ASP.NET MVC

Caching the VoiceModel and state machine for an ASP.NET MVC VoiceXML application is very simple. In the previous post Where is the Controller For an MVC VoiceXML Application I demonstrated how to create a voice application to get the current weather conditions in an area identified by a zip code.  In this post the VoiceModel and state machine were recreated each time the controller was called.  I have modified the example to cache this information by slightly modifying the Builder classes for the VoiceModel and state machine.  The Builder classes follow the Builder design pattern and are responsible for building the object graphs that represent the application VoiceModel and call flow.  It makes sense to handle the caching in these classes since they are responsible for creating these objects.  Here is what the class for the state machine builder looks like now.


public class WeatherCallFlowBuilder 
{
    const string cacheId = "weather.cf";

    public static ICallFlow Build()
    {
        CallFlow flow = (CallFlow)HttpRuntime.Cache.Get(cacheId);
        if (flow == null)
        {
            flow = Recreate();
            HttpContext.Current.Cache.Insert(cacheId, flow);
        }
        return flow;

    }

    public static CallFlow Recreate()
    {
        CallFlow flow = new CallFlow();
        flow.AddStartState(new State("greeting", "getZip"));
        flow.AddState(new State("getZip", "getWeather"));
        flow.AddState(new GetWeather("getWeather", "voiceWeather", 
            new DAL.WeatherServiceMockup()));
        flow.AddState(new State("voiceWeather", "goodbye"));
        flow.AddState(new State("goodbye"));
        return flow;

    }
}

Now the Build method first checks the cache to see if this object graph is available.  If it is not available it recreates it.  The Builder class for the VoiceModel uses caching in the same way. We can cache this information because we are not keeping any state information in these models.  Any state information is passed to these models for processing. Most of the required state information comes from the VoiceXML browser. Caching this information will greatly improve performance on larger applications.  You can get the updated VS 2010 solution for this example from the VoiceModel Project on CodePlex.

Comments

Popular posts from this blog

Using Claims in ASP.NET Identity

Seeding & Customizing ASP.NET MVC SimpleMembership

Customizing Claims for Authorization in ASP.NET Core 2.0